Skip to content

Commit 4bed1c5

Browse files
authored
feat: Make helper method public to allow Spring to use it (#10863)
* feat: Make helper method public to allow Spring to use it
1 parent 86235b4 commit 4bed1c5

2 files changed

Lines changed: 144 additions & 14 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/HandlerHelper.java‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Locale;
25+
import java.util.Objects;
2526
import java.util.Optional;
2627
import java.util.function.BiConsumer;
2728
import java.util.regex.Pattern;
@@ -191,29 +192,77 @@ static boolean isInternalRequestInsideServlet(
191192
return false;
192193
}
193194

194-
private static Optional<String> getPathIfInsideServlet(
195+
/**
196+
* Returns the rest of the path after the servlet mapping part, if the
197+
* requested path targets a path inside the servlet.
198+
*
199+
* @param servletMappingPath
200+
* the servlet mapping from the servlet configuration
201+
* @param requestedPath
202+
* the request path relative to the context root
203+
* @return an optional containing the path relative to the servlet if the
204+
* request is inside the servlet mapping, an empty optional
205+
* otherwise
206+
*/
207+
public static Optional<String> getPathIfInsideServlet(
195208
String servletMappingPath, String requestedPath) {
209+
Objects.requireNonNull(servletMappingPath,
210+
"servletMappingPath cannot be null");
211+
Objects.requireNonNull(requestedPath, "requestedPath cannot be null");
212+
213+
if ("/*".equals(servletMappingPath) || "/".equals(servletMappingPath)) {
214+
// All paths are inside a /* servlet and a / servlet
215+
return Optional.of(requestedPath);
216+
}
217+
196218
if (servletMappingPath.endsWith("/*")) {
197-
servletMappingPath = servletMappingPath.substring(0,
198-
servletMappingPath.length() - "/*".length());
219+
String servletPrefix = servletMappingPath.substring(0,
220+
servletMappingPath.length() - 1); // Only remove "*"
221+
if (requestedPath.startsWith(servletPrefix)) {
222+
return Optional
223+
.of(requestedPath.substring(servletPrefix.length()));
224+
}
225+
return Optional.empty();
199226
}
200-
if ("/".equals(servletMappingPath)) {
201-
servletMappingPath = "";
227+
228+
// Servlet is mapped only to a static path such as "" or /foo/bar
229+
String servletMappingWithoutSlash;
230+
231+
if (servletMappingPath.startsWith("/")) {
232+
// Requested path should not contain the initial slash
233+
servletMappingWithoutSlash = servletMappingPath.substring(1);
234+
} else {
235+
servletMappingWithoutSlash = servletMappingPath;
202236
}
203-
if (!requestedPath.startsWith(servletMappingPath)) {
204-
return Optional.empty();
237+
238+
if (requestedPath.equals(servletMappingWithoutSlash)) {
239+
return Optional.of(requestedPath
240+
.substring(servletMappingWithoutSlash.length()));
205241
}
206-
return Optional
207-
.of(requestedPath.substring(servletMappingPath.length()));
242+
243+
return Optional.empty();
208244
}
209245

246+
/**
247+
* Returns the requested path inside the context root.
248+
*
249+
* @param request
250+
* the servlet request
251+
* @return the path inside the context root, not including the slash after
252+
* the context root path
253+
*/
210254
private static String getRequestPathInsideContext(
211255
HttpServletRequest request) {
212256
String servletPath = request.getServletPath();
213257
String pathInfo = request.getPathInfo();
214258
String url = "";
215259
if (servletPath != null) {
216-
url += servletPath;
260+
if (servletPath.startsWith("/")) {
261+
// This SHOULD always be true...
262+
url += servletPath.substring(1);
263+
} else {
264+
url += servletPath;
265+
}
217266
}
218267
if (pathInfo != null) {
219268
url += pathInfo;

‎flow-server/src/test/java/com/vaadin/flow/server/HandlerHelperTest.java‎

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.HashSet;
5+
import java.util.Optional;
56
import java.util.Set;
67

78
import javax.servlet.http.HttpServletRequest;
@@ -107,7 +108,7 @@ public void isFrameworkInternalRequest_noType() {
107108

108109
@Test
109110
public void isFrameworkInternalRequest_validType_withPath() {
110-
HttpServletRequest request = createRequest("/hello", RequestType.INIT);
111+
HttpServletRequest request = createRequest("hello", RequestType.INIT);
111112

112113
Assert.assertFalse(
113114
HandlerHelper.isFrameworkInternalRequest("/", request));
@@ -119,17 +120,19 @@ public void isFrameworkInternalRequest_validType_withPath() {
119120
HandlerHelper.isFrameworkInternalRequest("/foo/*", request));
120121
Assert.assertTrue(
121122
HandlerHelper.isFrameworkInternalRequest("/hello", request));
122-
Assert.assertTrue(
123+
Assert.assertFalse(
123124
HandlerHelper.isFrameworkInternalRequest("/hello/*", request));
124125
}
125126

126127
@Test
127128
public void isFrameworkInternalRequest_validType_withServletMappingAndPath() {
128-
HttpServletRequest request = createRequest("/", RequestType.INIT);
129+
HttpServletRequest request = createRequest("", RequestType.INIT);
129130
Mockito.when(request.getServletPath()).thenReturn("/servlet");
130131

131132
Assert.assertFalse(
132133
HandlerHelper.isFrameworkInternalRequest("/", request));
134+
Assert.assertFalse(
135+
HandlerHelper.isFrameworkInternalRequest("", request));
133136
Assert.assertTrue(
134137
HandlerHelper.isFrameworkInternalRequest("/servlet", request));
135138
}
@@ -194,11 +197,89 @@ public void isFrameworkInternalRequest_vaadinRequest_nonRootServlet_pathInsideSe
194197
BootstrapHandler.isFrameworkInternalRequest(request));
195198
}
196199

200+
@Test
201+
public void getPathIfInsideServlet_default_servlet() {
202+
String servletMapping = "/*";
203+
Assert.assertEquals(Optional.of(""),
204+
HandlerHelper.getPathIfInsideServlet(servletMapping, ""));
205+
Assert.assertEquals(Optional.of("/"),
206+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/"));
207+
Assert.assertEquals(Optional.of("foo"),
208+
HandlerHelper.getPathIfInsideServlet(servletMapping, "foo"));
209+
Assert.assertEquals(Optional.of("/foo"),
210+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foo"));
211+
}
212+
213+
@Test
214+
public void getPathIfInsideServlet_root_only_servlet() {
215+
String servletMapping = "";
216+
Assert.assertEquals(Optional.of(""),
217+
HandlerHelper.getPathIfInsideServlet(servletMapping, ""));
218+
Assert.assertEquals(Optional.empty(),
219+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/"));
220+
Assert.assertEquals(Optional.empty(),
221+
HandlerHelper.getPathIfInsideServlet(servletMapping, "foo"));
222+
Assert.assertEquals(Optional.empty(),
223+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foo"));
224+
}
225+
226+
@Test
227+
public void getPathIfInsideServlet_all_urls_servlet() {
228+
String servletMapping = "/";
229+
Assert.assertEquals(Optional.of(""),
230+
HandlerHelper.getPathIfInsideServlet(servletMapping, ""));
231+
Assert.assertEquals(Optional.of("/"),
232+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/"));
233+
Assert.assertEquals(Optional.of("foo"),
234+
HandlerHelper.getPathIfInsideServlet(servletMapping, "foo"));
235+
Assert.assertEquals(Optional.of("/foo"),
236+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foo"));
237+
}
238+
239+
@Test
240+
public void getPathIfInsideServlet_sevlet_using_single_path() {
241+
String servletMapping = "/foo";
242+
Assert.assertEquals(Optional.empty(),
243+
HandlerHelper.getPathIfInsideServlet(servletMapping, ""));
244+
Assert.assertEquals(Optional.empty(),
245+
HandlerHelper.getPathIfInsideServlet(servletMapping, "bar"));
246+
Assert.assertEquals(Optional.empty(),
247+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/bar"));
248+
Assert.assertEquals(Optional.of(""),
249+
HandlerHelper.getPathIfInsideServlet(servletMapping, "foo"));
250+
Assert.assertEquals(Optional.empty(),
251+
HandlerHelper.getPathIfInsideServlet(servletMapping, "foo/"));
252+
Assert.assertEquals(Optional.empty(), HandlerHelper
253+
.getPathIfInsideServlet(servletMapping, "foo/bar"));
254+
}
255+
256+
@Test
257+
public void getPathIfInsideServlet_sevlet_with_context_path() {
258+
String servletMapping = "/foo/*";
259+
Assert.assertEquals(Optional.empty(),
260+
HandlerHelper.getPathIfInsideServlet(servletMapping, ""));
261+
Assert.assertEquals(Optional.empty(),
262+
HandlerHelper.getPathIfInsideServlet(servletMapping, "bar"));
263+
Assert.assertEquals(Optional.empty(),
264+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/bar"));
265+
Assert.assertEquals(Optional.empty(),
266+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foo"));
267+
Assert.assertEquals(Optional.empty(),
268+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foos"));
269+
Assert.assertEquals(Optional.empty(), HandlerHelper
270+
.getPathIfInsideServlet(servletMapping, "/foos/bar"));
271+
Assert.assertEquals(Optional.of(""),
272+
HandlerHelper.getPathIfInsideServlet(servletMapping, "/foo/"));
273+
Assert.assertEquals(Optional.of("bar"), HandlerHelper
274+
.getPathIfInsideServlet(servletMapping, "/foo/bar"));
275+
}
276+
197277
private VaadinRequest createVaadinRequest(String requestPath,
198278
String servletPath, RequestType type) {
199279
HttpServletRequest servletRequest = createRequest(requestPath, type);
200280
if (servletPath.equals("/*")) {
201-
// This is what the spec says HttpServletRequest#getServletPath
281+
// This is what the spec says
282+
// HttpServletRequest#getServletPath
202283
servletPath = "";
203284
}
204285
Mockito.when(servletRequest.getServletPath()).thenReturn(servletPath);

0 commit comments

Comments
 (0)