1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
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 | 0 | super(); |
53 | |
|
54 | 0 | setObjectFactory(new SingletonObjectFactory(new AbderaCallable(this))); |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
public static final class AbderaCallable implements Callable |
59 | |
{ |
60 | 0 | protected static transient final Log logger = LogFactory.getLog(AbderaServiceComponent.class); |
61 | |
|
62 | |
private final AbderaServiceComponent abderaServiceComponent; |
63 | |
|
64 | |
public AbderaCallable(AbderaServiceComponent abderaServiceComponent) |
65 | 0 | { |
66 | 0 | this.abderaServiceComponent = abderaServiceComponent; |
67 | 0 | } |
68 | |
|
69 | |
public Object onCall(MuleEventContext event) throws MuleException |
70 | |
{ |
71 | 0 | if (logger.isDebugEnabled()) |
72 | |
{ |
73 | 0 | logger.debug(event.getMessageAsString()); |
74 | |
} |
75 | |
|
76 | 0 | MuleMessage msg = event.getMessage(); |
77 | 0 | IRI baseIri = initBaseUri(event.getEndpointURI()); |
78 | 0 | String contextPath = msg.getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY, StringUtils.EMPTY); |
79 | |
|
80 | 0 | Provider provider = abderaServiceComponent.getProvider(); |
81 | 0 | MuleRequestContext reqcontext = new MuleRequestContext(provider, |
82 | |
event, |
83 | |
msg, |
84 | |
contextPath, |
85 | |
baseIri); |
86 | 0 | reqcontext.setAttribute(Scope.REQUEST, EVENT_CONTEXT, event); |
87 | |
|
88 | 0 | FilterChain chain = new FilterChain(provider, reqcontext); |
89 | |
|
90 | |
try |
91 | |
{ |
92 | 0 | return output(msg, chain.next(reqcontext), event.getMuleContext()); |
93 | |
} |
94 | 0 | catch (IOException e) |
95 | |
{ |
96 | 0 | throw new RuntimeException(e); |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
private IRI initBaseUri(EndpointURI endpointURI) |
101 | |
{ |
102 | 0 | String iri = endpointURI.toString(); |
103 | 0 | if (!iri.endsWith("/")) |
104 | |
{ |
105 | 0 | iri = iri + "/"; |
106 | |
} |
107 | |
|
108 | 0 | return new IRI(iri); |
109 | |
} |
110 | |
|
111 | |
private MuleMessage output(final MuleMessage request, |
112 | |
final ResponseContext context, final MuleContext muleContext) throws IOException |
113 | |
{ |
114 | 0 | OutputHandler payload = new OutputHandler() |
115 | 0 | { |
116 | |
|
117 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
118 | |
{ |
119 | 0 | if (context.hasEntity()) |
120 | |
{ |
121 | 0 | context.writeTo(out); |
122 | 0 | out.flush(); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
}; |
127 | 0 | DefaultMuleMessage response = new DefaultMuleMessage(payload, muleContext); |
128 | |
|
129 | 0 | response.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, context.getStatus()); |
130 | 0 | long cl = context.getContentLength(); |
131 | 0 | String cc = context.getCacheControl(); |
132 | 0 | if (cl > -1) |
133 | |
{ |
134 | 0 | response.setOutboundProperty("Content-Length", Long.toString(cl)); |
135 | |
} |
136 | 0 | if (cc != null && cc.length() > 0) |
137 | |
{ |
138 | 0 | response.setOutboundProperty("Cache-Control", cc); |
139 | |
} |
140 | |
|
141 | 0 | MimeType ct = context.getContentType(); |
142 | 0 | if (ct != null) |
143 | |
{ |
144 | 0 | response.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct.toString()); |
145 | |
} |
146 | |
|
147 | 0 | String[] names = context.getHeaderNames(); |
148 | 0 | for (String name : names) |
149 | |
{ |
150 | 0 | Object[] headers = context.getHeaders(name); |
151 | 0 | for (Object value : headers) |
152 | |
{ |
153 | 0 | if (value instanceof Date) |
154 | |
{ |
155 | 0 | throw new RuntimeException(); |
156 | |
} |
157 | |
|
158 | |
else |
159 | |
{ |
160 | 0 | response.setOutboundProperty(name, value.toString()); |
161 | |
} |
162 | |
} |
163 | |
} |
164 | |
|
165 | 0 | return response; |
166 | |
} |
167 | |
|
168 | |
} |
169 | |
|
170 | |
public Provider getProvider() |
171 | |
{ |
172 | 0 | return provider; |
173 | |
} |
174 | |
|
175 | |
public void setProvider(Provider provider) |
176 | |
{ |
177 | 0 | this.provider = provider; |
178 | 0 | provider.init(Abdera.getInstance(), new HashMap<String, String>()); |
179 | 0 | } |
180 | |
} |