View Javadoc

1   /*
2    * $Id: MuleRequestContext.java 22409 2011-07-14 05:14:27Z dirk.olmes $
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      @Override
76      public Locale getPreferredLocale()
77      {
78          return null;
79      }
80  
81      @Override
82      public Locale[] getPreferredLocales()
83      {
84          return null;
85      }
86  
87      @Override
88      public String getTargetBasePath()
89      {
90          return event.getEndpointURI().getPath();
91      }
92  
93      @Override
94      public Object getProperty(Property property)
95      {
96          switch (property)
97          {
98              case SESSIONID:
99                  return (session != null) ? session.getId() : null;
100             case SESSIONCREATED:
101                 return (session != null) ? new Date(session.getCreationTime()) : null;
102             case SESSIONACCESSED:
103                 return (session != null) ? new Date(session.getLastAccessedTime()) : null;
104             case SESSIONTIMEOUT:
105                 return (session != null) ? session.getMaxInactiveInterval() : -1;
106             case CHARACTERENCODING:
107                 return request.getEncoding();
108             case LOCALES:
109                 return null;
110             case PROTOCOL:
111                 return request.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY);
112             case REMOTEADDRESS:
113                 return null;
114             case REMOTEHOST:
115                 return baseIri.getHost();
116             case REMOTEUSER:
117                 return baseIri.getUserInfo();
118             case SCHEME:
119                 return baseIri.getScheme();
120             case PRINCIPAL:
121                 return null; // TODO
122             case AUTHTYPE:
123                 return null; // TODO
124             case CONTENTLENGTH:
125                 return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH);
126             case CONTENTTYPE:
127                 return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
128             case CONTEXTPATH:
129                 return ""; // TODO
130             case LOCALADDR:
131                 return ""; // TODO
132             case LOCALNAME:
133                 return ""; // TODO
134             case SERVERNAME:
135                 return ""; // TODO
136             case SERVERPORT:
137                 return ""; // TODO
138             default:
139                 throw new UnsupportedOperationException("Property " + property.name() + " is not supported.");
140         }
141     }
142 
143     @Override
144     public Reader getReader() throws IOException
145     {
146         Object payload = request.getPayload();
147         if (payload instanceof Reader)
148         {
149             return (Reader) payload;
150         }
151         else if (payload instanceof InputStream)
152         {
153             return new InputStreamReader((InputStream) payload);
154         }
155         else if (payload instanceof byte[])
156         {
157             return new InputStreamReader(new ByteArrayInputStream((byte[]) payload));
158         }
159         else
160         {
161             try
162             {
163                 return new StringReader(request.getPayloadAsString());
164             }
165             catch (Exception e)
166             {
167                 IOException e2 = new IOException("Could not convert message to String.");
168                 e2.initCause(e);
169                 throw e2;
170             }
171         }
172     }
173 
174     @Override
175     public InputStream getInputStream() throws IOException
176     {
177         Object payload = request.getPayload();
178         if (payload instanceof InputStream)
179         {
180             return (InputStream) payload;
181         }
182         else if (payload instanceof byte[])
183         {
184             return new ByteArrayInputStream((byte[]) payload);
185         }
186         else
187         {
188             try
189             {
190                 return new ByteArrayInputStream(request.getPayloadAsString().getBytes());
191             }
192             catch (Exception e)
193             {
194                 IOException e2 = new IOException("Could not convert message to String.");
195                 e2.initCause(e);
196                 throw e2;
197             }
198         }
199     }
200 
201     @Override
202     public RequestContext setAttribute(Scope scope, String name, Object value)
203     {
204         switch (scope)
205         {
206             case REQUEST:
207                 // note it's not a usual Mule property scope, abdera puts and checks for things here
208                 request.setProperty(name, value, PropertyScope.INBOUND);
209                 break;
210             case SESSION:
211                 event.getSession().setProperty(name, value);
212                 break;
213         }
214         return this;
215     }
216 
217     @Override
218     public Object getAttribute(Scope scope, String name)
219     {
220         switch (scope)
221         {
222             case REQUEST:
223                 return request.getInboundProperty(name);
224             case SESSION:
225                 if (event.getSession() != null)
226                 {
227                     return event.getSession().getProperty(name);
228                 }
229         }
230         return null;
231     }
232 
233     @Override
234     public String[] getAttributeNames(Scope scope)
235     {
236         switch (scope)
237         {
238             case REQUEST:
239                 Set<String> names = request.getPropertyNames();
240                 return names.toArray(new String[names.size()]);
241             case SESSION:
242                 return new String[0];
243         }
244         return null;
245     }
246 
247     @Override
248     public String getParameter(String name)
249     {
250         return null;
251     }
252 
253     @Override
254     public String[] getParameterNames()
255     {
256         return new String[0];
257     }
258 
259     @Override
260     public List<String> getParameters(String name)
261     {
262         return Collections.emptyList();
263     }
264 
265     @Override
266     public Date getDateHeader(String name)
267     {
268 //        long value = request.getDateHeader(name);
269 //        return value != -1 ? new Date(value) : null;
270         throw new UnsupportedOperationException();
271     }
272 
273     @Override
274     public String getHeader(String name)
275     {
276         Object prop = request.getInboundProperty(name);
277         if (prop == null)
278         {
279             return null;
280         }
281         return prop.toString();
282     }
283 
284     @Override
285     public String[] getHeaderNames()
286     {
287         Set<String> propNames = request.getPropertyNames();
288         return propNames.toArray(new String[propNames.size()]);
289     }
290 
291     @Override
292     public Object[] getHeaders(String name)
293     {
294         List<String> values = new ArrayList<String>();
295         Set<String> propNames = request.getPropertyNames();
296 
297         for (String n : propNames)
298         {
299             Object prop = request.getProperty(n);
300             if (prop instanceof String)
301             {
302                 values.add((String) prop);
303             }
304         }
305         return values.toArray();
306     }
307 //
308 //    private static String getHost(ServiceContext context, HttpServletRequest request) {
309 //        Abdera abdera = context.getAbdera();
310 //        String host = abdera.getConfiguration()
311 //            .getConfigurationOption("org.apache.abdera.protocol.server.Host");
312 //        return (host != null) ? host : request.getServerName();
313 //    }
314 //
315 //    private static int getPort(ServiceContext context, HttpServletRequest request) {
316 //        Abdera abdera = context.getAbdera();
317 //        String port = abdera.getConfiguration()
318 //            .getConfigurationOption("org.apache.abdera.protocol.server.Port");
319 //        return (port != null) ? Integer.parseInt(port) : request.getServerPort();
320 //    }
321 
322     @Override
323     public boolean isUserInRole(String role)
324     {
325         return false; // TODO
326     }
327 
328     @Override
329     public String getContextPath()
330     {
331         return event.getEndpointURI().getPath();
332     }
333 }