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;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.EndpointURI;
16  import org.mule.api.lifecycle.Callable;
17  import org.mule.api.transport.OutputHandler;
18  import org.mule.component.DefaultJavaComponent;
19  import org.mule.module.atom.server.MuleRequestContext;
20  import org.mule.object.SingletonObjectFactory;
21  import org.mule.transport.http.HttpConnector;
22  import org.mule.transport.http.HttpConstants;
23  import org.mule.util.StringUtils;
24  
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.util.Date;
28  import java.util.HashMap;
29  
30  import javax.activation.MimeType;
31  
32  import org.apache.abdera.Abdera;
33  import org.apache.abdera.i18n.iri.IRI;
34  import org.apache.abdera.protocol.server.FilterChain;
35  import org.apache.abdera.protocol.server.Provider;
36  import org.apache.abdera.protocol.server.RequestContext.Scope;
37  import org.apache.abdera.protocol.server.ResponseContext;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  /**
42   * This component receives requests from Mule and passes them off to Abdera.
43   */
44  public class AbderaServiceComponent extends DefaultJavaComponent
45  {
46      public static final String EVENT_CONTEXT = "_muleEventContext";
47  
48      private Provider provider;
49  
50      public AbderaServiceComponent()
51      {
52          super();
53  
54          setObjectFactory(new SingletonObjectFactory(new AbderaCallable(this)));
55      }
56  
57  
58      public static final class AbderaCallable implements Callable
59      {
60          protected static transient final Log logger = LogFactory.getLog(AbderaServiceComponent.class);
61  
62          private final AbderaServiceComponent abderaServiceComponent;
63  
64          public AbderaCallable(AbderaServiceComponent abderaServiceComponent)
65          {
66              this.abderaServiceComponent = abderaServiceComponent;
67          }
68  
69          public Object onCall(MuleEventContext event) throws MuleException
70          {
71              if (logger.isDebugEnabled())
72              {
73                  logger.debug(event.getMessageAsString());
74              }
75  
76              MuleMessage msg = event.getMessage();
77              IRI baseIri = initBaseUri(event.getEndpointURI());
78              String contextPath = msg.getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY, StringUtils.EMPTY);
79  
80              Provider provider = abderaServiceComponent.getProvider();
81              MuleRequestContext reqcontext = new MuleRequestContext(provider,
82                                                                     event,
83                                                                     msg,
84                                                                     contextPath,
85                                                                     baseIri);
86              reqcontext.setAttribute(Scope.REQUEST, EVENT_CONTEXT, event);
87  
88              FilterChain chain = new FilterChain(provider, reqcontext);
89  
90              try
91              {
92                  return output(msg, chain.next(reqcontext), event.getMuleContext());
93              }
94              catch (IOException e)
95              {
96                  throw new RuntimeException(e);
97              }
98          }
99  
100         private IRI initBaseUri(EndpointURI endpointURI)
101         {
102             String iri = endpointURI.toString();
103             if (!iri.endsWith("/"))
104             {
105                 iri = iri + "/";
106             }
107 
108             return new IRI(iri);
109         }
110 
111         private MuleMessage output(final MuleMessage request,
112                                    final ResponseContext context, final MuleContext muleContext) throws IOException
113         {
114             OutputHandler payload = new OutputHandler()
115             {
116 
117                 public void write(MuleEvent event, OutputStream out) throws IOException
118                 {
119                     if (context.hasEntity())
120                     {
121                         context.writeTo(out);
122                         out.flush();
123                     }
124                 }
125 
126             };
127             DefaultMuleMessage response = new DefaultMuleMessage(payload, muleContext);
128 
129             response.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, context.getStatus());
130             long cl = context.getContentLength();
131             String cc = context.getCacheControl();
132             if (cl > -1)
133             {
134                 response.setOutboundProperty("Content-Length", Long.toString(cl));
135             }
136             if (cc != null && cc.length() > 0)
137             {
138                 response.setOutboundProperty("Cache-Control", cc);
139             }
140 
141             MimeType ct = context.getContentType();
142             if (ct != null)
143             {
144                 response.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct.toString());
145             }
146 
147             String[] names = context.getHeaderNames();
148             for (String name : names)
149             {
150                 Object[] headers = context.getHeaders(name);
151                 for (Object value : headers)
152                 {
153                     if (value instanceof Date)
154                     {
155                         throw new RuntimeException();
156                     }
157 //                        response.setDateHeader(name, ((Date)value).getTime());
158                     else
159                     {
160                         response.setOutboundProperty(name, value.toString());
161                     }
162                 }
163             }
164 
165             return response;
166         }
167 
168     }
169 
170     public Provider getProvider()
171     {
172         return provider;
173     }
174 
175     public void setProvider(Provider provider)
176     {
177         this.provider = provider;
178         provider.init(Abdera.getInstance(), new HashMap<String, String>());
179     }
180 }