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

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