1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }