use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.
the class JavaScriptBootstrapHandler method getErrors.
private JsonValue getErrors(VaadinService service) {
JsonObject errors = Json.createObject();
Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
if (devModeHandler.isPresent()) {
String errorMsg = devModeHandler.get().getFailedOutput();
if (errorMsg != null) {
errors.put("webpack-dev-server", errorMsg);
}
}
return errors.keys().length > 0 ? errors : Json.createNull();
}
use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.
the class AbstractDevServerRunnerTest method shouldPassEncodedUrlToDevServer.
@Test
public void shouldPassEncodedUrlToDevServer() throws Exception {
handler = new DummyRunner();
DevModeHandler devServer = Mockito.spy(handler);
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class));
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getRequestURI()).thenReturn("/foo%20bar");
Mockito.when(request.getPathInfo()).thenReturn("foo bar");
Mockito.when(request.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
AtomicReference<String> requestedPath = new AtomicReference<>();
Mockito.when(devServer.prepareConnection(Mockito.any(), Mockito.any())).then(invocation -> {
requestedPath.set((String) invocation.getArguments()[0]);
return Mockito.mock(HttpURLConnection.class);
});
devServer.serveDevModeRequest(request, response);
Assert.assertEquals("foo%20bar", requestedPath.get());
}
use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.
the class IndexHtmlRequestHandlerTest method should_attachWebpackErrors.
@Test
public void should_attachWebpackErrors() throws Exception {
deploymentConfiguration.setEnableDevServer(true);
deploymentConfiguration.setProductionMode(false);
DevModeHandler devServer = Mockito.mock(DevModeHandler.class);
Mockito.when(devServer.getFailedOutput()).thenReturn("Failed to compile");
Mockito.when(devServer.prepareConnection(Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(HttpURLConnection.class));
service.setContext(context);
DevModeHandlerManager devModeHandlerManager = new DevModeHandlerManager() {
@Override
public Class<?>[] getHandlesTypes() {
return new Class[0];
}
@Override
public void initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
}
@Override
public void setDevModeHandler(DevModeHandler devModeHandler) {
}
@Override
public DevModeHandler getDevModeHandler() {
return devServer;
}
};
ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
Lookup lookup = Lookup.compose(Lookup.of(devModeHandlerManager, DevModeHandlerManager.class), Lookup.of(resourceProvider, ResourceProvider.class));
Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
mockApplicationConfiguration(appConfig);
URL resource = Mockito.mock(URL.class);
Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
when(resource.openStream()).thenReturn(new ByteArrayInputStream("<html><head></head></html>".getBytes()));
// Send the request
indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
String indexHtml = responseOutput.toString(StandardCharsets.UTF_8.name());
Assert.assertTrue("Should have a system error dialog", indexHtml.contains("<div class=\"v-system-error\">"));
Assert.assertTrue("Should show webpack failure error", indexHtml.contains("Failed to compile"));
}
use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.
the class FrontendUtils method getFileContent.
private static String getFileContent(VaadinService service, String path) throws IOException {
DeploymentConfiguration config = service.getDeploymentConfiguration();
InputStream content = null;
try {
Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
content = getFileFromDevModeHandler(devModeHandler.get(), path);
}
if (content == null) {
content = getFileFromClassPath(service, path);
}
return content != null ? streamToString(content) : null;
} finally {
IOUtils.closeQuietly(content);
}
}
use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.
the class FrontendUtils method getStatsAssetsByChunkName.
/**
* Load the asset chunks from <code>stats.json</code>. We will only read the
* file until we have reached the assetsByChunkName json and return that as
* a json object string.
*
* Note: The <code>stats.json</code> is cached when external stats is
* enabled or <code>stats.json</code> is provided from the class path. To
* clear the cache use {@link #clearCachedStatsContent(VaadinService)}.
*
* @param service
* the Vaadin service.
* @return json for assetsByChunkName object in stats.json or {@code null}
* if stats.json not found or content not found.
* @throws IOException
* if an I/O error occurs while creating the input stream.
*/
public static String getStatsAssetsByChunkName(VaadinService service) throws IOException {
DeploymentConfiguration config = service.getDeploymentConfiguration();
Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
return streamToString(getResourceFromWebpack(devModeHandler.get(), "/assetsByChunkName", "getting assets by chunk name."));
}
InputStream resourceAsStream;
if (config.isStatsExternal()) {
resourceAsStream = getStatsFromExternalUrl(config.getExternalStatsUrl(), service.getContext());
} else {
resourceAsStream = getStatsFromClassPath(service);
}
if (resourceAsStream == null) {
return null;
}
try (Scanner scan = new Scanner(resourceAsStream, StandardCharsets.UTF_8.name())) {
StringBuilder assets = new StringBuilder();
assets.append("{");
// Scan until we reach the assetsByChunkName object line
scanToAssetChunkStart(scan, assets);
// Add lines until we reach the first } breaking the object
while (scan.hasNextLine()) {
String line = scan.nextLine().trim();
if ("}".equals(line) || "},".equals(line)) {
// Encountering } or }, means end of asset chunk
return assets.append("}").toString();
} else if (line.endsWith("}") || line.endsWith("},")) {
return assets.append(line.substring(0, line.indexOf('}')).trim()).append("}").toString();
} else if (line.contains("{")) {
// should only contain key-value pairs.
break;
}
assets.append(line);
}
getLogger().error("Could not parse assetsByChunkName from stats.json");
}
return null;
}
Aggregations