123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /*
- * Copyright 2011 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.server;
-
- import java.io.File;
- import java.net.MalformedURLException;
- import java.net.URL;
-
- import javax.servlet.http.HttpServletRequest;
-
- import com.vaadin.server.VaadinServlet.RequestType;
- import com.vaadin.ui.UI;
-
- public class VaadinServletService extends VaadinService {
- private final VaadinServlet servlet;
-
- public VaadinServletService(VaadinServlet servlet,
- DeploymentConfiguration deploymentConfiguration) {
- super(deploymentConfiguration);
- this.servlet = servlet;
- }
-
- protected VaadinServlet getServlet() {
- return servlet;
- }
-
- @Override
- public String getStaticFileLocation(VaadinRequest request) {
- VaadinServletRequest servletRequest = (VaadinServletRequest) request;
- String staticFileLocation;
- // if property is defined in configurations, use that
- staticFileLocation = getDeploymentConfiguration()
- .getApplicationOrSystemProperty(
- VaadinServlet.PARAMETER_VAADIN_RESOURCES, null);
- if (staticFileLocation != null) {
- return staticFileLocation;
- }
-
- // the last (but most common) option is to generate default location
- // from request by finding how many "../" should be added to the
- // requested path before we get to the context root
-
- String requestedPath = servletRequest.getServletPath();
- String pathInfo = servletRequest.getPathInfo();
- if (pathInfo != null) {
- requestedPath += pathInfo;
- }
-
- return getCancelingRelativePath(requestedPath);
- }
-
- /**
- * 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();
- }
-
- @Override
- public String getConfiguredWidgetset(VaadinRequest request) {
- return getDeploymentConfiguration().getApplicationOrSystemProperty(
- VaadinServlet.PARAMETER_WIDGETSET,
- VaadinServlet.DEFAULT_WIDGETSET);
- }
-
- @Override
- public String getConfiguredTheme(VaadinRequest request) {
- // Use the default
- return VaadinServlet.getDefaultTheme();
- }
-
- @Override
- public boolean isStandalone(VaadinRequest request) {
- return true;
- }
-
- @Override
- public String getMimeType(String resourceName) {
- return getServlet().getServletContext().getMimeType(resourceName);
- }
-
- @Override
- public File getBaseDirectory() {
- final String realPath = VaadinServlet.getResourcePath(
- servlet.getServletContext(), "/");
- if (realPath == null) {
- return null;
- }
- return new File(realPath);
- }
-
- @Override
- protected boolean requestCanCreateSession(VaadinRequest request) {
- RequestType requestType = getRequestType(request);
- if (requestType == RequestType.BROWSER_DETAILS) {
- // This is the first request if you are embedding by writing the
- // embedding code yourself
- return true;
- } else if (requestType == RequestType.OTHER) {
- /*
- * I.e URIs that are not RPC calls or static (theme) files.
- */
- return true;
- }
-
- return false;
- }
-
- /**
- * Gets the request type for the request.
- *
- * @param request
- * the request to get a request type for
- * @return the request type
- *
- * @deprecated As of 7.0. Will likely change or be removed in a future version
- */
- @Deprecated
- protected RequestType getRequestType(VaadinRequest request) {
- RequestType type = (RequestType) request.getAttribute(RequestType.class
- .getName());
- if (type == null) {
- type = getServlet().getRequestType((VaadinServletRequest) request);
- request.setAttribute(RequestType.class.getName(), type);
- }
- return type;
- }
-
- @Override
- protected URL getApplicationUrl(VaadinRequest request)
- throws MalformedURLException {
- return getServlet().getApplicationUrl((VaadinServletRequest) request);
- }
-
- @Override
- protected AbstractCommunicationManager createCommunicationManager(
- VaadinSession session) {
- return new CommunicationManager(session);
- }
-
- public static HttpServletRequest getCurrentServletRequest() {
- VaadinRequest currentRequest = VaadinService.getCurrentRequest();
- if (currentRequest instanceof VaadinServletRequest) {
- return (VaadinServletRequest) currentRequest;
- } else {
- return null;
- }
- }
-
- public static VaadinServletResponse getCurrentResponse() {
- return (VaadinServletResponse) VaadinService.getCurrentResponse();
- }
-
- @Override
- public String getServiceName() {
- return getServlet().getServletName();
- }
-
- @Override
- public String getMainDivId(VaadinSession session, VaadinRequest request,
- Class<? extends UI> uiClass) {
- String appId = null;
- try {
- URL appUrl = getServlet().getApplicationUrl(
- (VaadinServletRequest) request);
- appId = appUrl.getPath();
- } catch (MalformedURLException e) {
- // Just ignore problem here
- }
-
- if (appId == null || "".equals(appId)) {
- appId = "ROOT";
- }
- appId = appId.replaceAll("[^a-zA-Z0-9]", "");
- // Add hashCode to the end, so that it is still (sort of)
- // predictable, but indicates that it should not be used in CSS
- // and
- // such:
- int hashCode = appId.hashCode();
- if (hashCode < 0) {
- hashCode = -hashCode;
- }
- appId = appId + "-" + hashCode;
- return appId;
- }
- }
|