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.

VaadinServletResponse.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 javax.servlet.http.HttpServletResponse;
  18. import javax.servlet.http.HttpServletResponseWrapper;
  19. /**
  20. * Wrapper for {@link HttpServletResponse}.
  21. *
  22. * @author Vaadin Ltd.
  23. * @since 7.0
  24. *
  25. * @see VaadinResponse
  26. * @see VaadinServletRequest
  27. */
  28. public class VaadinServletResponse extends HttpServletResponseWrapper
  29. implements VaadinResponse {
  30. private final VaadinServletService vaadinService;
  31. /**
  32. * Wraps a http servlet response and an associated vaadin service
  33. *
  34. * @param response
  35. * the http servlet response to wrap
  36. * @param vaadinService
  37. * the associated vaadin service
  38. */
  39. public VaadinServletResponse(HttpServletResponse response,
  40. VaadinServletService vaadinService) {
  41. super(response);
  42. this.vaadinService = vaadinService;
  43. }
  44. /**
  45. * Gets the original unwrapped <code>HttpServletResponse</code>
  46. *
  47. * @return the unwrapped response
  48. */
  49. public HttpServletResponse getHttpServletResponse() {
  50. return this;
  51. }
  52. @Override
  53. public void setCacheTime(long milliseconds) {
  54. doSetCacheTime(this, milliseconds);
  55. }
  56. // Implementation shared with VaadinPortletResponse
  57. static void doSetCacheTime(VaadinResponse response, long milliseconds) {
  58. if (milliseconds <= 0) {
  59. response.setHeader("Cache-Control", "no-cache");
  60. response.setHeader("Pragma", "no-cache");
  61. response.setDateHeader("Expires", 0);
  62. } else {
  63. response.setHeader("Cache-Control",
  64. "max-age=" + milliseconds / 1000);
  65. response.setDateHeader("Expires",
  66. System.currentTimeMillis() + milliseconds);
  67. // Required to apply caching in some Tomcats
  68. response.setHeader("Pragma", "cache");
  69. }
  70. }
  71. @Override
  72. public VaadinServletService getService() {
  73. return vaadinService;
  74. }
  75. /**
  76. * Gets the currently processed Vaadin servlet response. The current
  77. * response is automatically defined when the request is started. The
  78. * current response can not be used in e.g. background threads because of
  79. * the way server implementations reuse response instances.
  80. *
  81. * @return the current Vaadin servlet response instance if available,
  82. * otherwise <code>null</code>
  83. * @since 8.1
  84. */
  85. public static VaadinServletResponse getCurrent() {
  86. VaadinResponse currentResponse = VaadinResponse.getCurrent();
  87. if (currentResponse instanceof VaadinServletResponse) {
  88. return (VaadinServletResponse) currentResponse;
  89. } else {
  90. return null;
  91. }
  92. }
  93. }