Skip to content

Commit 3773b7d

Browse files
authored
refactor: Integrate dev server static resource serving with StaticFileServer (#12256)
1 parent 6cb6644 commit 3773b7d

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

flow-server/src/main/java/com/vaadin/flow/internal/DevModeHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
public interface DevModeHandler extends RequestHandler {
2626

2727
/**
28-
* Return webpack console output when a compilation error happened.
28+
* Return the dev server console output when a compilation error happened.
2929
*
3030
* @return console output if error or null otherwise.
3131
*/
3232
String getFailedOutput();
3333

3434
/**
35-
* Prepare a HTTP connection against webpack-dev-server.
35+
* Prepare a HTTP connection against the dev server.
3636
*
3737
* @param path
3838
* the file to request, needs to be safe
@@ -46,22 +46,22 @@ HttpURLConnection prepareConnection(String path, String method)
4646
throws IOException;
4747

4848
/**
49-
* Returns true if it's a request that should be handled by webpack.
49+
* Returns true if it's a request that should be handled by the dev server.
5050
*
5151
* @param request
5252
* the servlet request
53-
* @return true if the request should be forwarded to webpack
53+
* @return true if the request should be forwarded to the dev server
5454
*/
5555
boolean isDevModeRequest(HttpServletRequest request);
5656

5757
/**
58-
* Serve a file by proxying to webpack.
58+
* Serve a file by proxying to the dev server.
5959
*
6060
* @param request
6161
* the servlet request
6262
* @param response
6363
* the servlet response
64-
* @return false if webpack returned a not found, true otherwise
64+
* @return false if the dev server returned a not found, true otherwise
6565
* @throws IOException
6666
* in the case something went wrong like connection refused
6767
*/

flow-server/src/main/java/com/vaadin/flow/server/StaticFileServer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.slf4j.LoggerFactory;
4343

4444
import com.vaadin.flow.function.DeploymentConfiguration;
45+
import com.vaadin.flow.internal.DevModeHandler;
46+
import com.vaadin.flow.internal.DevModeHandlerManager;
4547
import com.vaadin.flow.internal.ResponseWriter;
4648
import com.vaadin.flow.server.frontend.FrontendUtils;
4749

@@ -73,6 +75,7 @@ public class StaticFileServer implements StaticFileHandler {
7375
private final VaadinService vaadinService;
7476
private DeploymentConfiguration deploymentConfiguration;
7577
private final List<String> manifestPaths;
78+
private DevModeHandler devModeHandler;
7679

7780
// Matcher to match string starting with '/themes/[theme-name]/'
7881
public static final Pattern APP_THEME_PATTERN = Pattern
@@ -93,6 +96,9 @@ public StaticFileServer(VaadinService vaadinService) {
9396
deploymentConfiguration = vaadinService.getDeploymentConfiguration();
9497
responseWriter = new ResponseWriter(deploymentConfiguration);
9598
manifestPaths = getManifestPathsFromJson();
99+
100+
this.devModeHandler = DevModeHandlerManager
101+
.getDevModeHandler(vaadinService).orElse(null);
96102
}
97103

98104
@Override
@@ -107,6 +113,11 @@ public boolean isStaticResourceRequest(HttpServletRequest request) {
107113
return false;
108114
}
109115

116+
if (devModeHandler != null
117+
&& devModeHandler.isDevModeRequest(request)) {
118+
return true;
119+
}
120+
110121
if (APP_THEME_PATTERN.matcher(requestFilename).find()
111122
|| requestFilename.startsWith("/" + VAADIN_STATIC_FILES_PATH)
112123
|| requestFilename.startsWith("/" + VAADIN_BUILD_FILES_PATH)) {
@@ -273,6 +284,13 @@ public boolean serveStaticResource(HttpServletRequest request,
273284
return true;
274285
}
275286

287+
if (devModeHandler != null
288+
&& devModeHandler.isDevModeRequest(request)) {
289+
if (devModeHandler.serveDevModeRequest(request, response)) {
290+
return true;
291+
}
292+
}
293+
276294
URL resourceUrl = null;
277295
if (isAllowedVAADINBuildOrStaticUrl(filenameWithPath)
278296
|| manifestPaths.contains(filenameWithPath)) {

flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525
import java.io.IOException;
2626
import java.net.MalformedURLException;
2727
import java.net.URL;
28-
import java.util.Optional;
2928
import java.util.Properties;
3029

3130
import com.vaadin.flow.component.UI;
3231
import com.vaadin.flow.di.Lookup;
3332
import com.vaadin.flow.function.DeploymentConfiguration;
3433
import com.vaadin.flow.internal.ApplicationClassLoaderAccess;
3534
import com.vaadin.flow.internal.CurrentInstance;
36-
import com.vaadin.flow.internal.DevModeHandler;
37-
import com.vaadin.flow.internal.DevModeHandlerManager;
3835
import com.vaadin.flow.internal.VaadinContextInitializer;
3936
import com.vaadin.flow.server.HandlerHelper.RequestType;
4037
import com.vaadin.flow.server.startup.ApplicationConfiguration;
@@ -331,15 +328,6 @@ protected void service(HttpServletRequest request,
331328
protected boolean serveStaticOrWebJarRequest(HttpServletRequest request,
332329
HttpServletResponse response) throws IOException {
333330

334-
Optional<DevModeHandler> devModeHandler = DevModeHandlerManager
335-
.getDevModeHandler(getService());
336-
if (devModeHandler.isPresent()
337-
&& devModeHandler.get().isDevModeRequest(request)
338-
&& devModeHandler.get().serveDevModeRequest(request,
339-
response)) {
340-
return true;
341-
}
342-
343331
if (staticFileHandler.isStaticResourceRequest(request)) {
344332
staticFileHandler.serveStaticResource(request, response);
345333
return true;

flow-tests/test-dev-mode/src/test/java/com/vaadin/flow/uitest/ui/UrlValidationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void devModeUriValidation_uriWithDirectoryChange_statusForbidden()
3939
waitUntil(input -> $(LabelElement.class).id("elementId").isDisplayed());
4040
// check the forbidden url
4141
sendRequestAndValidateResponseStatusForbidden(
42-
"/VAADIN/build/%252E%252E/");
42+
"/VAADIN/build/%252E%252E");
4343
}
4444

4545
@Test

vaadin-dev-server/src/test/java/com/vaadin/base/devserver/startup/AbstractDevModeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.vaadin.flow.server.Constants;
2222
import com.vaadin.flow.server.StaticFileHandler;
2323
import com.vaadin.flow.server.StaticFileHandlerFactory;
24+
import com.vaadin.flow.server.StaticFileServer;
2425
import com.vaadin.flow.server.VaadinService;
2526
import com.vaadin.flow.server.VaadinServletContext;
2627
import com.vaadin.flow.server.frontend.FrontendUtils;
@@ -85,7 +86,7 @@ public void setup() throws Exception {
8586
Mockito.when(lookup.lookup(ApplicationConfiguration.class))
8687
.thenReturn(appConfig);
8788
Mockito.when(lookup.lookup(StaticFileHandlerFactory.class))
88-
.thenReturn(service -> Mockito.mock(StaticFileHandler.class));
89+
.thenReturn(service -> new StaticFileServer(service));
8990

9091
vaadinService = Mockito.mock(VaadinService.class);
9192

0 commit comments

Comments
 (0)