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.

VaadinPortletService.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.URL;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.portlet.PortletContext;
  22. import javax.portlet.PortletRequest;
  23. import com.vaadin.server.VaadinPortlet.RequestType;
  24. import com.vaadin.ui.UI;
  25. public class VaadinPortletService extends VaadinService {
  26. private final VaadinPortlet portlet;
  27. public VaadinPortletService(VaadinPortlet portlet,
  28. DeploymentConfiguration deploymentConfiguration) {
  29. super(deploymentConfiguration);
  30. this.portlet = portlet;
  31. }
  32. protected VaadinPortlet getPortlet() {
  33. return portlet;
  34. }
  35. private static String getPortalProperty(VaadinRequest request,
  36. String portalParameterVaadinWidgetset) {
  37. return ((VaadinPortletRequest) request)
  38. .getPortalProperty(portalParameterVaadinWidgetset);
  39. }
  40. @Override
  41. public String getConfiguredWidgetset(VaadinRequest request) {
  42. String widgetset = getDeploymentConfiguration()
  43. .getApplicationOrSystemProperty(
  44. VaadinPortlet.PARAMETER_WIDGETSET, null);
  45. if (widgetset == null) {
  46. // If no widgetset defined for the application, check the
  47. // portal property
  48. widgetset = getPortalProperty(request,
  49. VaadinPortlet.PORTAL_PARAMETER_VAADIN_WIDGETSET);
  50. if ("com.vaadin.portal.gwt.PortalDefaultWidgetSet"
  51. .equals(widgetset)) {
  52. // For backwards compatibility - automatically map old portal
  53. // default widget set to default widget set
  54. widgetset = VaadinPortlet.DEFAULT_WIDGETSET;
  55. }
  56. }
  57. if (widgetset == null) {
  58. // If no widgetset defined for the portal, use the default
  59. widgetset = VaadinPortlet.DEFAULT_WIDGETSET;
  60. }
  61. return widgetset;
  62. }
  63. @Override
  64. public String getConfiguredTheme(VaadinRequest request) {
  65. // is the default theme defined by the portal?
  66. String themeName = getPortalProperty(request,
  67. Constants.PORTAL_PARAMETER_VAADIN_THEME);
  68. if (themeName == null) {
  69. // no, using the default theme defined by Vaadin
  70. themeName = VaadinPortlet.DEFAULT_THEME_NAME;
  71. }
  72. return themeName;
  73. }
  74. @Override
  75. public boolean isStandalone(VaadinRequest request) {
  76. return false;
  77. }
  78. @Override
  79. public String getStaticFileLocation(VaadinRequest request) {
  80. String staticFileLocation = getPortalProperty(request,
  81. Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH);
  82. if (staticFileLocation != null) {
  83. // remove trailing slash if any
  84. while (staticFileLocation.endsWith(".")) {
  85. staticFileLocation = staticFileLocation.substring(0,
  86. staticFileLocation.length() - 1);
  87. }
  88. return staticFileLocation;
  89. } else {
  90. // default for Liferay
  91. return "/html";
  92. }
  93. }
  94. @Override
  95. public String getMimeType(String resourceName) {
  96. return getPortlet().getPortletContext().getMimeType(resourceName);
  97. }
  98. @Override
  99. public File getBaseDirectory() {
  100. PortletContext context = getPortlet().getPortletContext();
  101. String resultPath = context.getRealPath("/");
  102. if (resultPath != null) {
  103. return new File(resultPath);
  104. } else {
  105. try {
  106. final URL url = context.getResource("/");
  107. return new File(url.getFile());
  108. } catch (final Exception e) {
  109. // FIXME: Handle exception
  110. getLogger()
  111. .log(Level.INFO,
  112. "Cannot access base directory, possible security issue "
  113. + "with Application Server or Servlet Container",
  114. e);
  115. }
  116. }
  117. return null;
  118. }
  119. private static final Logger getLogger() {
  120. return Logger.getLogger(VaadinPortletService.class.getName());
  121. }
  122. @Override
  123. protected boolean requestCanCreateSession(VaadinRequest request) {
  124. RequestType requestType = getRequestType(request);
  125. if (requestType == RequestType.RENDER) {
  126. // In most cases the first request is a render request that
  127. // renders the HTML fragment. This should create a Vaadin
  128. // session unless there is already one.
  129. return true;
  130. } else if (requestType == RequestType.EVENT) {
  131. // A portlet can also be sent an event even though it has not
  132. // been rendered, e.g. portlet on one page sends an event to a
  133. // portlet on another page and then moves the user to that page.
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Gets the request type for the request.
  140. *
  141. * @param request
  142. * the request to get a request type for
  143. * @return the request type
  144. *
  145. * @deprecated As of 7.0. Will likely change or be removed in a future version
  146. */
  147. @Deprecated
  148. protected RequestType getRequestType(VaadinRequest request) {
  149. RequestType type = (RequestType) request.getAttribute(RequestType.class
  150. .getName());
  151. if (type == null) {
  152. type = getPortlet().getRequestType((VaadinPortletRequest) request);
  153. request.setAttribute(RequestType.class.getName(), type);
  154. }
  155. return type;
  156. }
  157. @Override
  158. protected AbstractCommunicationManager createCommunicationManager(
  159. VaadinSession session) {
  160. return new PortletCommunicationManager(session);
  161. }
  162. public static PortletRequest getCurrentPortletRequest() {
  163. VaadinRequest currentRequest = VaadinService.getCurrentRequest();
  164. if (currentRequest instanceof VaadinPortletRequest) {
  165. return ((VaadinPortletRequest) currentRequest).getPortletRequest();
  166. } else {
  167. return null;
  168. }
  169. }
  170. public static VaadinPortletResponse getCurrentResponse() {
  171. return (VaadinPortletResponse) VaadinService.getCurrentResponse();
  172. }
  173. @Override
  174. protected VaadinSession createVaadinSession(VaadinRequest request)
  175. throws ServiceException {
  176. return new VaadinPortletSession(this);
  177. }
  178. @Override
  179. public String getServiceName() {
  180. return getPortlet().getPortletName();
  181. }
  182. /**
  183. * Always preserve UIs in portlets to make portlet actions work.
  184. */
  185. @Override
  186. public boolean preserveUIOnRefresh(UIProvider provider, UICreateEvent event) {
  187. return true;
  188. }
  189. @Override
  190. public String getMainDivId(VaadinSession session, VaadinRequest request,
  191. Class<? extends UI> uiClass) {
  192. PortletRequest portletRequest = ((VaadinPortletRequest) request)
  193. .getPortletRequest();
  194. /*
  195. * We need to generate a unique ID because some portals already create a
  196. * DIV with the portlet's Window ID as the DOM ID.
  197. */
  198. return "v-" + portletRequest.getWindowID();
  199. }
  200. }