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.

VaadinPortletResponse.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.text.DateFormat;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.Locale;
  24. import java.util.TimeZone;
  25. import javax.portlet.MimeResponse;
  26. import javax.portlet.PortletResponse;
  27. import javax.portlet.ResourceResponse;
  28. import javax.servlet.http.Cookie;
  29. /**
  30. * Wrapper for {@link PortletResponse} and its subclasses.
  31. *
  32. * @author Vaadin Ltd.
  33. * @since 7.0
  34. *
  35. * @see VaadinResponse
  36. * @see VaadinPortletRequest
  37. */
  38. public class VaadinPortletResponse implements VaadinResponse {
  39. static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat(
  40. "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
  41. static {
  42. HTTP_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
  43. }
  44. private final PortletResponse response;
  45. private final VaadinPortletService vaadinService;
  46. /**
  47. * Wraps a portlet response and an associated vaadin service
  48. *
  49. * @param response
  50. * the portlet response to wrap
  51. * @param vaadinService
  52. * the associated vaadin service
  53. */
  54. public VaadinPortletResponse(PortletResponse response,
  55. VaadinPortletService vaadinService) {
  56. this.response = response;
  57. this.vaadinService = vaadinService;
  58. }
  59. @Override
  60. public OutputStream getOutputStream() throws IOException {
  61. if (response instanceof MimeResponse) {
  62. return ((MimeResponse) response).getPortletOutputStream();
  63. } else {
  64. throw new IOException(
  65. "Output stream not available for response of type "
  66. + response.getClass().getName());
  67. }
  68. }
  69. /**
  70. * Gets the original, unwrapped portlet response.
  71. *
  72. * @return the unwrapped portlet response
  73. */
  74. public PortletResponse getPortletResponse() {
  75. return response;
  76. }
  77. @Override
  78. public void setContentType(String type) {
  79. if (response instanceof MimeResponse) {
  80. ((MimeResponse) response).setContentType(type);
  81. } else {
  82. throw new RuntimeException(
  83. "Content type cannot be set for response of type "
  84. + response.getClass().getName());
  85. }
  86. }
  87. @Override
  88. public void setContentLength(int len) {
  89. if (response instanceof ResourceResponse) {
  90. ((ResourceResponse) response).setContentLength(len);
  91. }
  92. }
  93. @Override
  94. public PrintWriter getWriter() throws IOException {
  95. if (response instanceof MimeResponse) {
  96. return ((MimeResponse) response).getWriter();
  97. } else {
  98. throw new IOException("Writer not available for response of type "
  99. + response.getClass().getName());
  100. }
  101. }
  102. @Override
  103. public void setStatus(int responseStatus) {
  104. response.setProperty(ResourceResponse.HTTP_STATUS_CODE,
  105. Integer.toString(responseStatus));
  106. }
  107. @Override
  108. public void setHeader(String name, String value) {
  109. response.setProperty(name, value);
  110. }
  111. @Override
  112. public void setDateHeader(String name, long timestamp) {
  113. response.setProperty(name,
  114. HTTP_DATE_FORMAT.format(new Date(timestamp)));
  115. }
  116. @Override
  117. public void setCacheTime(long milliseconds) {
  118. VaadinServletResponse.doSetCacheTime(this, milliseconds);
  119. }
  120. @Override
  121. public void sendError(int errorCode, String message) throws IOException {
  122. setStatus(errorCode);
  123. if (message != null) {
  124. message = escapeHtml(message);
  125. }
  126. getWriter().write(message);
  127. }
  128. /**
  129. * Perform minimal HTML escaping similar to Guava HtmlEscapers.
  130. *
  131. * @param input
  132. * string to escape
  133. * @return minimally escaped HTML safe string
  134. */
  135. private static String escapeHtml(String input) {
  136. StringBuilder sb = new StringBuilder();
  137. for (int i = 0; i < input.length(); i++) {
  138. char c = input.charAt(i);
  139. switch (c) {
  140. case '"':
  141. sb.append("&quot;");
  142. break;
  143. case '\'':
  144. sb.append("&#39;");
  145. break;
  146. case '&':
  147. sb.append("&amp;");
  148. break;
  149. case '<':
  150. sb.append("&lt;");
  151. break;
  152. case '>':
  153. sb.append("&gt;");
  154. break;
  155. default:
  156. sb.append(c);
  157. }
  158. }
  159. return sb.toString();
  160. }
  161. @Override
  162. public VaadinPortletService getService() {
  163. return vaadinService;
  164. }
  165. @Override
  166. public void addCookie(Cookie cookie) {
  167. response.addProperty(cookie);
  168. }
  169. }