1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.abstracthorizon.danube.tapestry;
15
16 import java.net.InetSocketAddress;
17 import java.net.MalformedURLException;
18 import java.net.Socket;
19 import java.net.SocketAddress;
20 import java.net.URL;
21 import java.security.Principal;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Locale;
25
26 import javax.net.ssl.SSLSocket;
27
28 import org.abstracthorizon.danube.connection.ConnectionException;
29 import org.abstracthorizon.danube.http.HTTPConnection;
30 import org.abstracthorizon.danube.http.Status;
31 import org.abstracthorizon.danube.http.session.HTTPSessionManager;
32 import org.abstracthorizon.danube.http.session.Session;
33
34 import org.apache.tapestry.describe.DescriptionReceiver;
35 import org.apache.tapestry.web.WebRequest;
36 import org.apache.tapestry.web.WebSession;
37
38
39
40
41
42
43 public class DanubeRequest implements WebRequest {
44
45
46 protected HTTPConnection connection;
47
48
49 protected HTTPSessionManager sessionManager;
50
51
52
53
54
55
56 public DanubeRequest(HTTPConnection connection, HTTPSessionManager sessionManager) {
57 this.connection = connection;
58 this.sessionManager = sessionManager;
59 }
60
61
62
63
64
65 @SuppressWarnings("unchecked")
66 public List getParameterNames() {
67 return new ArrayList<String>(connection.getRequestParameters().keySet());
68 }
69
70
71
72
73
74
75 public String getParameterValue(String name) {
76 String[] values = getParameterValues(name);
77 if ((values != null) && (values.length > 0)) {
78 return values[0];
79 } else {
80 return null;
81 }
82 }
83
84
85
86
87
88
89 public String[] getParameterValues(String name) {
90 return connection.getRequestParameters().getAsArray(name);
91 }
92
93
94
95
96
97 public String getContextPath() {
98 return connection.getContextPath();
99 }
100
101
102
103
104
105
106 public WebSession getSession(boolean create) {
107 Session session = (Session)sessionManager.findSession(connection, create);
108 if (session != null) {
109 DanubeSession danubeSession = (DanubeSession)session.getAttributes().get(DanubeSession.SESSION_ATTRIBUTE);
110 if (danubeSession == null) {
111 danubeSession = new DanubeSession(session);
112 } else {
113 danubeSession.clearNew();
114 }
115 return danubeSession;
116 } else {
117 return null;
118 }
119 }
120
121
122
123
124
125 public String getScheme() {
126 String scheme = "HTTP";
127 if ((Socket)connection.adapt(Socket.class) instanceof SSLSocket) {
128 scheme = "HTTPS";
129 }
130 return scheme;
131 }
132
133
134
135
136
137 public String getServerName() {
138
139 return ((Socket)connection.adapt(Socket.class)).getLocalAddress().getHostName();
140 }
141
142
143
144
145
146 public int getServerPort() {
147 return ((Socket)connection.adapt(Socket.class)).getLocalPort();
148 }
149
150
151
152
153
154 public String getRequestURI() {
155 return connection.getRequestURI();
156 }
157
158
159
160
161
162 public void forward(String url) {
163 try {
164 InetSocketAddress localAddressSocket = (InetSocketAddress)((Socket)connection.adapt(Socket.class)).getLocalSocketAddress();
165
166 URL u = new URL(url);
167
168 int port = u.getPort();
169 if (port <= 0) {
170 port = localAddressSocket.getPort();
171 }
172
173 SocketAddress redirectSocketAddress = new InetSocketAddress(u.getHost(), u.getPort());
174
175 if (u.getProtocol().equals(connection.getRequestProtocol())
176 && localAddressSocket.equals(redirectSocketAddress)) {
177 connection.forward(u.getFile());
178 } else {
179 if (connection.getRequestProtocol().equals("HTTP/1.1")) {
180 connection.setResponseStatus(Status.SEE_OTHER);
181 } else {
182 connection.setResponseStatus(Status.FOUND);
183 }
184 connection.getResponseHeaders().add("Location", url.toString());
185 }
186 } catch (MalformedURLException e) {
187 throw new ConnectionException(e);
188 }
189 }
190
191
192
193
194
195 public String getActivationPath() {
196 return connection.getComponentPath();
197 }
198
199
200
201
202
203 public String getPathInfo() {
204 String path = connection.getComponentResourcePath();
205 if (path == null) {
206 return path;
207 } else {
208 int i = path.indexOf('?');
209 if (i >= 0) {
210 path = path.substring(0, i);
211 }
212 if (path.startsWith("/")) {
213 path = path.substring(1);
214 }
215 return path;
216 }
217 }
218
219
220
221
222
223 public Locale getLocale() {
224 return Locale.getDefault();
225 }
226
227
228
229
230
231
232 public String getHeader(String name) {
233 return (String)connection.getRequestHeaders().getOnly(name);
234 }
235
236
237
238
239
240 public String getRemoteUser() {
241 return null;
242 }
243
244
245
246
247
248 public Principal getUserPrincipal() {
249 return null;
250 }
251
252
253
254
255
256
257 public boolean isUserInRole(String role) {
258 return false;
259 }
260
261
262
263
264
265 @SuppressWarnings("unchecked")
266 public List getAttributeNames() {
267 return new ArrayList<Object>(connection.getAttributes().keySet());
268 }
269
270
271
272
273
274
275 public Object getAttribute(String name) {
276 return connection.getAttributes().get(name);
277 }
278
279
280
281
282
283
284 public void setAttribute(String name, Object value) {
285 connection.getAttributes().put(name, value);
286 }
287
288
289
290
291 public void describeTo(DescriptionReceiver descriptionReceiver) {
292 }
293
294 }