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