View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.javax.servlet;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpSession;
21  
22  import org.apache.commons.chain.web.AbstractSessionScopeMap;
23  import org.apache.commons.chain.web.MutableParameterMap;
24  
25  /**
26   * Private implementation of {@code Map} for HTTP session attributes.
27   *
28   * @author Craig R. McClanahan
29   * @version $Revision$ $Date$
30   */
31  final class ServletSessionScopeMap extends AbstractSessionScopeMap<HttpSession, HttpServletRequest> {
32  
33      /**
34       * The constructor for the servlet session attributes.
35       *
36       * @param request the servlet-request.
37       */
38      ServletSessionScopeMap(HttpServletRequest request) {
39          super(request);
40      }
41  
42      /**
43       * Returns the current {@code HttpSession} associated with this request or, if
44       * there is no current session and <code>create</code> is true, returns a new
45       * session.
46       *
47       * <p>If {@code create} is {@code false} and the request has no valid
48       * {@code HttpSession}, this method returns {@code null}.</p>
49       *
50       * <p>To make sure the session is properly maintained, you must call this
51       * method before the response is committed. If the container is using cookies
52       * to maintain session integrity and is asked to create a new session when the
53       * response is committed, an IllegalStateException is thrown.</p>
54       *
55       * @param create {@code true} to create a new session for this request if
56       *               necessary; {@code false} to return {@code null} if there's no
57       *               current session
58       *
59       * @return the {@code HttpSession} associated with this request or {@code null}
60       *         if {@code create} is {@code false} and the request has no valid
61       *         session
62       */
63      @Override
64      protected HttpSession getSession(final boolean create) {
65          return getRequest().getSession(create);
66      }
67  
68      /**
69       * Creates a new mutable-parameter-map to access the session-attributes.
70       *
71       * @return a new mutable-parameter-map to access the session-attributes
72       */
73      @Override
74      protected MutableParameterMap<HttpSession, Object> createParameterMap() {
75          return new MutableParameterMap<>(getSession(), getSession()::getAttribute,
76                  getSession()::getAttributeNames, getSession()::removeAttribute,
77                  getSession()::setAttribute);
78      }
79  }