Skip to content

Commit

Permalink
Deprecate ServletHelper, introduce HandlerHelper instead (#7003)
Browse files Browse the repository at this point in the history
Fixes #5565
  • Loading branch information
Denis authored Nov 27, 2019
1 parent b496a60 commit 3c9e908
Show file tree
Hide file tree
Showing 17 changed files with 497 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public boolean synchronizedHandleRequest(VaadinSession session,
BootstrapContext context = createAndInitUI(uiClass, request, response,
session);

ServletHelper.setResponseNoCacheHeaders(response::setHeader,
HandlerHelper.setResponseNoCacheHeaders(response::setHeader,
response::setDateHeader);

Document document = pageBuilder.getBootstrapPage(context);
Expand Down Expand Up @@ -1319,7 +1319,7 @@ private JsonObject getApplicationParameters(VaadinRequest request,
}

// Use locale from session if set, else from the request
Locale locale = ServletHelper.findLocale(session, request);
Locale locale = HandlerHelper.findLocale(session, request);
// Get system messages
SystemMessages systemMessages = session.getService()
.getSystemMessages(locale, request);
Expand Down Expand Up @@ -1388,7 +1388,7 @@ protected static String getServiceUrl(BootstrapContext context) {
* path segment in pathInfo (i.e. the part of the requested path
* that comes after the servlet mapping)
*/
return ServletHelper.getCancelingRelativePath(pathInfo);
return HandlerHelper.getCancelingRelativePath(pathInfo);
}
}

Expand Down
174 changes: 174 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/server/HandlerHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2000-2018 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.server;

import java.io.Serializable;
import java.util.Locale;
import java.util.function.BiConsumer;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.shared.ApplicationConstants;

/**
* Contains helper methods for {@link VaadinServlet} and generally for handling
* {@link VaadinRequest VaadinRequests}.
*
* @since 1.0
*/
public class HandlerHelper implements Serializable {

/**
* The default SystemMessages (read-only).
*/
static final SystemMessages DEFAULT_SYSTEM_MESSAGES = new SystemMessages();

/**
* Framework internal enum for tracking the type of a request.
*/
public enum RequestType {

/**
* UIDL requests.
*/
UIDL(ApplicationConstants.REQUEST_TYPE_UIDL),
/**
* Heartbeat requests.
*/
HEARTBEAT(ApplicationConstants.REQUEST_TYPE_HEARTBEAT),
/**
* Push requests (any transport).
*/
PUSH(ApplicationConstants.REQUEST_TYPE_PUSH);

private String identifier;

private RequestType(String identifier) {
this.identifier = identifier;
}

/**
* Returns the identifier for the request type.
*
* @return the identifier
*/
public String getIdentifier() {
return identifier;
}
}

private HandlerHelper() {
// Only utility methods
}

/**
* Returns whether the given request is of the given type.
*
* @param request
* the request to check
* @param requestType
* the type to check for
* @return <code>true</code> if the request is of the given type,
* <code>false</code> otherwise
*/
public static boolean isRequestType(VaadinRequest request,
RequestType requestType) {
return requestType.getIdentifier().equals(request
.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER));
}

/**
* Helper to find the most most suitable Locale. These potential sources are
* checked in order until a Locale is found:
* <ol>
* <li>The passed component (or UI) if not null</li>
* <li>{@link UI#getCurrent()} if defined</li>
* <li>The passed session if not null</li>
* <li>{@link VaadinSession#getCurrent()} if defined</li>
* <li>The passed request if not null</li>
* <li>{@link VaadinService#getCurrentRequest()} if defined</li>
* <li>{@link Locale#getDefault()}</li>
* </ol>
*
* @param session
* the session that is searched for locale or <code>null</code>
* if not available
* @param request
* the request that is searched for locale or <code>null</code>
* if not available
* @return the found locale
*/
public static Locale findLocale(VaadinSession session,
VaadinRequest request) {

if (session == null) {
session = VaadinSession.getCurrent();
}
if (session != null) {
Locale locale = session.getLocale();
if (locale != null) {
return locale;
}
}

if (request == null) {
request = VaadinService.getCurrentRequest();
}
if (request != null) {
Locale locale = request.getLocale();
if (locale != null) {
return locale;
}
}

return Locale.getDefault();
}

/**
* Sets no cache headers to the specified response.
*
* @param headerSetter
* setter for string value headers
* @param longHeaderSetter
* setter for long value headers
*/
public static void setResponseNoCacheHeaders(
BiConsumer<String, String> headerSetter,
BiConsumer<String, Long> longHeaderSetter) {
headerSetter.accept("Cache-Control", "no-cache, no-store");
headerSetter.accept("Pragma", "no-cache");
longHeaderSetter.accept("Expires", 0L);
}

/**
* Gets a relative path that cancels the provided path. This essentially
* adds one .. for each part of the path to cancel.
*
* @param pathToCancel
* the path that should be canceled
* @return a relative path that cancels out the provided path segment
*/
public static String getCancelingRelativePath(String pathToCancel) {
StringBuilder sb = new StringBuilder(".");
// Start from i = 1 to ignore first slash
for (int i = 1; i < pathToCancel.length(); i++) {
if (pathToCancel.charAt(i) == '/') {
sb.append("/..");
}
}
return sb.toString();
}

}
68 changes: 31 additions & 37 deletions flow-server/src/main/java/com/vaadin/flow/server/ServletHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
*/
package com.vaadin.flow.server;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.shared.ApplicationConstants;

import java.io.Serializable;
import java.util.Locale;
import java.util.function.BiConsumer;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.shared.ApplicationConstants;

/**
* Contains helper methods for {@link VaadinServlet} and generally for handling
* {@link VaadinRequest VaadinRequests}.
*
* @since 1.0
* @deprecated Use {@link HandlerHelper} instead
*
* @deprecated Use {@link HandlerHelper} instead
*/
@Deprecated
public class ServletHelper implements Serializable {

/**
Expand Down Expand Up @@ -82,7 +86,11 @@ private ServletHelper() {
* the type to check for
* @return <code>true</code> if the request is of the given type,
* <code>false</code> otherwise
* @deprecated Use
* {@link HandlerHelper#isRequestType(VaadinRequest, com.vaadin.flow.server.HandlerHelper.RequestType)}
* instead
*/
@Deprecated
public static boolean isRequestType(VaadinRequest request,
RequestType requestType) {
return requestType.getIdentifier().equals(request
Expand All @@ -109,31 +117,14 @@ public static boolean isRequestType(VaadinRequest request,
* the request that is searched for locale or <code>null</code>
* if not available
* @return the found locale
* @deprecated Use
* {@link HandlerHelper#findLocale(VaadinSession, VaadinRequest)}
* instead
*/
@Deprecated
public static Locale findLocale(VaadinSession session,
VaadinRequest request) {

if (session == null) {
session = VaadinSession.getCurrent();
}
if (session != null) {
Locale locale = session.getLocale();
if (locale != null) {
return locale;
}
}

if (request == null) {
request = VaadinService.getCurrentRequest();
}
if (request != null) {
Locale locale = request.getLocale();
if (locale != null) {
return locale;
}
}

return Locale.getDefault();
return HandlerHelper.findLocale(session, request);
}

/**
Expand All @@ -143,13 +134,15 @@ public static Locale findLocale(VaadinSession session,
* setter for string value headers
* @param longHeaderSetter
* setter for long value headers
* @deprecated Use
* {@link HandlerHelper#setResponseNoCacheHeaders(BiConsumer, BiConsumer)}
* instead
*/
@Deprecated
public static void setResponseNoCacheHeaders(
BiConsumer<String, String> headerSetter,
BiConsumer<String, Long> longHeaderSetter) {
headerSetter.accept("Cache-Control", "no-cache, no-store");
headerSetter.accept("Pragma", "no-cache");
longHeaderSetter.accept("Expires", 0L);
HandlerHelper.setResponseNoCacheHeaders(headerSetter, longHeaderSetter);
}

/**
Expand All @@ -159,16 +152,13 @@ public static void setResponseNoCacheHeaders(
* @param pathToCancel
* the path that should be canceled
* @return a relative path that cancels out the provided path segment
* @deprecated Use {@link HandlerHelper#getCancelingRelativePath(String)}
* instead
*
*/
@Deprecated
public static String getCancelingRelativePath(String pathToCancel) {
StringBuilder sb = new StringBuilder(".");
// Start from i = 1 to ignore first slash
for (int i = 1; i < pathToCancel.length(); i++) {
if (pathToCancel.charAt(i) == '/') {
sb.append("/..");
}
}
return sb.toString();
return HandlerHelper.getCancelingRelativePath(pathToCancel);
}

/**
Expand All @@ -177,8 +167,12 @@ public static String getCancelingRelativePath(String pathToCancel) {
* @param request
* the request for which the location should be determined
* @return A relative path to the context root. Never ends with a slash (/).
* @deprecated Don't use this method since it's tied to
* {@link VaadinServletRequest}
*/
public static String getContextRootRelativePath(VaadinServletRequest request) {
@Deprecated
public static String getContextRootRelativePath(
VaadinServletRequest request) {
// Generate location from the request by finding how many "../" should
// be added to the servlet path before we get to the context root

Expand Down
Loading

0 comments on commit 3c9e908

Please sign in to comment.