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.

WrappedHttpSession.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.util.Collections;
  18. import java.util.Enumeration;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import javax.servlet.http.HttpSession;
  22. /**
  23. * Wrapper for {@link HttpSession}.
  24. *
  25. * @author Vaadin Ltd
  26. * @since 7.0.0
  27. * @see WrappedSession
  28. */
  29. public class WrappedHttpSession implements WrappedSession {
  30. private final HttpSession session;
  31. /**
  32. * Creates a new wrapped http session.
  33. *
  34. * @param session
  35. * the http session to wrap.
  36. */
  37. public WrappedHttpSession(HttpSession session) {
  38. this.session = session;
  39. }
  40. @Override
  41. public int getMaxInactiveInterval() {
  42. return session.getMaxInactiveInterval();
  43. }
  44. @Override
  45. public Object getAttribute(String name) {
  46. return session.getAttribute(name);
  47. }
  48. @Override
  49. public void setAttribute(String name, Object value) {
  50. session.setAttribute(name, value);
  51. }
  52. /**
  53. * Gets the wrapped {@link HttpSession}.
  54. *
  55. * @return the wrapped http session
  56. */
  57. public HttpSession getHttpSession() {
  58. return session;
  59. }
  60. @Override
  61. public Set<String> getAttributeNames() {
  62. Enumeration<String> attributeNames = session.getAttributeNames();
  63. return enumerationToSet(attributeNames);
  64. }
  65. // Helper shared with WrappedPortletSession
  66. static <T> Set<T> enumerationToSet(Enumeration<T> values) {
  67. HashSet<T> set = new HashSet<>();
  68. while (values.hasMoreElements()) {
  69. set.add(values.nextElement());
  70. }
  71. return Collections.unmodifiableSet(set);
  72. }
  73. @Override
  74. public void invalidate() {
  75. if (session == null) {
  76. throw new IllegalStateException(
  77. "Session is null and cannot be invalidated");
  78. }
  79. if (session.getClass().getName()
  80. .equals("org.atmosphere.util.FakeHttpSession")) {
  81. throw new UnsupportedOperationException(
  82. "FakeHttpSession cannot be invalidated. "
  83. + "This typically means you are using websockets together with Tomcat 7. "
  84. + "Because Tomcat 7 does not support sharing the HTTP session between standard HTTP requests and websockets, a copy of the session is used for websockets. "
  85. + "Invalidating this session does not have the desired effect. "
  86. + "To resolve this, upgrade to Tomcat 8 or use another transport mechanism than websockets.");
  87. }
  88. session.invalidate();
  89. }
  90. @Override
  91. public String getId() {
  92. return session.getId();
  93. }
  94. @Override
  95. public long getCreationTime() {
  96. return session.getCreationTime();
  97. }
  98. @Override
  99. public long getLastAccessedTime() {
  100. return session.getLastAccessedTime();
  101. }
  102. @Override
  103. public boolean isNew() {
  104. return session.isNew();
  105. }
  106. @Override
  107. public void removeAttribute(String name) {
  108. session.removeAttribute(name);
  109. }
  110. @Override
  111. public void setMaxInactiveInterval(int interval) {
  112. session.setMaxInactiveInterval(interval);
  113. }
  114. }