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.

VaadinPortletRequest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2000-2018 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.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.text.ParseException;
  21. import java.util.Enumeration;
  22. import javax.portlet.ClientDataRequest;
  23. import javax.portlet.PortletPreferences;
  24. import javax.portlet.PortletRequest;
  25. import javax.portlet.PortletSession;
  26. import javax.portlet.ResourceRequest;
  27. import javax.portlet.filter.PortletRequestWrapper;
  28. import com.vaadin.shared.ApplicationConstants;
  29. /**
  30. * Wrapper for {@link PortletRequest} and its subclasses.
  31. *
  32. * @author Vaadin Ltd.
  33. * @since 7.0
  34. *
  35. * @see VaadinRequest
  36. * @see VaadinPortletResponse
  37. */
  38. public class VaadinPortletRequest extends PortletRequestWrapper
  39. implements VaadinRequest {
  40. private final VaadinPortletService vaadinService;
  41. /**
  42. * Wraps a portlet request and an associated vaadin service.
  43. *
  44. * @param request
  45. * the portlet request to wrap
  46. * @param vaadinService
  47. * the associated vaadin service
  48. */
  49. public VaadinPortletRequest(PortletRequest request,
  50. VaadinPortletService vaadinService) {
  51. super(request);
  52. this.vaadinService = vaadinService;
  53. }
  54. @Override
  55. public int getContentLength() {
  56. try {
  57. return ((ClientDataRequest) getRequest()).getContentLength();
  58. } catch (ClassCastException e) {
  59. throw new IllegalStateException(
  60. "Content lenght only available for ClientDataRequests");
  61. }
  62. }
  63. @Override
  64. public InputStream getInputStream() throws IOException {
  65. try {
  66. return ((ClientDataRequest) getRequest()).getPortletInputStream();
  67. } catch (ClassCastException e) {
  68. throw new IllegalStateException(
  69. "Input data only available for ClientDataRequests");
  70. }
  71. }
  72. @Override
  73. public BufferedReader getReader() throws IOException {
  74. try {
  75. return ((ClientDataRequest) getRequest()).getReader();
  76. } catch (ClassCastException e) {
  77. throw new IllegalStateException(
  78. "Reader only available for ClientDataRequests");
  79. }
  80. }
  81. @Override
  82. public String getPathInfo() {
  83. PortletRequest request = getRequest();
  84. if (request instanceof ResourceRequest) {
  85. ResourceRequest resourceRequest = (ResourceRequest) request;
  86. String resourceID = resourceRequest.getResourceID();
  87. if (VaadinPortlet.RESOURCE_URL_ID.equals(resourceID)) {
  88. String resourcePath = resourceRequest
  89. .getParameter(ApplicationConstants.V_RESOURCE_PATH);
  90. return resourcePath;
  91. }
  92. return resourceID;
  93. } else {
  94. return null;
  95. }
  96. }
  97. @Override
  98. public WrappedSession getWrappedSession() {
  99. return getWrappedSession(true);
  100. }
  101. @Override
  102. public WrappedSession getWrappedSession(boolean allowSessionCreation) {
  103. PortletSession session = getPortletSession(allowSessionCreation);
  104. if (session != null) {
  105. return new WrappedPortletSession(session);
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * Gets the original, unwrapped portlet request.
  112. *
  113. * @return the unwrapped portlet request
  114. */
  115. public PortletRequest getPortletRequest() {
  116. return getRequest();
  117. }
  118. @Override
  119. public String getContentType() {
  120. try {
  121. return ((ResourceRequest) getRequest()).getContentType();
  122. } catch (ClassCastException e) {
  123. throw new IllegalStateException(
  124. "Content type only available for ResourceRequests");
  125. }
  126. }
  127. @Override
  128. public String getCharacterEncoding() {
  129. try {
  130. return ((ClientDataRequest) getRequest()).getCharacterEncoding();
  131. } catch (ClassCastException e) {
  132. throw new IllegalStateException(
  133. "Character encoding only available for ClientDataRequest");
  134. }
  135. }
  136. @Override
  137. public String getMethod() {
  138. try {
  139. return ((ClientDataRequest) getRequest()).getMethod();
  140. } catch (ClassCastException e) {
  141. throw new IllegalStateException(
  142. "Method only available for ClientDataRequest");
  143. }
  144. }
  145. @Override
  146. public String getRemoteAddr() {
  147. return null;
  148. }
  149. @Override
  150. public String getRemoteHost() {
  151. return null;
  152. }
  153. @Override
  154. public int getRemotePort() {
  155. return -1;
  156. }
  157. @Override
  158. public String getHeader(String string) {
  159. return null;
  160. }
  161. /**
  162. * Reads a portal property from the portal context of the Vaadin request.
  163. *
  164. * @param name
  165. * a string with the name of the portal property to get
  166. * @return a string with the value of the property, or <code>null</code> if
  167. * the property is not defined
  168. */
  169. public String getPortalProperty(String name) {
  170. return getRequest().getPortalContext().getProperty(name);
  171. }
  172. /**
  173. * Reads a portlet preference from the portlet of the request.
  174. *
  175. * @param name
  176. * The name of the portlet preference. Cannot be
  177. * <code>null</code>.
  178. *
  179. * @return The value of the portlet preference, <code>null</code> if the
  180. * preference is not defined.
  181. */
  182. public String getPortletPreference(String name) {
  183. PortletRequest request = getRequest();
  184. PortletPreferences preferences = request.getPreferences();
  185. return preferences.getValue(name, null);
  186. }
  187. @Override
  188. public VaadinPortletService getService() {
  189. return vaadinService;
  190. }
  191. @Override
  192. public long getDateHeader(String name) {
  193. String header = getHeader(name);
  194. if (header == null) {
  195. return -1;
  196. } else {
  197. try {
  198. return VaadinPortletResponse.HTTP_DATE_FORMAT.parse(header)
  199. .getTime();
  200. } catch (ParseException e) {
  201. throw new IllegalArgumentException(e);
  202. }
  203. }
  204. }
  205. @Override
  206. public Enumeration<String> getHeaderNames() {
  207. return null;
  208. }
  209. @Override
  210. public Enumeration<String> getHeaders(String name) {
  211. return null;
  212. }
  213. /**
  214. * Gets the currently processed portlet request. The current portlet request
  215. * is automatically defined when the request is started. The current portlet
  216. * request can not be used in e.g. background threads because of the way
  217. * server implementations reuse request instances.
  218. *
  219. * @return the current portlet request instance if available, otherwise
  220. * <code>null</code>
  221. * @since 7.3
  222. */
  223. public static PortletRequest getCurrentPortletRequest() {
  224. return VaadinPortletService.getCurrentPortletRequest();
  225. }
  226. /**
  227. * Gets the currently processed Vaadin portlet request. The current request
  228. * is automatically defined when the request is started. The current request
  229. * can not be used in e.g. background threads because of the way server
  230. * implementations reuse request instances.
  231. *
  232. * @return the current Vaadin portlet request instance if available,
  233. * otherwise <code>null</code>
  234. * @since 7.3
  235. */
  236. public static VaadinPortletRequest getCurrent() {
  237. return VaadinPortletService.getCurrentRequest();
  238. }
  239. }