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.

VaadinResponse.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.IOException;
  18. import java.io.OutputStream;
  19. import java.io.PrintWriter;
  20. import java.io.Serializable;
  21. import javax.portlet.MimeResponse;
  22. import javax.portlet.PortletResponse;
  23. import javax.portlet.ResourceResponse;
  24. import javax.servlet.ServletResponse;
  25. import javax.servlet.http.Cookie;
  26. import javax.servlet.http.HttpServletResponse;
  27. import com.vaadin.util.CurrentInstance;
  28. /**
  29. * A generic response from the server, wrapping a more specific response type,
  30. * e.g. HttpServletResponse or PortletResponse.
  31. *
  32. * @since 7.0
  33. */
  34. public interface VaadinResponse extends Serializable {
  35. /**
  36. * Sets the (http) status code for the response. If you want to include an
  37. * error message along the status code, use {@link #sendError(int, String)}
  38. * instead.
  39. *
  40. * @param statusCode
  41. * the status code to set
  42. * @see HttpServletResponse#setStatus(int)
  43. *
  44. * @see ResourceResponse#HTTP_STATUS_CODE
  45. */
  46. public void setStatus(int statusCode);
  47. /**
  48. * Sets the content type of this response. If the content type including a
  49. * charset is set before {@link #getWriter()} is invoked, the returned
  50. * PrintWriter will automatically use the defined charset.
  51. *
  52. * @param contentType
  53. * a string specifying the MIME type of the content
  54. *
  55. * @see ServletResponse#setContentType(String)
  56. * @see MimeResponse#setContentType(String)
  57. */
  58. public void setContentType(String contentType);
  59. /**
  60. * Sets the value of a generic response header. If the header had already
  61. * been set, the new value overwrites the previous one.
  62. *
  63. * @param name
  64. * the name of the header
  65. * @param value
  66. * the header value.
  67. *
  68. * @see HttpServletResponse#setHeader(String, String)
  69. * @see PortletResponse#setProperty(String, String)
  70. */
  71. public void setHeader(String name, String value);
  72. /**
  73. * Properly formats a timestamp as a date header. If the header had already
  74. * been set, the new value overwrites the previous one.
  75. *
  76. * @param name
  77. * the name of the header
  78. * @param timestamp
  79. * the number of milliseconds since epoch
  80. *
  81. * @see HttpServletResponse#setDateHeader(String, long)
  82. */
  83. public void setDateHeader(String name, long timestamp);
  84. /**
  85. * Returns a <code>OutputStream</code> for writing binary data in the
  86. * response.
  87. * <p>
  88. * Either this method or getWriter() may be called to write the response,
  89. * not both.
  90. *
  91. * @return a <code>OutputStream</code> for writing binary data
  92. * @throws IOException
  93. * if an input or output exception occurred
  94. *
  95. * @see #getWriter()
  96. * @see ServletResponse#getOutputStream()
  97. * @see MimeResponse#getPortletOutputStream()
  98. */
  99. public OutputStream getOutputStream() throws IOException;
  100. /**
  101. * Returns a <code>PrintWriter</code> object that can send character text to
  102. * the client. The PrintWriter uses the character encoding defined using
  103. * setContentType.
  104. * <p>
  105. * Either this method or getOutputStream() may be called to write the
  106. * response, not both.
  107. *
  108. * @return a <code>PrintWriter</code> for writing character text
  109. * @throws IOException
  110. * if an input or output exception occurred
  111. *
  112. * @see #getOutputStream()
  113. * @see ServletResponse#getWriter()
  114. * @see MimeResponse#getWriter()
  115. */
  116. public PrintWriter getWriter() throws IOException;
  117. /**
  118. * Sets cache time in milliseconds, -1 means no cache at all. All required
  119. * headers related to caching in the response are set based on the time.
  120. *
  121. * @param milliseconds
  122. * Cache time in milliseconds
  123. */
  124. public void setCacheTime(long milliseconds);
  125. /**
  126. * Sends an error response to the client using the specified status code and
  127. * clears the buffer. In some configurations, this can cause a predefined
  128. * error page to be displayed.
  129. *
  130. * @param errorCode
  131. * the HTTP status code
  132. * @param message
  133. * a message to accompany the error
  134. * @throws IOException
  135. * if an input or output exception occurs
  136. *
  137. * @see HttpServletResponse#sendError(int, String)
  138. */
  139. public void sendError(int errorCode, String message) throws IOException;
  140. /**
  141. * Gets the vaadin service for the context of this response.
  142. *
  143. * @return the vaadin service
  144. *
  145. * @see VaadinService
  146. */
  147. public VaadinService getService();
  148. /**
  149. * Adds the specified cookie to the response. This method can be called
  150. * multiple times to set more than one cookie.
  151. *
  152. * @param cookie
  153. * the Cookie to return to the client
  154. *
  155. * @see HttpServletResponse#addCookie(Cookie)
  156. * @see PortletResponse#addProperty(Cookie)
  157. */
  158. public void addCookie(Cookie cookie);
  159. /**
  160. * Sets the length of the content body in the response In HTTP servlets,
  161. * this method sets the HTTP Content-Length header. For some portlet
  162. * responses, this method sets the content-length header, for others this
  163. * method does nothing.
  164. *
  165. * @param len
  166. * an integer specifying the length of the content being returned
  167. * to the client
  168. * @since 7.3.8
  169. */
  170. public void setContentLength(int len);
  171. /**
  172. * Gets the currently processed Vaadin response. The current response is
  173. * automatically defined when the request is started. The current response
  174. * can not be used in e.g. background threads because of the way server
  175. * implementations reuse response instances.
  176. *
  177. * @return the current Vaadin response instance if available, otherwise
  178. * <code>null</code>
  179. * @since 8.1
  180. */
  181. public static VaadinResponse getCurrent() {
  182. return CurrentInstance.get(VaadinResponse.class);
  183. }
  184. }