Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VaadinServletService.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.util.List;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.servlet.http.HttpServletRequest;
  26. import com.vaadin.server.communication.PushRequestHandler;
  27. import com.vaadin.server.communication.ServletBootstrapHandler;
  28. import com.vaadin.server.communication.ServletUIInitHandler;
  29. import com.vaadin.ui.UI;
  30. public class VaadinServletService extends VaadinService {
  31. private final VaadinServlet servlet;
  32. public VaadinServletService(VaadinServlet servlet,
  33. DeploymentConfiguration deploymentConfiguration)
  34. throws ServiceException {
  35. super(deploymentConfiguration);
  36. this.servlet = servlet;
  37. }
  38. @Override
  39. protected List<RequestHandler> createRequestHandlers()
  40. throws ServiceException {
  41. List<RequestHandler> handlers = super.createRequestHandlers();
  42. handlers.add(0, new ServletBootstrapHandler());
  43. handlers.add(new ServletUIInitHandler());
  44. if (isAtmosphereAvailable()) {
  45. try {
  46. handlers.add(new PushRequestHandler(this));
  47. } catch (ServiceException e) {
  48. // Atmosphere init failed. Push won't work but we don't throw a
  49. // service exception as we don't want to prevent non-push
  50. // applications from working
  51. getLogger().log(Level.WARNING,
  52. "Error initializing Atmosphere. Push will not work.",
  53. e);
  54. }
  55. }
  56. return handlers;
  57. }
  58. /**
  59. * Retrieves a reference to the servlet associated with this service.
  60. *
  61. * @return A reference to the VaadinServlet this service is using
  62. */
  63. public VaadinServlet getServlet() {
  64. return servlet;
  65. }
  66. @Override
  67. public String getStaticFileLocation(VaadinRequest request) {
  68. VaadinServletRequest servletRequest = (VaadinServletRequest) request;
  69. String staticFileLocation;
  70. // if property is defined in configurations, use that
  71. staticFileLocation = getDeploymentConfiguration().getResourcesPath();
  72. if (staticFileLocation != null) {
  73. return staticFileLocation;
  74. }
  75. // the last (but most common) option is to generate default location
  76. // from request by finding how many "../" should be added to the
  77. // requested path before we get to the context root
  78. String requestedPath = servletRequest.getServletPath();
  79. String pathInfo = servletRequest.getPathInfo();
  80. if (pathInfo != null) {
  81. requestedPath += pathInfo;
  82. }
  83. return getCancelingRelativePath(requestedPath);
  84. }
  85. /**
  86. * Gets a relative path that cancels the provided path. This essentially
  87. * adds one .. for each part of the path to cancel.
  88. *
  89. * @param pathToCancel
  90. * the path that should be canceled
  91. * @return a relative path that cancels out the provided path segment
  92. */
  93. public static String getCancelingRelativePath(String pathToCancel) {
  94. StringBuilder sb = new StringBuilder(".");
  95. // Start from i = 1 to ignore first slash
  96. for (int i = 1; i < pathToCancel.length(); i++) {
  97. if (pathToCancel.charAt(i) == '/') {
  98. sb.append("/..");
  99. }
  100. }
  101. return sb.toString();
  102. }
  103. /**
  104. * Gets a relative path you can use to refer to the context root.
  105. *
  106. * @param request
  107. * the request for which the location should be determined
  108. * @return A relative path to the context root. Never ends with a slash (/).
  109. *
  110. * @since 8.0.3
  111. */
  112. public static String getContextRootRelativePath(VaadinRequest request) {
  113. VaadinServletRequest servletRequest = (VaadinServletRequest) request;
  114. // Generate location from the request by finding how many "../" should
  115. // be added to the servlet path before we get to the context root
  116. String servletPath = servletRequest.getServletPath();
  117. if (servletPath == null) {
  118. // Not allowed by the spec but servers are servers...
  119. servletPath = "";
  120. }
  121. String pathInfo = servletRequest.getPathInfo();
  122. if (pathInfo != null && !"".equals(pathInfo)) {
  123. servletPath += pathInfo;
  124. }
  125. return getCancelingRelativePath(servletPath);
  126. }
  127. @Override
  128. public String getConfiguredWidgetset(VaadinRequest request) {
  129. return getDeploymentConfiguration()
  130. .getWidgetset(VaadinServlet.DEFAULT_WIDGETSET);
  131. }
  132. @Override
  133. public String getConfiguredTheme(VaadinRequest request) {
  134. // Use the default
  135. return VaadinServlet.getDefaultTheme();
  136. }
  137. @Override
  138. public boolean isStandalone(VaadinRequest request) {
  139. return true;
  140. }
  141. @Override
  142. public String getMimeType(String resourceName) {
  143. return getServlet().getServletContext().getMimeType(resourceName);
  144. }
  145. @Override
  146. public File getBaseDirectory() {
  147. final String realPath = VaadinServlet
  148. .getResourcePath(servlet.getServletContext(), "/");
  149. if (realPath == null) {
  150. return null;
  151. }
  152. return new File(realPath);
  153. }
  154. @Override
  155. protected boolean requestCanCreateSession(VaadinRequest request) {
  156. if (ServletUIInitHandler.isUIInitRequest(request)) {
  157. // This is the first request if you are embedding by writing the
  158. // embedding code yourself
  159. return true;
  160. } else if (isOtherRequest(request)) {
  161. /*
  162. * I.e URIs that are not RPC calls or static (theme) files.
  163. */
  164. return true;
  165. }
  166. return false;
  167. }
  168. private boolean isOtherRequest(VaadinRequest request) {
  169. // TODO This should be refactored in some way. It should not be
  170. // necessary to check all these types.
  171. return (!ServletPortletHelper.isAppRequest(request)
  172. && !ServletUIInitHandler.isUIInitRequest(request)
  173. && !ServletPortletHelper.isFileUploadRequest(request)
  174. && !ServletPortletHelper.isHeartbeatRequest(request)
  175. && !ServletPortletHelper.isPublishedFileRequest(request)
  176. && !ServletPortletHelper.isUIDLRequest(request)
  177. && !ServletPortletHelper.isPushRequest(request));
  178. }
  179. @Override
  180. protected URL getApplicationUrl(VaadinRequest request)
  181. throws MalformedURLException {
  182. return getServlet().getApplicationUrl((VaadinServletRequest) request);
  183. }
  184. public static HttpServletRequest getCurrentServletRequest() {
  185. return VaadinServletRequest.getCurrent();
  186. }
  187. public static VaadinServletResponse getCurrentResponse() {
  188. return VaadinServletResponse.getCurrent();
  189. }
  190. @Override
  191. public String getServiceName() {
  192. return getServlet().getServletName();
  193. }
  194. @Override
  195. public InputStream getThemeResourceAsStream(UI uI, String themeName,
  196. String resource) throws IOException {
  197. String filename = "/" + VaadinServlet.THEME_DIR_PATH + '/' + themeName
  198. + "/" + resource;
  199. URL resourceUrl = servlet.findResourceURL(filename);
  200. if (resourceUrl != null) {
  201. // security check: do not permit navigation out of the VAADIN
  202. // directory
  203. if (!servlet.isAllowedVAADINResourceUrl(null, resourceUrl)) {
  204. throw new IOException(String.format(
  205. "Requested resource [{0}] not accessible in the VAADIN directory or access to it is forbidden.",
  206. filename));
  207. }
  208. return resourceUrl.openStream();
  209. } else {
  210. return null;
  211. }
  212. }
  213. @Override
  214. public String getMainDivId(VaadinSession session, VaadinRequest request,
  215. Class<? extends UI> uiClass) {
  216. String appId = null;
  217. try {
  218. URL appUrl = getServlet()
  219. .getApplicationUrl((VaadinServletRequest) request);
  220. appId = appUrl.getPath();
  221. } catch (MalformedURLException e) {
  222. // Just ignore problem here
  223. }
  224. if (appId == null || "".equals(appId) || "/".equals(appId)) {
  225. appId = "ROOT";
  226. }
  227. appId = appId.replaceAll("[^a-zA-Z0-9]", "");
  228. // Add hashCode to the end, so that it is still (sort of)
  229. // predictable, but indicates that it should not be used in CSS
  230. // and
  231. // such:
  232. int hashCode = appId.hashCode();
  233. if (hashCode < 0) {
  234. hashCode = -hashCode;
  235. }
  236. appId = appId + "-" + hashCode;
  237. return appId;
  238. }
  239. private static final Logger getLogger() {
  240. return Logger.getLogger(VaadinServletService.class.getName());
  241. }
  242. }