View Javadoc

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