View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * {@link WebRequest} interface implementation
40   *
41   * @author Daniel Sendula
42   */
43  public class DanubeRequest implements WebRequest {
44  
45      /** Reference connection */
46      protected HTTPConnection connection;
47  
48      /** Session manager */
49      protected HTTPSessionManager sessionManager;
50  
51      /**
52       * Constructor
53       * @param connection connection
54       * @param sessionManager session manager
55       */
56      public DanubeRequest(HTTPConnection connection, HTTPSessionManager sessionManager) {
57          this.connection = connection;
58          this.sessionManager = sessionManager;
59      }
60  
61      /**
62       * Returns parameter names as a list
63       * @return parameter names as a list
64       */
65      @SuppressWarnings("unchecked")
66      public List getParameterNames() {
67          return new ArrayList<String>(connection.getRequestParameters().keySet());
68      }
69  
70      /**
71       * Returns parameter value
72       * @param name parameter's name
73       * @return parameter value
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       * Returns parameter's values
86       * @param name parameter's name
87       * @return array of attribute's values
88       */
89      public String[] getParameterValues(String name) {
90          return connection.getRequestParameters().getAsArray(name);
91      }
92  
93      /**
94       * Returns context path
95       * @return context path
96       */
97      public String getContextPath() {
98          return connection.getContextPath();
99      }
100 
101     /**
102      * Returns session
103      * @param create if <code>true</code> new session is going to be create if it doesn't exist already
104      * @return web sesison
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      * Returns <code>HTTP</code>
123      * @return scheme
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      * Returns server's name
135      * @return server's name
136      */
137     public String getServerName() {
138 //      TODO ???
139         return ((Socket)connection.adapt(Socket.class)).getLocalAddress().getHostName();
140     }
141 
142     /**
143      * Returns port request is served from. That is server's port.
144      * @return server's port
145      */
146     public int getServerPort() {
147         return ((Socket)connection.adapt(Socket.class)).getLocalPort();
148     }
149 
150     /**
151      * Returns request URI
152      * @return request URI
153      */
154     public String getRequestURI() {
155         return connection.getRequestURI();
156     }
157 
158     /**
159      * Forwards internally to given URL
160      * @param url URI to be forwarded to
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      * Returns component's path
193      * @return component's path
194      */
195     public String getActivationPath() {
196         return connection.getComponentPath();
197     }
198 
199     /**
200      * Returns path info
201      * @return path info
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      * Returns locale
221      * @return locale
222      */
223     public Locale getLocale() {
224         return Locale.getDefault(); // TODO
225     }
226 
227     /**
228      * Returns header's value
229      * @param name headers name
230      * @return header's value
231      */
232     public String getHeader(String name) {
233         return (String)connection.getRequestHeaders().getOnly(name);
234     }
235 
236     /**
237      * Returns remote user
238      * @return remote user
239      */
240     public String getRemoteUser() {
241         return null; // TODO
242     }
243 
244     /**
245      * Returns user principal
246      * @return user principal
247      */
248     public Principal getUserPrincipal() {
249         return null; // TODO
250     }
251 
252     /**
253      * Checks if user is in role
254      * @param role role
255      * @return <code>true</code> if user is in role
256      */
257     public boolean isUserInRole(String role) {
258         return false; // TODO
259     }
260 
261     /**
262      * Returns attribute names
263      * @return attribute names as a list
264      */
265     @SuppressWarnings("unchecked")
266     public List getAttributeNames() {
267         return new ArrayList<Object>(connection.getAttributes().keySet());
268     }
269 
270     /**
271      * Returns attribute value
272      * @param name attribute's name
273      * @return attribute's value
274      */
275     public Object getAttribute(String name) {
276         return connection.getAttributes().get(name);
277     }
278 
279     /**
280      * Sets attribute
281      * @param name attribute's name
282      * @param value attribute's value
283      */
284     public void setAttribute(String name, Object value) {
285         connection.getAttributes().put(name, value);
286     }
287 
288     /**
289      * Does nothing
290      */
291     public void describeTo(DescriptionReceiver descriptionReceiver) {
292     }
293 
294 }