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