You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinServletService.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2011 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.net.MalformedURLException;
  19. import java.net.URL;
  20. import javax.servlet.http.HttpServletRequest;
  21. import com.vaadin.server.VaadinServlet.RequestType;
  22. import com.vaadin.ui.UI;
  23. public class VaadinServletService extends VaadinService {
  24. private final VaadinServlet servlet;
  25. public VaadinServletService(VaadinServlet servlet,
  26. DeploymentConfiguration deploymentConfiguration) {
  27. super(deploymentConfiguration);
  28. this.servlet = servlet;
  29. }
  30. protected VaadinServlet getServlet() {
  31. return servlet;
  32. }
  33. @Override
  34. public String getStaticFileLocation(VaadinRequest request) {
  35. VaadinServletRequest servletRequest = (VaadinServletRequest) request;
  36. String staticFileLocation;
  37. // if property is defined in configurations, use that
  38. staticFileLocation = getDeploymentConfiguration()
  39. .getApplicationOrSystemProperty(
  40. VaadinServlet.PARAMETER_VAADIN_RESOURCES, null);
  41. if (staticFileLocation != null) {
  42. return staticFileLocation;
  43. }
  44. // the last (but most common) option is to generate default location
  45. // from request by finding how many "../" should be added to the
  46. // requested path before we get to the context root
  47. String requestedPath = servletRequest.getServletPath();
  48. String pathInfo = servletRequest.getPathInfo();
  49. if (pathInfo != null) {
  50. requestedPath += pathInfo;
  51. }
  52. return getCancelingRelativePath(requestedPath);
  53. }
  54. /**
  55. * Gets a relative path that cancels the provided path. This essentially
  56. * adds one .. for each part of the path to cancel.
  57. *
  58. * @param pathToCancel
  59. * the path that should be canceled
  60. * @return a relative path that cancels out the provided path segment
  61. */
  62. public static String getCancelingRelativePath(String pathToCancel) {
  63. StringBuilder sb = new StringBuilder(".");
  64. // Start from i = 1 to ignore first slash
  65. for (int i = 1; i < pathToCancel.length(); i++) {
  66. if (pathToCancel.charAt(i) == '/') {
  67. sb.append("/..");
  68. }
  69. }
  70. return sb.toString();
  71. }
  72. @Override
  73. public String getConfiguredWidgetset(VaadinRequest request) {
  74. return getDeploymentConfiguration().getApplicationOrSystemProperty(
  75. VaadinServlet.PARAMETER_WIDGETSET,
  76. VaadinServlet.DEFAULT_WIDGETSET);
  77. }
  78. @Override
  79. public String getConfiguredTheme(VaadinRequest request) {
  80. // Use the default
  81. return VaadinServlet.getDefaultTheme();
  82. }
  83. @Override
  84. public boolean isStandalone(VaadinRequest request) {
  85. return true;
  86. }
  87. @Override
  88. public String getMimeType(String resourceName) {
  89. return getServlet().getServletContext().getMimeType(resourceName);
  90. }
  91. @Override
  92. public File getBaseDirectory() {
  93. final String realPath = VaadinServlet.getResourcePath(
  94. servlet.getServletContext(), "/");
  95. if (realPath == null) {
  96. return null;
  97. }
  98. return new File(realPath);
  99. }
  100. @Override
  101. protected boolean requestCanCreateSession(VaadinRequest request) {
  102. RequestType requestType = getRequestType(request);
  103. if (requestType == RequestType.BROWSER_DETAILS) {
  104. // This is the first request if you are embedding by writing the
  105. // embedding code yourself
  106. return true;
  107. } else if (requestType == RequestType.OTHER) {
  108. /*
  109. * I.e URIs that are not RPC calls or static (theme) files.
  110. */
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * Gets the request type for the request.
  117. *
  118. * @param request
  119. * the request to get a request type for
  120. * @return the request type
  121. *
  122. * @deprecated As of 7.0. Will likely change or be removed in a future version
  123. */
  124. @Deprecated
  125. protected RequestType getRequestType(VaadinRequest request) {
  126. RequestType type = (RequestType) request.getAttribute(RequestType.class
  127. .getName());
  128. if (type == null) {
  129. type = getServlet().getRequestType((VaadinServletRequest) request);
  130. request.setAttribute(RequestType.class.getName(), type);
  131. }
  132. return type;
  133. }
  134. @Override
  135. protected URL getApplicationUrl(VaadinRequest request)
  136. throws MalformedURLException {
  137. return getServlet().getApplicationUrl((VaadinServletRequest) request);
  138. }
  139. @Override
  140. protected AbstractCommunicationManager createCommunicationManager(
  141. VaadinSession session) {
  142. return new CommunicationManager(session);
  143. }
  144. public static HttpServletRequest getCurrentServletRequest() {
  145. VaadinRequest currentRequest = VaadinService.getCurrentRequest();
  146. if (currentRequest instanceof VaadinServletRequest) {
  147. return (VaadinServletRequest) currentRequest;
  148. } else {
  149. return null;
  150. }
  151. }
  152. public static VaadinServletResponse getCurrentResponse() {
  153. return (VaadinServletResponse) VaadinService.getCurrentResponse();
  154. }
  155. @Override
  156. public String getServiceName() {
  157. return getServlet().getServletName();
  158. }
  159. @Override
  160. public String getMainDivId(VaadinSession session, VaadinRequest request,
  161. Class<? extends UI> uiClass) {
  162. String appId = null;
  163. try {
  164. URL appUrl = getServlet().getApplicationUrl(
  165. (VaadinServletRequest) request);
  166. appId = appUrl.getPath();
  167. } catch (MalformedURLException e) {
  168. // Just ignore problem here
  169. }
  170. if (appId == null || "".equals(appId)) {
  171. appId = "ROOT";
  172. }
  173. appId = appId.replaceAll("[^a-zA-Z0-9]", "");
  174. // Add hashCode to the end, so that it is still (sort of)
  175. // predictable, but indicates that it should not be used in CSS
  176. // and
  177. // such:
  178. int hashCode = appId.hashCode();
  179. if (hashCode < 0) {
  180. hashCode = -hashCode;
  181. }
  182. appId = appId + "-" + hashCode;
  183. return appId;
  184. }
  185. }