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.

VaadinRequest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright 2000-2016 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.io.Serializable;
  21. import java.io.UnsupportedEncodingException;
  22. import java.security.Principal;
  23. import java.util.Enumeration;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import javax.portlet.ClientDataRequest;
  27. import javax.portlet.PortletRequest;
  28. import javax.servlet.ServletRequest;
  29. import javax.servlet.http.Cookie;
  30. import javax.servlet.http.HttpServletRequest;
  31. import com.vaadin.util.CurrentInstance;
  32. /**
  33. * A generic request to the server, wrapping a more specific request type, e.g.
  34. * HttpServletReqest or PortletRequest.
  35. *
  36. * @author Vaadin Ltd
  37. * @since 7.0.0
  38. */
  39. public interface VaadinRequest extends Serializable {
  40. /**
  41. * Gets the named request parameter This is typically a HTTP GET or POST
  42. * parameter, though other request types might have other ways of
  43. * representing parameters.
  44. *
  45. * @see javax.servlet.ServletRequest#getParameter(String)
  46. * @see javax.portlet.PortletRequest#getParameter(String)
  47. *
  48. * @param parameter
  49. * the name of the parameter
  50. * @return The paramter value, or <code>null</code> if no parameter with the
  51. * given name is present
  52. */
  53. public String getParameter(String parameter);
  54. /**
  55. * Gets all the parameters of the request. Framework's internal init
  56. * parameters have prefix "v-" (does not include such parameters as "theme"
  57. * and "debug").
  58. *
  59. * @see #getParameter(String)
  60. *
  61. * @see javax.servlet.ServletRequest#getParameterMap()
  62. * @see javax.portlet.PortletRequest#getParameter(String)
  63. *
  64. * @return A mapping of parameter names to arrays of parameter values
  65. */
  66. public Map<String, String[]> getParameterMap();
  67. /**
  68. * Returns the length of the request content that can be read from the input
  69. * stream returned by {@link #getInputStream()}.
  70. *
  71. * @see javax.servlet.ServletRequest#getContentLength()
  72. * @see javax.portlet.ClientDataRequest#getContentLength()
  73. *
  74. * @return content length in bytes
  75. */
  76. public int getContentLength();
  77. /**
  78. * Returns an input stream from which the request content can be read. The
  79. * request content length can be obtained with {@link #getContentLength()}
  80. * without reading the full stream contents.
  81. *
  82. * @see javax.servlet.ServletRequest#getInputStream()
  83. * @see javax.portlet.ClientDataRequest#getPortletInputStream()
  84. *
  85. * @return the input stream from which the contents of the request can be
  86. * read
  87. * @throws IOException
  88. * if the input stream can not be opened
  89. */
  90. public InputStream getInputStream() throws IOException;
  91. /**
  92. * Gets a request attribute.
  93. *
  94. * @param name
  95. * the name of the attribute
  96. * @return the value of the attribute, or <code>null</code> if there is no
  97. * attribute with the given name
  98. *
  99. * @see javax.servlet.ServletRequest#getAttribute(String)
  100. * @see javax.portlet.PortletRequest#getAttribute(String)
  101. */
  102. public Object getAttribute(String name);
  103. /**
  104. * Defines a request attribute.
  105. *
  106. * @param name
  107. * the name of the attribute
  108. * @param value
  109. * the attribute value
  110. *
  111. * @see javax.servlet.ServletRequest#setAttribute(String, Object)
  112. * @see javax.portlet.PortletRequest#setAttribute(String, Object)
  113. */
  114. public void setAttribute(String name, Object value);
  115. /**
  116. * Gets the path of the requested resource relative to the application. The
  117. * path is <code>null</code> if no path information is available. Does
  118. * always start with / if the path isn't <code>null</code>.
  119. *
  120. * @return a string with the path relative to the application.
  121. *
  122. * @see javax.servlet.http.HttpServletRequest#getPathInfo()
  123. */
  124. public String getPathInfo();
  125. /**
  126. * Returns the portion of the request URI that indicates the context of the
  127. * request. The context path always comes first in a request URI.
  128. *
  129. * @see HttpServletRequest#getContextPath()
  130. * @see PortletRequest#getContextPath()
  131. *
  132. * @return a String specifying the portion of the request URI that indicates
  133. * the context of the request
  134. */
  135. public String getContextPath();
  136. /**
  137. * Gets the session associated with this request, creating a new if there is
  138. * no session.
  139. *
  140. * @see WrappedSession
  141. * @see HttpServletRequest#getSession()
  142. * @see PortletRequest#getPortletSession()
  143. *
  144. * @return the wrapped session for this request
  145. */
  146. public WrappedSession getWrappedSession();
  147. /**
  148. * Gets the session associated with this request, optionally creating a new
  149. * if there is no session.
  150. *
  151. * @param allowSessionCreation
  152. * <code>true</code> to create a new session for this request if
  153. * necessary; <code>false</code> to return <code>null</code> if
  154. * there's no current session
  155. *
  156. * @see WrappedSession
  157. * @see HttpServletRequest#getSession(boolean)
  158. * @see PortletRequest#getPortletSession(boolean)
  159. *
  160. * @return the wrapped session for this request
  161. */
  162. public WrappedSession getWrappedSession(boolean allowSessionCreation);
  163. /**
  164. * Returns the MIME type of the body of the request, or null if the type is
  165. * not known.
  166. *
  167. * @return a string containing the name of the MIME type of the request, or
  168. * null if the type is not known
  169. *
  170. * @see javax.servlet.ServletRequest#getContentType()
  171. * @see javax.portlet.ResourceRequest#getContentType()
  172. *
  173. */
  174. public String getContentType();
  175. /**
  176. * Gets locale information from the query, e.g. using the Accept-Language
  177. * header.
  178. *
  179. * @return the preferred Locale
  180. *
  181. * @see ServletRequest#getLocale()
  182. * @see PortletRequest#getLocale()
  183. */
  184. public Locale getLocale();
  185. /**
  186. * Returns the IP address from which the request came. This might also be
  187. * the address of a proxy between the server and the original requester.
  188. *
  189. * @return a string containing the IP address, or <code>null</code> if the
  190. * address is not available
  191. *
  192. * @see ServletRequest#getRemoteAddr()
  193. */
  194. public String getRemoteAddr();
  195. /**
  196. * Checks whether the request was made using a secure channel, e.g. using
  197. * https.
  198. *
  199. * @return a boolean indicating if the request is secure
  200. *
  201. * @see ServletRequest#isSecure()
  202. * @see PortletRequest#isSecure()
  203. */
  204. public boolean isSecure();
  205. /**
  206. * Gets the value of a request header, e.g. a http header for a
  207. * {@link HttpServletRequest}.
  208. *
  209. * @param headerName
  210. * the name of the header
  211. * @return the header value, or <code>null</code> if the header is not
  212. * present in the request
  213. *
  214. * @see HttpServletRequest#getHeader(String)
  215. */
  216. public String getHeader(String headerName);
  217. /**
  218. * Gets the vaadin service for the context of this request.
  219. *
  220. * @return the vaadin service
  221. *
  222. * @see VaadinService
  223. */
  224. public VaadinService getService();
  225. /**
  226. * Returns an array containing all of the <code>Cookie</code> objects the
  227. * client sent with this request. This method returns <code>null</code> if
  228. * no cookies were sent.
  229. *
  230. * @return an array of all the <code>Cookies</code> included with this
  231. * request, or <code>null</code> if the request has no cookies
  232. *
  233. * @see HttpServletRequest#getCookies()
  234. * @see PortletRequest#getCookies()
  235. */
  236. public Cookie[] getCookies();
  237. /**
  238. * Returns the name of the authentication scheme used for the connection
  239. * between client and server, for example, <code>BASIC_AUTH</code>,
  240. * <code>CLIENT_CERT_AUTH</code>, a custom one or <code>null</code> if there
  241. * was no authentication.
  242. *
  243. * @return a string indicating the authentication scheme, or
  244. * <code>null</code> if the request was not authenticated.
  245. *
  246. * @see HttpServletRequest#getAuthType()
  247. * @see PortletRequest#getAuthType()
  248. */
  249. public String getAuthType();
  250. /**
  251. * Returns the login of the user making this request, if the user has been
  252. * authenticated, or null if the user has not been authenticated. Whether
  253. * the user name is sent with each subsequent request depends on the browser
  254. * and type of authentication.
  255. *
  256. * @return a String specifying the login of the user making this request, or
  257. * <code>null</code> if the user login is not known.
  258. *
  259. * @see HttpServletRequest#getRemoteUser()
  260. * @see PortletRequest#getRemoteUser()
  261. */
  262. public String getRemoteUser();
  263. /**
  264. * Returns a <code>java.security.Principal</code> object containing the name
  265. * of the current authenticated user. If the user has not been
  266. * authenticated, the method returns <code>null</code>.
  267. *
  268. * @return a <code>java.security.Principal</code> containing the name of the
  269. * user making this request; <code>null</code> if the user has not
  270. * been authenticated
  271. *
  272. * @see HttpServletRequest#getUserPrincipal()
  273. * @see PortletRequest#getUserPrincipal()
  274. */
  275. public Principal getUserPrincipal();
  276. /**
  277. * Returns a boolean indicating whether the authenticated user is included
  278. * in the specified logical "role". Roles and role membership can be defined
  279. * using deployment descriptors. If the user has not been authenticated, the
  280. * method returns <code>false</code>.
  281. *
  282. * @param role
  283. * a String specifying the name of the role
  284. * @return a boolean indicating whether the user making this request belongs
  285. * to a given role; <code>false</code> if the user has not been
  286. * authenticated
  287. *
  288. * @see HttpServletRequest#isUserInRole(String)
  289. * @see PortletRequest#isUserInRole(String)
  290. */
  291. public boolean isUserInRole(String role);
  292. /**
  293. * Removes an attribute from this request. This method is not generally
  294. * needed as attributes only persist as long as the request is being
  295. * handled.
  296. *
  297. * @param name
  298. * a String specifying the name of the attribute to remove
  299. *
  300. * @see ServletRequest#removeAttribute(String)
  301. * @see PortletRequest#removeAttribute(String)
  302. */
  303. public void removeAttribute(String name);
  304. /**
  305. * Returns an Enumeration containing the names of the attributes available
  306. * to this request. This method returns an empty Enumeration if the request
  307. * has no attributes available to it.
  308. *
  309. * @return an Enumeration of strings containing the names of the request's
  310. * attributes
  311. *
  312. * @see ServletRequest#getAttributeNames()
  313. * @see PortletRequest#getAttributeNames()
  314. */
  315. public Enumeration<String> getAttributeNames();
  316. /**
  317. * Returns an Enumeration of Locale objects indicating, in decreasing order
  318. * starting with the preferred locale, the locales that are acceptable to
  319. * the client based on the Accept-Language header. If the client request
  320. * doesn't provide an Accept-Language header, this method returns an
  321. * Enumeration containing one Locale, the default locale for the server.
  322. *
  323. * @return an Enumeration of preferred Locale objects for the client
  324. *
  325. * @see HttpServletRequest#getLocales()
  326. * @see PortletRequest#getLocales()
  327. */
  328. public Enumeration<Locale> getLocales();
  329. /**
  330. * Returns the fully qualified name of the client or the last proxy that
  331. * sent the request. If the engine cannot or chooses not to resolve the
  332. * hostname (to improve performance), this method returns the dotted-string
  333. * form of the IP address.
  334. *
  335. * @return a String containing the fully qualified name of the client, or
  336. * <code>null</code> if the information is not available.
  337. *
  338. * @see HttpServletRequest#getRemoteHost()
  339. */
  340. public String getRemoteHost();
  341. /**
  342. * Returns the Internet Protocol (IP) source port of the client or last
  343. * proxy that sent the request.
  344. *
  345. * @return an integer specifying the port number, or -1 if the information
  346. * is not available.
  347. *
  348. * @see ServletRequest#getRemotePort()
  349. */
  350. public int getRemotePort();
  351. /**
  352. * Returns the name of the character encoding used in the body of this
  353. * request. This method returns <code>null</code> if the request does not
  354. * specify a character encoding.
  355. *
  356. * @return a String containing the name of the character encoding, or null
  357. * if the request does not specify a character encoding
  358. *
  359. * @see ServletRequest#getCharacterEncoding()
  360. * @see ClientDataRequest#getCharacterEncoding()
  361. */
  362. public String getCharacterEncoding();
  363. /**
  364. * Retrieves the body of the request as character data using a
  365. * <code>BufferedReader</code>. The reader translates the character data
  366. * according to the character encoding used on the body. Either this method
  367. * or {@link #getInputStream()} may be called to read the body, not both.
  368. *
  369. * @return a BufferedReader containing the body of the request
  370. *
  371. * @throws UnsupportedEncodingException
  372. * - if the character set encoding used is not supported and the
  373. * text cannot be decoded
  374. * @throws IllegalStateException
  375. * - if {@link #getInputStream()} method has been called on this
  376. * request
  377. * @throws IOException
  378. * if an input or output exception occurred
  379. *
  380. * @see ServletRequest#getReader()
  381. * @see ClientDataRequest#getReader()
  382. */
  383. public BufferedReader getReader() throws IOException;
  384. /**
  385. * Returns the name of the HTTP method with which this request was made, for
  386. * example, GET, POST, or PUT.
  387. *
  388. * @return a String specifying the name of the method with which this
  389. * request was made
  390. *
  391. * @see HttpServletRequest#getMethod()
  392. * @see ClientDataRequest#getMethod()
  393. */
  394. public String getMethod();
  395. /**
  396. * Returns the value of the specified request header as a long value that
  397. * represents a Date object. Use this method with headers that contain
  398. * dates, such as If-Modified-Since.
  399. * <p>
  400. * The date is returned as the number of milliseconds since January 1, 1970
  401. * GMT. The header name is case insensitive.
  402. * <p>
  403. * If the request did not have a header of the specified name, this method
  404. * returns -1. If the header can't be converted to a date, the method throws
  405. * an IllegalArgumentException.
  406. *
  407. * @param name
  408. * a String specifying the name of the header
  409. * @return a long value representing the date specified in the header
  410. * expressed as the number of milliseconds since January 1, 1970
  411. * GMT, or -1 if the named header was not included with the request
  412. * @throws IllegalArgumentException
  413. * If the header value can't be converted to a date
  414. * @see HttpServletRequest#getDateHeader(String)
  415. */
  416. public long getDateHeader(String name);
  417. /**
  418. * Returns an enumeration of all the header names this request contains. If
  419. * the request has no headers, this method returns an empty enumeration.
  420. * <p>
  421. * Some implementations do not allow access headers using this method, in
  422. * which case this method returns <code>null</code>
  423. *
  424. * @return an enumeration of all the header names sent with this request; if
  425. * the request has no headers, an empty enumeration; if the
  426. * implementation does not allow this method, <code>null</code>
  427. * @see HttpServletRequest#getHeaderNames()
  428. */
  429. public Enumeration<String> getHeaderNames();
  430. /**
  431. * Returns all the values of the specified request header as an Enumeration
  432. * of String objects.
  433. * <p>
  434. * Some headers, such as <code>Accept-Language</code> can be sent by clients
  435. * as several headers each with a different value rather than sending the
  436. * header as a comma separated list.
  437. * <p>
  438. * If the request did not include any headers of the specified name, this
  439. * method returns an empty Enumeration. If the request does not support
  440. * accessing headers, this method returns <code>null</code>.
  441. * <p>
  442. * The header name is case insensitive. You can use this method with any
  443. * request header.
  444. *
  445. *
  446. * @param name
  447. * a String specifying the header name
  448. * @return an Enumeration containing the values of the requested header. If
  449. * the request does not have any headers of that name return an
  450. * empty enumeration. If the header information is not available,
  451. * return <code>null</code>
  452. * @see HttpServletRequest#getHeaders(String)
  453. */
  454. public Enumeration<String> getHeaders(String name);
  455. /**
  456. * Gets the currently processed Vaadin request. The current request is
  457. * automatically defined when the request is started. The current request
  458. * can not be used in e.g. background threads because of the way server
  459. * implementations reuse request instances.
  460. *
  461. * @return the current Vaadin request instance if available, otherwise
  462. * <code>null</code>
  463. * @since 8.1
  464. */
  465. public static VaadinRequest getCurrent() {
  466. return CurrentInstance.get(VaadinRequest.class);
  467. }
  468. }