View Javadoc
1   /*
2    * Copyright 2023 Web-Legacy
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.tiles.request.jakarta.servlet.extractor;
17  
18  import java.util.Collections;
19  import java.util.Enumeration;
20  
21  import org.apache.tiles.request.attribute.AttributeExtractor;
22  
23  import jakarta.servlet.http.HttpServletRequest;
24  import jakarta.servlet.http.HttpSession;
25  
26  /**
27   * Extract attributes from session scope.
28   *
29   * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
30   * Jakarta EE 9.</p>
31   */
32  public class SessionScopeExtractor implements AttributeExtractor {
33  
34      /**
35       * The servlet request.
36       */
37      private HttpServletRequest request;
38  
39      /**
40       * Constructor.
41       *
42       * @param request The servlet request.
43       */
44      public SessionScopeExtractor(HttpServletRequest request) {
45          this.request = request;
46      }
47  
48      /**
49       * Sets a value for the given key.
50       *
51       * @param name  The key of the attribute.
52       * @param value The value of the attribute.
53       */
54      @Override
55      public void setValue(String name, Object value) {
56          request.getSession().setAttribute(name, value);
57      }
58  
59      /**
60       * Removes an attribute.
61       *
62       * @param name The key of the attribute to remove.
63       */
64      @Override
65      public void removeValue(String name) {
66          HttpSession session = request.getSession(false);
67          if (session != null) {
68              session.removeAttribute(name);
69          }
70      }
71  
72      /**
73       * The enumeration of the keys in the stored attributes.
74       *
75       * @return The keys.
76       */
77      @Override
78      public Enumeration<String> getKeys() {
79          HttpSession session = request.getSession(false);
80          if (session != null) {
81              return session.getAttributeNames();
82          }
83  
84          return Collections.enumeration(Collections.<String>emptySet());
85      }
86  
87      /**
88       * Returns the value of the attribute with the given key.
89       *
90       * @param key The key of the attribute.
91       *
92       * @return The value.
93       */
94      @Override
95      public Object getValue(String key) {
96          HttpSession session = request.getSession(false);
97          if (session != null) {
98              return session.getAttribute(key);
99          }
100 
101         return null;
102     }
103 }