Skip to content

Commit 290d75e

Browse files
authored
Merge pull request #45 from sis-yoshiday/features/spring-data-commons-3.x
support spring-data-commons 3.x
2 parents db642b7 + a2cfd87 commit 290d75e

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

pom.xml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@
3333

3434
<properties>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<java.version>17</java.version>
37+
<maven.compiler.source>17</maven.compiler.source>
38+
<maven.compiler.target>17</maven.compiler.target>
3639
</properties>
3740

3841
<build>
3942
<plugins>
4043
<plugin>
4144
<groupId>org.apache.maven.plugins</groupId>
4245
<artifactId>maven-compiler-plugin</artifactId>
43-
<version>3.3</version>
44-
<configuration>
45-
<source>1.6</source>
46-
<target>1.6</target>
47-
</configuration>
46+
<version>3.8.1</version>
4847
</plugin>
4948
<plugin>
5049
<groupId>org.apache.maven.plugins</groupId>
5150
<artifactId>maven-source-plugin</artifactId>
52-
<version>3.0.1</version>
51+
<version>3.2.1</version>
5352
<executions>
5453
<execution>
5554
<id>attach-sources</id>
@@ -62,7 +61,7 @@
6261
<plugin>
6362
<groupId>org.apache.maven.plugins</groupId>
6463
<artifactId>maven-javadoc-plugin</artifactId>
65-
<version>3.0.1</version>
64+
<version>3.3.0</version>
6665
<executions>
6766
<execution>
6867
<id>attach-javadocs</id>
@@ -90,21 +89,15 @@
9089
</build>
9190

9291
<dependencies>
93-
<dependency>
94-
<groupId>javax.servlet</groupId>
95-
<artifactId>servlet-api</artifactId>
96-
<version>2.4</version>
97-
<scope>provided</scope>
98-
</dependency>
9992
<dependency>
10093
<groupId>org.springframework.data</groupId>
10194
<artifactId>spring-data-commons</artifactId>
102-
<version>2.6.0</version>
95+
<version>3.0.0</version>
10396
</dependency>
10497
<dependency>
10598
<groupId>org.thymeleaf</groupId>
10699
<artifactId>thymeleaf</artifactId>
107-
<version>3.0.14.RELEASE</version>
100+
<version>3.1.1.RELEASE</version>
108101
</dependency>
109102
</dependencies>
110103

src/main/java/org/thymeleaf/dialect/springdata/util/PageUtils.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111

1212
import java.util.Arrays;
1313
import java.util.Collection;
14-
import java.util.Enumeration;
1514
import java.util.Iterator;
1615
import java.util.Map;
1716
import java.util.Map.Entry;
1817
import java.util.Set;
1918

20-
import javax.servlet.http.HttpServletRequest;
21-
2219
import org.springframework.data.domain.Page;
2320
import org.springframework.data.domain.Sort;
2421
import org.springframework.data.domain.Sort.Direction;
@@ -31,6 +28,9 @@
3128
import org.thymeleaf.standard.expression.IStandardExpression;
3229
import org.thymeleaf.standard.expression.IStandardExpressionParser;
3330
import org.thymeleaf.standard.expression.StandardExpressions;
31+
import org.thymeleaf.web.IWebExchange;
32+
import org.thymeleaf.web.IWebRequest;
33+
import org.thymeleaf.web.servlet.IServletWebRequest;
3434
import org.unbescape.html.HtmlEscape;
3535

3636
@SuppressWarnings("unchecked")
@@ -60,12 +60,12 @@ public static Page<?> findPage(final ITemplateContext context) {
6060

6161
// Search for Page object, and only one instance, as request attribute
6262
if (context instanceof IWebContext) {
63-
HttpServletRequest request = ((IWebContext) context).getRequest();
64-
Enumeration<String> attrNames = request.getAttributeNames();
63+
IWebExchange webExchange = ((IWebContext) context).getExchange();
64+
Set<String> attrNames = webExchange.getAllAttributeNames();
6565
Page<?> pageOnRequest = null;
66-
while (attrNames.hasMoreElements()) {
67-
String attrName = (String) attrNames.nextElement();
68-
Object attr = request.getAttribute(attrName);
66+
67+
for (String attrName : attrNames) {
68+
Object attr = webExchange.getAttributeValue(attrName);
6969
if (isPageInstance(attr)) {
7070
if (pageOnRequest != null) {
7171
throw new InvalidObjectParameterException("More than one Page object found on request!");
@@ -162,10 +162,11 @@ private static String buildBaseUrl(final ITemplateContext context, Collection<St
162162
// Creates url from actual request URI and parameters
163163
final StringBuilder builder = new StringBuilder();
164164
final IWebContext webContext = (IWebContext) context;
165-
final HttpServletRequest request = webContext.getRequest();
165+
final IWebExchange webExchange = webContext.getExchange();
166+
final IWebRequest request = webExchange.getRequest();
166167

167168
// URL base path from request
168-
builder.append(request.getRequestURI());
169+
builder.append(getRequestURI(request));
169170

170171
Map<String, String[]> params = request.getParameterMap();
171172
Set<Entry<String, String[]>> entries = params.entrySet();
@@ -203,6 +204,31 @@ private static String buildBaseUrl(final ITemplateContext context, Collection<St
203204
return url == null ? EMPTY : url;
204205
}
205206

207+
private static String getRequestURI(IWebRequest webRequest) {
208+
if (webRequest instanceof IServletWebRequest servletWebRequest) {
209+
return servletWebRequest.getRequestURI();
210+
} else {
211+
// from org.thymeleaf.web.IWebRequest.getRequestURL
212+
String scheme = webRequest.getScheme();
213+
String serverName = webRequest.getServerName();
214+
Integer serverPort = webRequest.getServerPort();
215+
String requestPath = webRequest.getRequestPath();
216+
if (scheme != null && serverName != null && serverPort != null) {
217+
StringBuilder urlBuilder = new StringBuilder();
218+
urlBuilder.append(scheme).append("://").append(serverName);
219+
if ((!scheme.equals("http") || serverPort != 80) && (!scheme.equals("https") || serverPort != 443)) {
220+
urlBuilder.append(':').append(serverPort);
221+
}
222+
223+
urlBuilder.append(requestPath);
224+
225+
return urlBuilder.toString();
226+
} else {
227+
throw new UnsupportedOperationException("Request scheme, server name or port are null in this environment. Cannot compute request URL");
228+
}
229+
}
230+
}
231+
206232
private static boolean isPageInstance(Object page) {
207233
return page != null && (page instanceof Page<?>);
208234
}

0 commit comments

Comments
 (0)