Coverage Report - org.apache.tiles.request.servlet.extractor.SessionScopeExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
SessionScopeExtractor
100%
17/17
83%
5/6
2
 
 1  
 /*
 2  
  * $Id: SessionScopeExtractor.java 1199216 2011-11-08 12:25:24Z mck $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 package org.apache.tiles.request.servlet.extractor;
 22  
 
 23  
 import java.util.Enumeration;
 24  
 import java.util.Collections;
 25  
 
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 import javax.servlet.http.HttpSession;
 28  
 
 29  
 import org.apache.tiles.request.attribute.AttributeExtractor;
 30  
 
 31  
 /**
 32  
  * Extract attributes from session scope.
 33  
  *
 34  
  * @version $Rev: 1199216 $ $Date: 2011-11-08 23:25:24 +1100 (Tue, 08 Nov 2011) $
 35  
  */
 36  
 public class SessionScopeExtractor implements AttributeExtractor {
 37  
 
 38  
     /**
 39  
      * The servlet request.
 40  
      */
 41  
     private HttpServletRequest request;
 42  
 
 43  
     /**
 44  
      * Constructor.
 45  
      *
 46  
      * @param request The servlet request.
 47  
      */
 48  7
     public SessionScopeExtractor(HttpServletRequest request) {
 49  7
         this.request = request;
 50  7
     }
 51  
 
 52  
     @Override
 53  
     public void setValue(String name, Object value) {
 54  1
         request.getSession().setAttribute(name, value);
 55  1
     }
 56  
 
 57  
     @Override
 58  
     public void removeValue(String name) {
 59  1
         HttpSession session = request.getSession(false);
 60  1
         if (session != null) {
 61  1
             session.removeAttribute(name);
 62  
         }
 63  1
     }
 64  
 
 65  
     @SuppressWarnings("unchecked")
 66  
     @Override
 67  
     public Enumeration<String> getKeys() {
 68  2
         HttpSession session = request.getSession(false);
 69  2
         if (session != null) {
 70  1
             return session.getAttributeNames();
 71  
         }
 72  1
         return Collections.enumeration(Collections.<String>emptySet());
 73  
     }
 74  
 
 75  
     @Override
 76  
     public Object getValue(String key) {
 77  2
         HttpSession session = request.getSession(false);
 78  2
         if (session != null) {
 79  1
             return session.getAttribute(key);
 80  
         }
 81  1
         return null;
 82  
     }
 83  
 }