Skip to content

Commit

Permalink
version 1.3.1: update view context and add getRequestValueAnyway
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Apr 28, 2024
1 parent 9e660dd commit 35d7b34
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import static com.tvd12.ezyfox.io.EzyStrings.isBlank;
import static com.tvd12.ezyfox.io.EzyStrings.isNotBlank;

public final class HttpServletRequests {

private HttpServletRequests() {}

public static String getRequestValue(HttpServletRequest request, String name) {
/**
* Get request value from attribute or header or parameter or cookie.
*
* @param request the http request.
* @param name the name of value.
* @return the request value.
*/
public static String getRequestValue(
HttpServletRequest request,
String name
) {
String value = (String) request.getAttribute(name);
if (value == null) {
if (isBlank(value)) {
value = request.getHeader(name);
}
if (value == null) {
if (isBlank(value)) {
value = request.getParameter(name);
}
if (value == null && request.getCookies() != null) {
if (isBlank(value) && request.getCookies() != null) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals(name)) {
value = cookie.getValue();
Expand All @@ -29,4 +40,23 @@ public static String getRequestValue(HttpServletRequest request, String name) {
}
return value;
}

/**
* Get request value from attribute or header or parameter or cookie.
* If the value is blank, try to get by lowercase of name.
*
* @param request the http request.
* @param name the name of value.
* @return the request value.
*/
public static String getRequestValueAnyway(
HttpServletRequest request,
String name
) {
String value = getRequestValue(request, name);
if (isBlank(value)) {
value = getRequestValue(request, name.toLowerCase());
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ default Map<String, String> resolveMessages(
.collect(
Collectors.toMap(
it -> it,
it -> resolveMessage(locale, it)
it -> resolveMessage(locale, it),
(o, n) -> n
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public void headerTest() {
// then
Asserts.assertNull(headerOverSize);
Asserts.assertEquals("world", headerByName);
Asserts.assertEquals(
sut.getHeaders(),
EzyMapBuilder.mapBuilder()
.put("hello", "world")
.put("foo", "bar")
.toMap(),
false
);
sut.release();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@ public void getRequestValueTestWithCookie() {
verify(request, times(1)).getParameter("3");
verify(request, times(8)).getCookies();
}

@Test
public void getRequestValueAnywayTest() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);

// when
when(request.getAttribute("1")).thenReturn("a");
when(request.getHeader("2")).thenReturn("b");
when(request.getParameter("3")).thenReturn("c");
when(request.getParameter("a")).thenReturn("d");

// then
Asserts.assertEquals(HttpServletRequests.getRequestValueAnyway(request, "1"), "a");
Asserts.assertEquals(HttpServletRequests.getRequestValueAnyway(request, "2"), "b");
Asserts.assertEquals(HttpServletRequests.getRequestValueAnyway(request, "3"), "c");
Asserts.assertEquals(HttpServletRequests.getRequestValueAnyway(request, "A"), "d");
Asserts.assertNull(HttpServletRequests.getRequestValueAnyway(request, "unknown"));

verify(request, times(1)).getAttribute("1");
verify(request, times(1)).getHeader("2");
verify(request, times(1)).getParameter("3");
verify(request, times(1)).getParameter("A");
verify(request, times(1)).getParameter("a");
verify(request, times(3)).getCookies();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tvd12.ezyhttp.server.core.test.view;

import com.tvd12.ezyfox.collect.Sets;
import com.tvd12.ezyfox.collect.Lists;
import com.tvd12.ezyfox.util.EzyMapBuilder;
import com.tvd12.ezyhttp.server.core.view.View;
import com.tvd12.ezyhttp.server.core.view.ViewContext;
Expand All @@ -10,9 +10,9 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class ViewContextTest {

Expand All @@ -38,7 +38,7 @@ public String resolveMessage(
}
};

Set<String> keys = Sets.newHashSet("hello", "world");
List<String> keys = Lists.newArrayList("hello", "world", "hello");

// when
Map<String, String> actual = sut.resolveMessages(
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-server-jetty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<artifactId>ezyhttp-server-management</artifactId>
<name>ezyhttp-server-management</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<artifactId>ezyhttp-server-thymeleaf</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>ezyhttp-server-tomcat</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.6</version>
</parent>
<artifactId>ezyhttp</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<packaging>pom</packaging>

<name>ezyhttp</name>
Expand Down Expand Up @@ -68,7 +68,7 @@
<javax.servlet.version>4.0.1</javax.servlet.version>
<jetty.version>9.4.50.v20221201</jetty.version>
<tomcat.version>8.5.85</tomcat.version>
<thymeleaf.version>3.1.1.RELEASE</thymeleaf.version>
<thymeleaf.version>3.1.2.RELEASE</thymeleaf.version>
<layout.dialect.version>3.2.0</layout.dialect.version>
</properties>

Expand Down

0 comments on commit 35d7b34

Please sign in to comment.