View Javadoc

1   /*
2    * $Id: MuleRequestContext.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom.server;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.PropertyScope;
15  import org.mule.transport.http.HttpConnector;
16  import org.mule.transport.http.HttpConstants;
17  import org.mule.util.StringUtils;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.Reader;
24  import java.io.StringReader;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Date;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.Set;
31  
32  import javax.servlet.http.HttpSession;
33  
34  import org.apache.abdera.i18n.iri.IRI;
35  import org.apache.abdera.protocol.server.Provider;
36  import org.apache.abdera.protocol.server.RequestContext;
37  import org.apache.abdera.protocol.server.context.AbstractRequestContext;
38  
39  public class MuleRequestContext extends AbstractRequestContext implements RequestContext
40  {
41  
42      private final MuleMessage request;
43      private HttpSession session;
44      private MuleEventContext event;
45      private IRI baseIri;
46  
47      public MuleRequestContext(Provider context,
48                                MuleEventContext event,
49                                MuleMessage request,
50                                String contextPath,
51                                IRI baseIri)
52      {
53          super(context,
54                  getMethod(request),
55                  new IRI(contextPath),
56                  baseIri);
57  
58          this.baseIri = baseIri;
59          this.request = request;
60          this.event = event;
61          // TODO: Sessions?
62          this.session = null; // request.getSession(false);
63  
64          // TODO: Principals?
65          principal = null; // request.getUserPrincipal();
66          subject = context.resolveSubject(this);
67          target = context.resolveTarget(this);
68      }
69  
70      private static String getMethod(MuleMessage request)
71      {
72          return request.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, StringUtils.EMPTY);
73      }
74  
75      public Locale getPreferredLocale()
76      {
77          return null;
78      }
79  
80      public Locale[] getPreferredLocales()
81      {
82          return null;
83      }
84  
85      public String getTargetBasePath()
86      {
87          return event.getEndpointURI().getPath();
88      }
89  
90      public Object getProperty(Property property)
91      {
92          switch (property)
93          {
94              case SESSIONID:
95                  return (session != null) ? session.getId() : null;
96              case SESSIONCREATED:
97                  return (session != null) ? new Date(session.getCreationTime()) : null;
98              case SESSIONACCESSED:
99                  return (session != null) ? new Date(session.getLastAccessedTime()) : null;
100             case SESSIONTIMEOUT:
101                 return (session != null) ? session.getMaxInactiveInterval() : -1;
102             case CHARACTERENCODING:
103                 return request.getEncoding();
104             case LOCALES:
105                 return null;
106             case PROTOCOL:
107                 return request.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY);
108             case REMOTEADDRESS:
109                 return null;
110             case REMOTEHOST:
111                 return baseIri.getHost();
112             case REMOTEUSER:
113                 return baseIri.getUserInfo();
114             case SCHEME:
115                 return baseIri.getScheme();
116             case PRINCIPAL:
117                 return null; // TODO
118             case AUTHTYPE:
119                 return null; // TODO
120             case CONTENTLENGTH:
121                 return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH);
122             case CONTENTTYPE:
123                 return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
124             case CONTEXTPATH:
125                 return ""; // TODO
126             case LOCALADDR:
127                 return ""; // TODO
128             case LOCALNAME:
129                 return ""; // TODO
130             case SERVERNAME:
131                 return ""; // TODO
132             case SERVERPORT:
133                 return ""; // TODO
134             default:
135                 throw new UnsupportedOperationException("Property " + property.name() + " is not supported.");
136         }
137     }
138 
139     public Reader getReader() throws IOException
140     {
141         Object payload = request.getPayload();
142         if (payload instanceof Reader)
143         {
144             return (Reader) payload;
145         }
146         else if (payload instanceof InputStream)
147         {
148             return new InputStreamReader((InputStream) payload);
149         }
150         else if (payload instanceof byte[])
151         {
152             return new InputStreamReader(new ByteArrayInputStream((byte[]) payload));
153         }
154         else
155         {
156             try
157             {
158                 return new StringReader(request.getPayloadAsString());
159             }
160             catch (Exception e)
161             {
162                 IOException e2 = new IOException("Could not convert message to String.");
163                 e2.initCause(e);
164                 throw e2;
165             }
166         }
167     }
168 
169     public InputStream getInputStream() throws IOException
170     {
171         Object payload = request.getPayload();
172         if (payload instanceof InputStream)
173         {
174             return (InputStream) payload;
175         }
176         else if (payload instanceof byte[])
177         {
178             return new ByteArrayInputStream((byte[]) payload);
179         }
180         else
181         {
182             try
183             {
184                 return new ByteArrayInputStream(request.getPayloadAsString().getBytes());
185             }
186             catch (Exception e)
187             {
188                 IOException e2 = new IOException("Could not convert message to String.");
189                 e2.initCause(e);
190                 throw e2;
191             }
192         }
193     }
194 
195     public RequestContext setAttribute(Scope scope, String name, Object value)
196     {
197         switch (scope)
198         {
199             case REQUEST:
200                 // note it's not a usual Mule property scope, abdera puts and checks for things here
201                 request.setProperty(name, value, PropertyScope.INBOUND);
202                 break;
203             case SESSION:
204                 event.getSession().setProperty(name, value);
205                 break;
206         }
207         return this;
208     }
209 
210     public Object getAttribute(Scope scope, String name)
211     {
212         switch (scope)
213         {
214             case REQUEST:
215                 return request.getInboundProperty(name);
216             case SESSION:
217                 if (event.getSession() != null)
218                 {
219                     return event.getSession().getProperty(name);
220                 }
221         }
222         return null;
223     }
224 
225     @SuppressWarnings("unchecked")
226     public String[] getAttributeNames(Scope scope)
227     {
228         switch (scope)
229         {
230             case REQUEST:
231                 Set names = request.getPropertyNames();
232                 return (String[]) names.toArray(new String[names.size()]);
233             case SESSION:
234                 return new String[0];
235         }
236         return null;
237     }
238 
239     public String getParameter(String name)
240     {
241         return null;
242     }
243 
244     @SuppressWarnings("unchecked")
245     public String[] getParameterNames()
246     {
247         return new String[0];
248     }
249 
250     public List<String> getParameters(String name)
251     {
252         return Collections.emptyList();
253     }
254 
255     public Date getDateHeader(String name)
256     {
257 //        long value = request.getDateHeader(name);
258 //        return value != -1 ? new Date(value) : null;
259         throw new UnsupportedOperationException();
260     }
261 
262     public String getHeader(String name)
263     {
264         Object prop = request.getInboundProperty(name);
265         if (prop == null)
266         {
267             return null;
268         }
269         return prop.toString();
270     }
271 
272     @SuppressWarnings("unchecked")
273     public String[] getHeaderNames()
274     {
275         Set propNames = request.getPropertyNames();
276         return (String[]) propNames.toArray(new String[propNames.size()]);
277     }
278 
279     @SuppressWarnings("unchecked")
280     public Object[] getHeaders(String name)
281     {
282         List<String> values = new ArrayList<String>();
283         Set propNames = request.getPropertyNames();
284 
285         for (Object n : propNames)
286         {
287             Object prop = request.getProperty((String) n);
288             if (prop instanceof String)
289             {
290                 values.add((String) prop);
291             }
292         }
293         return values.toArray();
294     }
295 //    
296 //    private static String getHost(ServiceContext context, HttpServletRequest request) {
297 //        Abdera abdera = context.getAbdera();
298 //        String host = abdera.getConfiguration()
299 //            .getConfigurationOption("org.apache.abdera.protocol.server.Host");
300 //        return (host != null) ? host : request.getServerName();
301 //    }
302 //
303 //    private static int getPort(ServiceContext context, HttpServletRequest request) {
304 //        Abdera abdera = context.getAbdera();
305 //        String port = abdera.getConfiguration()
306 //            .getConfigurationOption("org.apache.abdera.protocol.server.Port");
307 //        return (port != null) ? Integer.parseInt(port) : request.getServerPort();
308 //    }
309 
310     public boolean isUserInRole(String role)
311     {
312         return false; // TODO
313     }
314 
315     public String getContextPath()
316     {
317         return event.getEndpointURI().getPath();
318     }
319 }