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