diff --git a/flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java index b9d736bf76a..52e9dde664a 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java @@ -720,57 +720,6 @@ private List setupDocumentHead(Element head, dependenciesToProcessOnServer); } - /** - * Generates the initial UIDL message which is included in the initial - * bootstrap page. - * - * @param ui - * the UI for which the UIDL should be generated - * @return a JSON object with the initial UIDL message - */ - protected JsonObject getInitialUidl(UI ui) { - JsonObject json = new UidlWriter().createUidl(ui, false); - - VaadinSession session = ui.getSession(); - if (session.getConfiguration().isXsrfProtectionEnabled()) { - writeSecurityKeyUIDL(json, ui); - } - writePushIdUIDL(json, session); - if (getLogger().isDebugEnabled()) { - getLogger().debug("Initial UIDL: {}", json.asString()); - } - return json; - } - - /** - * Writes the push id (and generates one if needed) to the given JSON - * object. - * - * @param response - * the response JSON object to write security key into - * @param session - * the vaadin session to which the security key belongs - */ - private void writePushIdUIDL(JsonObject response, - VaadinSession session) { - String pushId = session.getPushId(); - response.put(ApplicationConstants.UIDL_PUSH_ID, pushId); - } - - /** - * Writes the security key (and generates one if needed) to the given - * JSON object. - * - * @param response - * the response JSON object to write security key into - * @param ui - * the UI to which the security key belongs - */ - private void writeSecurityKeyUIDL(JsonObject response, UI ui) { - String seckey = ui.getCsrfToken(); - response.put(ApplicationConstants.UIDL_SECURITY_TOKEN_ID, seckey); - } - private List applyUserDependencies(Element head, BootstrapContext context, Map dependenciesToProcessOnServer) { @@ -1532,4 +1481,57 @@ private static String readClientEngine() { } } + + + /** + * Generates the initial UIDL message which is included in the initial + * bootstrap page. + * + * @param ui + * the UI for which the UIDL should be generated + * @return a JSON object with the initial UIDL message + */ + protected static JsonObject getInitialUidl(UI ui) { + JsonObject json = new UidlWriter().createUidl(ui, false); + + VaadinSession session = ui.getSession(); + if (session.getConfiguration().isXsrfProtectionEnabled()) { + writeSecurityKeyUIDL(json, ui); + } + writePushIdUIDL(json, session); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Initial UIDL: {}", json.asString()); + } + return json; + } + + + /** + * Writes the push id (and generates one if needed) to the given JSON + * object. + * + * @param response + * the response JSON object to write security key into + * @param session + * the vaadin session to which the security key belongs + */ + private static void writePushIdUIDL(JsonObject response, + VaadinSession session) { + String pushId = session.getPushId(); + response.put(ApplicationConstants.UIDL_PUSH_ID, pushId); + } + + /** + * Writes the security key (and generates one if needed) to the given + * JSON object. + * + * @param response + * the response JSON object to write security key into + * @param ui + * the UI to which the security key belongs + */ + private static void writeSecurityKeyUIDL(JsonObject response, UI ui) { + String seckey = ui.getCsrfToken(); + response.put(ApplicationConstants.UIDL_SECURITY_TOKEN_ID, seckey); + } } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/JsInitHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/JsInitHandler.java index 2610430947b..4638c6b4f75 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/JsInitHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/JsInitHandler.java @@ -24,8 +24,6 @@ import java.util.function.Function; import org.jsoup.nodes.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.vaadin.flow.component.ClientCallable; import com.vaadin.flow.component.Component; @@ -67,7 +65,7 @@ public class JsInitHandler extends BootstrapHandler { /** - * Custom BootstrapContext for JsInitHandler + * Custom BootstrapContext for JsInitHandler. */ private static class JsInitBootstrapContext extends BootstrapContext { private JsInitBootstrapContext(VaadinRequest request, @@ -83,16 +81,11 @@ protected Optional getTheme() { } /** - * Custom UI for JsInitHandler + * Custom UI for JsInitHandler. */ public static class JsInitUI extends UI { public static final String NO_NAVIGATION = "Navigation is not implemented yet"; - @Override - public void doInit(VaadinRequest request, int uiId) { - super.doInit(request, uiId); - } - /** * Connect a client side with server side UI. * @@ -136,17 +129,14 @@ public void navigate(String location, QueryParameters queryParameters) { * Creates a new bootstrap handler with default page builder. */ public JsInitHandler() { - super(new PageBuilder() { - public Document getBootstrapPage(BootstrapContext context) { - return null; - } - }); + super(context -> null); } @Override protected boolean canHandleRequest(VaadinRequest request) { - return ServletHelper.isRequestType(request, RequestType.INIT); + return !VaadinSession.getCurrent().getService().getDeploymentConfiguration().isCompatibilityMode() + && ServletHelper.isRequestType(request, RequestType.INIT); } protected String getRequestUrl(VaadinRequest request) { @@ -174,7 +164,7 @@ protected BootstrapContext createAndInitUI( pushURL = uri.resolve(new URI(pushURL)).toASCIIString(); } catch (URISyntaxException exception) { throw new IllegalStateException(String.format( - "Can't resolve pushURL '%s'", + "Can't resolve pushURL '%s' based on the service URL '%s'", pushURL, serviceUrl), exception); } } @@ -199,38 +189,33 @@ protected BootstrapContext createBootstrapContext(VaadinRequest request, @Override public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { - if (session.getService().getDeploymentConfiguration().isCompatibilityMode()) { - return super.synchronizedHandleRequest(session, request, response); - } else { - // Find UI class - Class uiClass = getUIClass(request); + Class uiClass = getUIClass(request); - BootstrapContext context = createAndInitUI(uiClass, request, response, - session); + BootstrapContext context = createAndInitUI(uiClass, request, response, + session); - ServletHelper.setResponseNoCacheHeaders(response::setHeader, - response::setDateHeader); + ServletHelper.setResponseNoCacheHeaders(response::setHeader, + response::setDateHeader); - JsonObject json = Json.createObject(); + JsonObject json = Json.createObject(); - DeploymentConfiguration config = context.getSession() - .getConfiguration(); + DeploymentConfiguration config = context.getSession() + .getConfiguration(); - if (!config.isProductionMode()) { - json.put("stats", getStats()); - } - json.put("errors", getErrors()); + if (!config.isProductionMode()) { + json.put("stats", getStats()); + } + json.put("errors", getErrors()); - if (context.getPushMode().isEnabled()) { - json.put("pushScript", getPushScript(context)); - } + if (context.getPushMode().isEnabled()) { + json.put("pushScript", getPushScript(context)); + } - JsonObject initialUIDL = getInitialUidl(context.getUI()); - json.put("appConfig", getAppConfig(initialUIDL, context)); + JsonObject initialUIDL = getInitialUidl(context.getUI()); + json.put("appConfig", getAppConfig(initialUIDL, context)); - writeResponse(response, json); - return true; - } + writeResponse(response, json); + return true; } private String getServiceUrl(VaadinRequest request) { @@ -293,31 +278,6 @@ private String getPushScript(BootstrapContext context) { return pushJSPath; } - private JsonObject getInitialUidl(UI ui) { - JsonObject json = new UidlWriter().createUidl(ui, false); - - VaadinSession session = ui.getSession(); - if (session.getConfiguration().isXsrfProtectionEnabled()) { - writeSecurityKeyUIDL(json, ui); - } - writePushIdUIDL(json, session); - if (getLogger().isDebugEnabled()) { - getLogger().debug("Initial UIDL: {}", json.asString()); - } - return json; - } - - private void writePushIdUIDL(JsonObject response, - VaadinSession session) { - String pushId = session.getPushId(); - response.put(ApplicationConstants.UIDL_PUSH_ID, pushId); - } - - private void writeSecurityKeyUIDL(JsonObject response, UI ui) { - String seckey = ui.getCsrfToken(); - response.put(ApplicationConstants.UIDL_SECURITY_TOKEN_ID, seckey); - } - private JsonObject getAppConfig(JsonValue initialUIDL, BootstrapContext context) { @@ -335,10 +295,6 @@ private JsonObject getAppConfig(JsonValue initialUIDL, private void writeResponse(VaadinResponse response, JsonObject json) throws IOException { response.setContentType("application/json"); response.setStatus(HttpURLConnection.HTTP_OK); - response.getOutputStream().write(JsonUtil.stringify(json).getBytes()); - } - - private Logger getLogger() { - return LoggerFactory.getLogger(this.getClass().getName()); + response.getOutputStream().write(JsonUtil.stringify(json).getBytes("UTF-8")); } } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java index c4039e790dc..518c01b2a6a 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java @@ -16,12 +16,15 @@ package com.vaadin.flow.server; import javax.servlet.ServletException; - import java.util.Collections; import java.util.List; import com.vaadin.flow.di.Instantiator; import com.vaadin.flow.function.DeploymentConfiguration; +import com.vaadin.flow.server.RequestHandler; +import com.vaadin.flow.server.ServiceException; +import com.vaadin.flow.server.VaadinServlet; +import com.vaadin.flow.server.VaadinServletService; import com.vaadin.tests.util.MockDeploymentConfiguration; /** diff --git a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinSession.java b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinSession.java index 03adb02176d..5fd3ebf9785 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinSession.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/MockVaadinSession.java @@ -18,6 +18,9 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import com.vaadin.flow.server.VaadinService; +import com.vaadin.flow.server.VaadinSession; + /** * * @author Vaadin Ltd