Skip to content

Commit

Permalink
refactor!: revert "feat: use superclass of VaadinServlet/Request (#9699
Browse files Browse the repository at this point in the history
…)" (#10812)

This reverts commit c581668, except
intentionally leaves out part of the protected API introduced to StaticFileServer to get a StaticFileServer
resource, so it may be overridden to use something else than VaadinServletService.
  • Loading branch information
pleku committed Apr 27, 2021
1 parent e33ec10 commit 9f903f8
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,15 @@ public boolean serveStaticResource(HttpServletRequest request,
URL resourceUrl = null;
if (isAllowedVAADINBuildOrStaticUrl(filenameWithPath)
|| manifestPaths.contains(filenameWithPath)) {
if (APP_THEME_PATTERN.matcher(filenameWithPath).find()) {
if(APP_THEME_PATTERN.matcher(filenameWithPath).find()) {
resourceUrl = vaadinService.getClassLoader()
.getResource(VAADIN_WEBAPP_RESOURCES + "VAADIN/static/"
+ filenameWithPath.replaceFirst("^/", ""));

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

.getResource(VAADIN_WEBAPP_RESOURCES
+ filenameWithPath.replaceFirst("^/", ""));
}
}
if (resourceUrl == null) {
Expand All @@ -149,8 +148,7 @@ public boolean serveStaticResource(HttpServletRequest request,
if (resourceUrl == null && shouldFixIncorrectWebjarPaths()
&& isIncorrectWebjarPath(filenameWithPath)) {
// Flow issue #4601
resourceUrl = getStaticResource(
fixIncorrectWebjarPath(filenameWithPath));
resourceUrl = getStaticResource(fixIncorrectWebjarPath(filenameWithPath));
}

if (resourceUrl == null) {
Expand Down Expand Up @@ -433,7 +431,8 @@ protected boolean browserHasNewestVersion(HttpServletRequest request,
*/
private List<String> getManifestPathsFromJson() {
InputStream stream = vaadinService.getClassLoader()
.getResourceAsStream(VAADIN_WEBAPP_RESOURCES + "manifest.json");
.getResourceAsStream(
VAADIN_WEBAPP_RESOURCES + "manifest.json");
if (stream == null) {
// manifest.json resource does not exist, probably dev mode
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1693,15 +1693,10 @@ 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
49 changes: 24 additions & 25 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 VaadinService vaadinService;
private VaadinServletService servletService;
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.
*/
VaadinContext vaadinContext = null;
VaadinServletContext vaadinServletContext = null;
if (getServletConfig() == null) {
isServletInitialized = true;
super.init(servletConfig);

vaadinContext = initializeContext();
vaadinServletContext = 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 (vaadinContext == null) {
vaadinContext = new VaadinServletContext(
if (vaadinServletContext == null) {
vaadinServletContext = new VaadinServletContext(
getServletConfig().getServletContext());
}

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

try {
vaadinService = createServletService();
servletService = 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.
vaadinService.setCurrentInstances(null, null);
servletService.setCurrentInstances(null, null);

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

servletInitialized();
} finally {
Expand All @@ -142,12 +142,12 @@ public ServletConfig getServletConfig() {

/**
* Creates a new instance of {@link StaticFileHandler}, that is responsible
* to find and serve static resources.
*
* to find and serve static resources. By default it returns a
* {@link StaticFileServer} instance.
*
* @param vaadinService
* the vaadinService created at {@link #createServletService()}
* @return the file handler to be used by this servlet, not
* <code>null</code>
* @return the file server to be used by this servlet, not <code>null</code>
*/
protected StaticFileHandler createStaticFileHandler(
VaadinService vaadinService) {
Expand Down Expand Up @@ -289,8 +289,8 @@ protected void service(HttpServletRequest request,

CurrentInstance.clearAll();

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

private VaadinResponse createVaadinResponse(HttpServletResponse response) {
private VaadinServletResponse createVaadinResponse(
HttpServletResponse response) {
return new VaadinServletResponse(response, getService());
}

Expand All @@ -443,8 +444,8 @@ protected VaadinServletRequest createVaadinRequest(
*
* @return the Vaadin service
*/
public VaadinService getService() {
return vaadinService;
public VaadinServletService getService() {
return servletService;
}

/**
Expand All @@ -458,15 +459,13 @@ public VaadinService getService() {
* @return false if cookies are disabled, true otherwise
* @throws IOException
*/
private boolean ensureCookiesEnabled(VaadinRequest request,
VaadinResponse response) throws IOException {
private boolean ensureCookiesEnabled(VaadinServletRequest request,
VaadinServletResponse 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 instanceof HttpServletRequest
&& ((HttpServletRequest) request)
.getRequestedSessionId() == null) {
if (request.getRequestedSessionId() == null) {
// User has cookies disabled
SystemMessages systemMessages = getService().getSystemMessages(
HandlerHelper.findLocale(null, request), request);
Expand Down Expand Up @@ -538,7 +537,7 @@ public void destroy() {
}
}

private VaadinContext initializeContext() {
private VaadinServletContext 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 VaadinService vaadinService;
private final VaadinServletService 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,
VaadinService vaadinService) {
VaadinServletService vaadinService) {
super(request);
this.vaadinService = vaadinService;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public HttpServletRequest getHttpServletRequest() {
}

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

Expand All @@ -91,7 +91,8 @@ 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 VaadinService vaadinService;
private VaadinServletService 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,
VaadinService vaadinService) {
VaadinServletService vaadinService) {
super(response);
this.vaadinService = vaadinService;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ private static void doSetCacheTime(VaadinResponse response,
}

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

Expand All @@ -95,7 +95,8 @@ 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 HttpServletRequest;
assert request instanceof VaadinServletRequest;
// 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 = ((HttpServletRequest) request).getServletPath();
String servletPath = ((VaadinServletRequest) 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,9 +39,7 @@ public class FaviconHandler implements RequestHandler {
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request,
VaadinResponse response) throws IOException {
// please be careful with changing HttpServletRequest.
// may Implementations VaadinService just deliver HttpServletRequest.
HttpServletRequest httpRequest = (HttpServletRequest) request;
VaadinServletRequest httpRequest = (VaadinServletRequest) 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,34 +16,32 @@

package com.vaadin.flow.server.communication;

import static com.vaadin.flow.component.internal.JavaScriptBootstrapUI.SERVER_ROUTING;

import java.io.IOException;
import java.net.HttpURLConnection;
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;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
import elemental.json.impl.JsonUtil;
import static com.vaadin.flow.component.internal.JavaScriptBootstrapUI.SERVER_ROUTING;

/**
* Processes a 'start' request type from the client to initialize server session
Expand Down Expand Up @@ -110,7 +108,7 @@ private boolean isServletRootRequest(VaadinRequest request) {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.server.VaadinServletService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException;
import com.vaadin.flow.shared.ApplicationConstants;
Expand Down Expand Up @@ -147,15 +148,15 @@ private interface PushEventCallback {
}
};

private VaadinService service;
private VaadinServletService service;

/**
* Creates an instance connected to the given service.
*
* @param service
* the service this handler belongs to
*/
public PushHandler(VaadinService service) {
public PushHandler(VaadinServletService service) {
this.service = service;
}

Expand Down

0 comments on commit 9f903f8

Please sign in to comment.