View Javadoc

1   /*
2    * $Id: RequestImpl.java 113 2004-10-22 19:22:56Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.command;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.Map;
29  import java.util.HashMap;
30  
31  import org.talika.tarsis.Globals;
32  import org.talika.tarsis.security.SecuritySession;
33  import org.talika.tarsis.security.User;
34  
35  /**
36   * Implementation of <code>Request</code> interface.
37   *
38   * @author  Jose M. Palomar
39   * @version $Revision: 113 $
40   * @see Request
41   */
42  public final class RequestImpl implements Request {
43  
44      // Fields
45      /**
46       * Source HttpServletRequest.
47       */
48      private final HttpServletRequest httpRequest;
49  
50      /**
51       * Attributes of request.
52       */
53      private final Map attributes;
54  
55      /**
56       * Parameters of request.
57       */
58      private final Map parameters;
59  
60      /**
61       * Session of request.
62       */
63      private Session session;
64  
65      // Constructors
66      /**
67       * Constructs a new <code>RequestImpl</code> object.
68       *
69       * @param request HttpServletRequest source HttpServletRequest.
70       */
71      public RequestImpl(HttpServletRequest request) {
72          this.httpRequest = request;
73  
74          this.attributes = new HashMap();
75          Enumeration attrNames = this.httpRequest.getAttributeNames();
76          while (attrNames.hasMoreElements()) {
77              String attrName = (String) attrNames.nextElement();
78              this.attributes.put(attrName, this.httpRequest.getAttribute(attrName));
79          }
80  
81          this.parameters = this.httpRequest.getParameterMap();
82  
83      }
84  
85      /**
86       * Returns the value of the especified attribute in request.
87       *
88       * @param name String name of attribute.
89       * @return Object value of attribute or <code>null</code> if not found.
90       * @see org.talika.tarsis.command.Request#getAttribute(String)
91       */
92      public Object getAttribute(String name) {
93          return attributes.get(name);
94      }
95  
96      /**
97       * Returns a map with all attributes in request.
98       *
99       * @return Map map with all attributes in request.
100      * @see org.talika.tarsis.command.Request#getAttributes()
101      */
102     public Map getAttributes() {
103         return Collections.unmodifiableMap(attributes);
104     }
105 
106     /**
107      * Returns the value of the specified parameter in request.
108      *
109      * @param name String name of parameter.
110      * @return Object value of parameter or <code>null</code> if not found.
111      * @see org.talika.tarsis.command.Request#getParameter(String)
112      */
113     public Object getParameter(String name) {
114 
115         if (parameters != null) {
116             return parameters.get(name);
117         }
118         else {
119             return null;
120         }
121 
122     }
123 
124     /**
125      * Returns a map with all parameters in request.
126      *
127      * @return Map map with all parameters in request.
128      * @see org.talika.tarsis.command.Request#getParameters()
129      */
130     public Map getParameters() {
131 
132         if (parameters != null) {
133             return parameters;
134         }
135         else {
136             return Collections.EMPTY_MAP;
137         }
138 
139     }
140 
141     /**
142      * Returns de user who maked the request.
143      *
144      * @return User user who made the request.
145      * @see org.talika.tarsis.command.Request#getRemoteUser()
146      */
147     public User getRemoteUser() {
148 
149         SecuritySession securitySession =
150             (SecuritySession) this.httpRequest.getAttribute(Globals.SECURITY_SESSION_ATTR);
151 
152         if (securitySession != null && securitySession.isValid()) {
153             return securitySession.getUser();
154         }
155         else {
156             return null;
157         }
158 
159     }
160 
161     /**
162      * Returns the session associated with request.
163      *
164      * @return Session session associated with request.
165      * @see org.talika.tarsis.command.Request#getSession()
166      */
167     public Session getSession() {
168         if (session == null) {
169             session = new SessionImpl(httpRequest.getSession());
170         }
171         return session;
172     }
173 
174 }