Skip to content

Commit

Permalink
feat: use superclass of VadinServlet/Request if possible (#9699)
Browse files Browse the repository at this point in the history
Makes it possible to use another VaadinService implementations than the built-in VaadinServletService.
  • Loading branch information
stbischof committed Mar 4, 2021
1 parent 0ecdf5f commit c581668
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class StaticFileServer implements StaticFileHandler {
.compile("^/frontend[-\\w/]*/webjars/");

private final ResponseWriter responseWriter;
private final VaadinServletService servletService;
private final VaadinService vaadinService;
private DeploymentConfiguration deploymentConfiguration;
private final List<String> manifestPaths;

Expand All @@ -68,12 +68,12 @@ public class StaticFileServer implements StaticFileHandler {
/**
* Constructs a file server.
*
* @param servletService
* servlet service for the deployment, not <code>null</code>
* @param vaadinService
* vaadin service for the deployment, not <code>null</code>
*/
public StaticFileServer(VaadinServletService servletService) {
this.servletService = servletService;
deploymentConfiguration = servletService.getDeploymentConfiguration();
public StaticFileServer(VaadinService vaadinService) {
this.vaadinService = vaadinService;
deploymentConfiguration = vaadinService.getDeploymentConfiguration();
responseWriter = new ResponseWriter(deploymentConfiguration);
manifestPaths = getManifestPathsFromJson();
}
Expand Down Expand Up @@ -103,8 +103,8 @@ public boolean isStaticResourceRequest(HttpServletRequest request) {
// resource as well.
return true;
}
resource = vaadinService.getStaticResource(requestFilename);

resource = servletService.getStaticResource(requestFilename);

if (resource == null && shouldFixIncorrectWebjarPaths()
&& isIncorrectWebjarPath(requestFilename)) {
Expand All @@ -131,23 +131,24 @@ public boolean serveStaticResource(HttpServletRequest request,
if (isAllowedVAADINBuildOrStaticUrl(filenameWithPath)
|| manifestPaths.contains(filenameWithPath)) {
if(APP_THEME_PATTERN.matcher(filenameWithPath).find()) {
resourceUrl = servletService.getClassLoader()
resourceUrl = vaadinService.getClassLoader()
.getResource(VAADIN_WEBAPP_RESOURCES + "VAADIN/static/"
+ filenameWithPath.replaceFirst("^/", ""));

} else {
resourceUrl = servletService.getClassLoader()
resourceUrl = vaadinService.getClassLoader()
.getResource(VAADIN_WEBAPP_RESOURCES
+ filenameWithPath.replaceFirst("^/", ""));

}
}
if (resourceUrl == null) {
resourceUrl = servletService.getStaticResource(filenameWithPath);
resourceUrl = vaadinService.getStaticResource(filenameWithPath);
}
if (resourceUrl == null && shouldFixIncorrectWebjarPaths()
&& isIncorrectWebjarPath(filenameWithPath)) {
// Flow issue #4601
resourceUrl = servletService.getStaticResource(
resourceUrl = vaadinService.getStaticResource(
fixIncorrectWebjarPath(filenameWithPath));
}

Expand Down Expand Up @@ -409,7 +410,7 @@ protected boolean browserHasNewestVersion(HttpServletRequest request,
* @return list of paths mapped to static webapp resources, or empty list
*/
private List<String> getManifestPathsFromJson() {
InputStream stream = servletService.getClassLoader()
InputStream stream = vaadinService.getClassLoader()
.getResourceAsStream(
VAADIN_WEBAPP_RESOURCES + "manifest.json");
if (stream == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1693,10 +1693,16 @@ protected void handleSessionExpired(VaadinRequest request,
SystemMessages systemMessages = getSystemMessages(
HandlerHelper.findLocale(null, request), request);
String sessionExpiredURL = systemMessages.getSessionExpiredURL();


if (sessionExpiredURL != null
&& (response instanceof VaadinServletResponse)) {
((VaadinServletResponse) response)
.sendRedirect(sessionExpiredURL);
} else if (sessionExpiredURL != null
&& (response instanceof HttpServletResponse)) {
((HttpServletResponse) response)
.sendRedirect(sessionExpiredURL);
} else {
/*
* Session expired as a result of a standard http request and we
Expand Down
44 changes: 22 additions & 22 deletions flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* @since 1.0
*/
public class VaadinServlet extends HttpServlet {
private VaadinServletService servletService;
private VaadinService vaadinService;
private StaticFileHandler staticFileHandler;

private volatile boolean isServletInitialized;
Expand Down Expand Up @@ -89,12 +89,12 @@ public void init(ServletConfig servletConfig) throws ServletException {
* its "init" method is called from the {@code
* ServletContextListener} with the same ServletConfig instance.
*/
VaadinServletContext vaadinServletContext = null;
VaadinContext vaadinContext = null;
if (getServletConfig() == null) {
isServletInitialized = true;
super.init(servletConfig);

vaadinServletContext = initializeContext();
vaadinContext = initializeContext();
}

if (getServletConfig() != servletConfig) {
Expand All @@ -103,28 +103,28 @@ public void init(ServletConfig servletConfig) throws ServletException {
+ "instance which has been used for the initial method call");
}

if (vaadinServletContext == null) {
vaadinServletContext = new VaadinServletContext(
if (vaadinContext == null) {
vaadinContext = new VaadinServletContext(
getServletConfig().getServletContext());
}

if (servletService != null || vaadinServletContext
if (vaadinService != null || vaadinContext
.getAttribute(Lookup.class) == null) {
return;
}

try {
servletService = createServletService();
vaadinService = createServletService();
} catch (ServiceException e) {
throw new ServletException("Could not initialize VaadinServlet",
e);
}

// Sets current service as it is needed in static file server even
// though there are no request and response.
servletService.setCurrentInstances(null, null);
vaadinService.setCurrentInstances(null, null);

staticFileHandler = createStaticFileHandler(servletService);
staticFileHandler = createStaticFileHandler(vaadinService);

servletInitialized();
} finally {
Expand All @@ -145,13 +145,13 @@ public ServletConfig getServletConfig() {
* to find and serve static resources. By default it returns a
* {@link StaticFileServer} instance.
*
* @param servletService
* the servlet service created at {@link #createServletService()}
* @param vaadinService
* the vaadinService created at {@link #createServletService()}
* @return the file server to be used by this servlet, not <code>null</code>
*/
protected StaticFileHandler createStaticFileHandler(
VaadinServletService servletService) {
return new StaticFileServer(servletService);
VaadinService vaadinService) {
return new StaticFileServer(vaadinService);
}

protected void servletInitialized() throws ServletException {
Expand Down Expand Up @@ -287,8 +287,8 @@ protected void service(HttpServletRequest request,

CurrentInstance.clearAll();

VaadinServletRequest vaadinRequest = createVaadinRequest(request);
VaadinServletResponse vaadinResponse = createVaadinResponse(response);
VaadinRequest vaadinRequest = createVaadinRequest(request);
VaadinResponse vaadinResponse = createVaadinResponse(response);
if (!ensureCookiesEnabled(vaadinRequest, vaadinResponse)) {
return;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ protected static String getLastPathParameter(String uri) {
}
}

private VaadinServletResponse createVaadinResponse(
private VaadinResponse createVaadinResponse(
HttpServletResponse response) {
return new VaadinServletResponse(response, getService());
}
Expand All @@ -442,8 +442,8 @@ protected VaadinServletRequest createVaadinRequest(
*
* @return the Vaadin service
*/
public VaadinServletService getService() {
return servletService;
public VaadinService getService() {
return vaadinService;
}

/**
Expand All @@ -457,13 +457,13 @@ public VaadinServletService getService() {
* @return false if cookies are disabled, true otherwise
* @throws IOException
*/
private boolean ensureCookiesEnabled(VaadinServletRequest request,
VaadinServletResponse response) throws IOException {
private boolean ensureCookiesEnabled(VaadinRequest request,
VaadinResponse response) throws IOException {
if (HandlerHelper.isRequestType(request, RequestType.UIDL)) {
// In all other but the first UIDL request a cookie should be
// returned by the browser.
// This can be removed if cookieless mode (#3228) is supported
if (request.getRequestedSessionId() == null) {
if (request instanceof HttpServletRequest && ((HttpServletRequest) request).getRequestedSessionId() == null) {
// User has cookies disabled
SystemMessages systemMessages = getService().getSystemMessages(
HandlerHelper.findLocale(null, request), request);
Expand Down Expand Up @@ -535,7 +535,7 @@ public void destroy() {
}
}

private VaadinServletContext initializeContext() {
private VaadinContext initializeContext() {
ServletContext servletContext = getServletConfig().getServletContext();
VaadinServletContext vaadinServletContext = new VaadinServletContext(
servletContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class VaadinServletRequest extends HttpServletRequestWrapper
implements VaadinRequest {

private final VaadinServletService vaadinService;
private final VaadinService vaadinService;

/**
* Wraps a http servlet request and associates with a vaadin service.
Expand All @@ -43,7 +43,7 @@ public class VaadinServletRequest extends HttpServletRequestWrapper
* the associated vaadin service
*/
public VaadinServletRequest(HttpServletRequest request,
VaadinServletService vaadinService) {
VaadinService vaadinService) {
super(request);
this.vaadinService = vaadinService;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public HttpServletRequest getHttpServletRequest() {
}

@Override
public VaadinServletService getService() {
public VaadinService getService() {
return vaadinService;
}

Expand All @@ -91,8 +91,7 @@ public static VaadinServletRequest getCurrent() {
VaadinRequest currentRequest = VaadinRequest.getCurrent();
if (currentRequest instanceof VaadinServletRequest) {
return (VaadinServletRequest) currentRequest;
} else {
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class VaadinServletResponse extends HttpServletResponseWrapper
implements VaadinResponse {

private VaadinServletService vaadinService;
private VaadinService vaadinService;

/**
* Wraps a http servlet response and an associated vaadin service.
Expand All @@ -42,7 +42,7 @@ public class VaadinServletResponse extends HttpServletResponseWrapper
* the associated vaadin service
*/
public VaadinServletResponse(HttpServletResponse response,
VaadinServletService vaadinService) {
VaadinService vaadinService) {
super(response);
this.vaadinService = vaadinService;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ private static void doSetCacheTime(VaadinResponse response,
}

@Override
public VaadinServletService getService() {
public VaadinService getService() {
return vaadinService;
}

Expand All @@ -95,8 +95,7 @@ public static VaadinServletResponse getCurrent() {
VaadinResponse currentResponse = VaadinResponse.getCurrent();
if (currentResponse instanceof VaadinServletResponse) {
return (VaadinServletResponse) currentResponse;
} else {
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ private InputStream getResourceInServletContextAsStream(String path) {

@Override
public String getContextRootRelativePath(VaadinRequest request) {
assert request instanceof VaadinServletRequest;
assert request instanceof HttpServletRequest;
// Generate location from the request by finding how many "../" should
// be added to the servlet path before we get to the context root

// Should not take pathinfo into account because the base URI refers to
// the servlet path

String servletPath = ((VaadinServletRequest) request).getServletPath();
String servletPath = ((HttpServletRequest) request).getServletPath();
assert servletPath != null;
if (!servletPath.endsWith("/")) {
servletPath += "/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.flow.server.RequestHandler;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinResponse;
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.server.VaadinSession;

/**
Expand All @@ -39,7 +39,9 @@ public class FaviconHandler implements RequestHandler {
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request,
VaadinResponse response) throws IOException {
VaadinServletRequest httpRequest = (VaadinServletRequest) request;
//please be careful with changing HttpServletRequest.
//may Implementations VaadinService just deliver HttpServletRequest.
HttpServletRequest httpRequest = (HttpServletRequest) request;
boolean isFavicon = httpRequest.getContextPath().isEmpty()
&& httpRequest.getServletPath().isEmpty()
&& "/favicon.ico".equals(httpRequest.getPathInfo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@

package com.vaadin.flow.server.communication;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;

import com.vaadin.flow.component.PushConfiguration;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.internal.JavaScriptBootstrapUI;
import com.vaadin.flow.internal.BootstrapHandlerHelper;
import com.vaadin.flow.internal.UsageStatistics;
import com.vaadin.flow.router.Location;
import com.vaadin.flow.server.AppShellRegistry;
import com.vaadin.flow.server.BootstrapHandler;
import com.vaadin.flow.server.DevModeHandler;
import com.vaadin.flow.server.HandlerHelper;
import com.vaadin.flow.server.HandlerHelper.RequestType;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinResponse;
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.AppShellRegistry;
import com.vaadin.flow.shared.ApplicationConstants;

import elemental.json.Json;
Expand Down Expand Up @@ -83,7 +85,7 @@ protected boolean canHandleRequest(VaadinRequest request) {
}

protected String getRequestUrl(VaadinRequest request) {
return ((VaadinServletRequest) request).getRequestURL().toString();
return ((HttpServletRequest) request).getRequestURL().toString();
}

@Override
Expand Down

0 comments on commit c581668

Please sign in to comment.