Skip to content

Commit

Permalink
CCDM: Applying reviewer suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
manolo committed Aug 6, 2019
1 parent 96eaa19 commit 79cfc59
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 125 deletions.
104 changes: 53 additions & 51 deletions flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,57 +720,6 @@ private List<Element> 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<Element> applyUserDependencies(Element head,
BootstrapContext context,
Map<LoadMode, JsonArray> dependenciesToProcessOnServer) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
import java.util.Optional;
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;
import com.vaadin.flow.component.PushConfiguration;
Expand Down Expand Up @@ -67,7 +63,7 @@
public class JsInitHandler extends BootstrapHandler {

/**
* Custom BootstrapContext for JsInitHandler
* Custom BootstrapContext for JsInitHandler.
*/
private static class JsInitBootstrapContext extends BootstrapContext {
private JsInitBootstrapContext(VaadinRequest request,
Expand All @@ -83,16 +79,11 @@ protected Optional<ThemeDefinition> 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.
*
Expand Down Expand Up @@ -136,17 +127,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 !request.getService().getDeploymentConfiguration().isCompatibilityMode()
&& ServletHelper.isRequestType(request, RequestType.INIT);
}

protected String getRequestUrl(VaadinRequest request) {
Expand Down Expand Up @@ -174,7 +162,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);
}
}
Expand All @@ -183,7 +171,11 @@ protected BootstrapContext createAndInitUI(
pushConfiguration.setPushUrl(pushURL);

config.put(ApplicationConstants.SERVICE_URL, serviceUrl);
// TODO(manolo) revise this

// TODO(manolo) this comment is left intentionally because we
// need to revise whether the info passed to client is valid
// when initialising push. Right now ccdm is not doing
// anything with push.
config.put("pushScript", getPushScript(context));
config.put("requestURL", requestURL);

Expand All @@ -199,38 +191,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<? extends UI> uiClass = getUIClass(request);
Class<? extends UI> 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) {
Expand Down Expand Up @@ -293,31 +280,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) {

Expand All @@ -335,10 +297,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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79cfc59

Please sign in to comment.