Skip to content

Commit a2cfd87

Browse files
author
Yuki YOSHIDA
committed
replace IWebRequest#getRequestURL()
1 parent e84b7b0 commit a2cfd87

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.thymeleaf.standard.expression.StandardExpressions;
3131
import org.thymeleaf.web.IWebExchange;
3232
import org.thymeleaf.web.IWebRequest;
33+
import org.thymeleaf.web.servlet.IServletWebRequest;
3334
import org.unbescape.html.HtmlEscape;
3435

3536
@SuppressWarnings("unchecked")
@@ -165,7 +166,7 @@ private static String buildBaseUrl(final ITemplateContext context, Collection<St
165166
final IWebRequest request = webExchange.getRequest();
166167

167168
// URL base path from request
168-
builder.append(request.getRequestURL());
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)