1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jersey; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.component.JavaComponent; |
12 | |
import org.mule.api.endpoint.EndpointURI; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.processor.MessageProcessor; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.component.AbstractComponent; |
17 | |
import org.mule.transport.http.HttpConnector; |
18 | |
|
19 | |
import com.sun.jersey.api.core.DefaultResourceConfig; |
20 | |
import com.sun.jersey.core.header.InBoundHeaders; |
21 | |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory; |
22 | |
import com.sun.jersey.spi.container.ContainerRequest; |
23 | |
import com.sun.jersey.spi.container.ContainerResponse; |
24 | |
import com.sun.jersey.spi.container.WebApplication; |
25 | |
import com.sun.jersey.spi.container.WebApplicationFactory; |
26 | |
|
27 | |
import java.io.InputStream; |
28 | |
import java.net.URI; |
29 | |
import java.net.URISyntaxException; |
30 | |
import java.util.ArrayList; |
31 | |
import java.util.HashSet; |
32 | |
import java.util.List; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
import javax.ws.rs.ext.ExceptionMapper; |
36 | |
|
37 | |
import org.apache.commons.logging.Log; |
38 | |
import org.apache.commons.logging.LogFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class JerseyResourcesComponent extends AbstractComponent |
46 | |
{ |
47 | 0 | public static String JERSEY_RESPONSE = "jersey_response"; |
48 | |
|
49 | 0 | protected final Log logger = LogFactory.getLog(this.getClass()); |
50 | |
|
51 | |
private List<JavaComponent> components; |
52 | |
|
53 | |
private WebApplication application; |
54 | |
|
55 | 0 | private List<ExceptionMapper<?>> exceptionMappers = new ArrayList<ExceptionMapper<?>>(); |
56 | |
|
57 | |
@Override |
58 | |
protected void doInitialise() throws InitialisationException |
59 | |
{ |
60 | 0 | super.doInitialise(); |
61 | |
|
62 | 0 | final Set<Class<?>> resources = new HashSet<Class<?>>(); |
63 | |
|
64 | 0 | if (components == null) |
65 | |
{ |
66 | 0 | throw new IllegalStateException("There must be at least one component in the Jersey resources."); |
67 | |
} |
68 | |
|
69 | 0 | initializeResources(resources); |
70 | 0 | initializeExceptionMappers(resources); |
71 | |
|
72 | 0 | DefaultResourceConfig resourceConfig = createConfiguration(resources); |
73 | |
|
74 | 0 | application = WebApplicationFactory.createWebApplication(); |
75 | 0 | application.initiate(resourceConfig, getComponentProvider()); |
76 | 0 | } |
77 | |
|
78 | |
protected void initializeResources(Set<Class<?>> resources) throws InitialisationException |
79 | |
{ |
80 | |
|
81 | 0 | for (JavaComponent component : components) |
82 | |
{ |
83 | |
Class<?> c; |
84 | |
try |
85 | |
{ |
86 | 0 | c = component.getObjectType(); |
87 | 0 | resources.add(c); |
88 | |
} |
89 | 0 | catch (Exception e) |
90 | |
{ |
91 | 0 | throw new InitialisationException(e, this); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
protected void initializeExceptionMappers(final Set<Class<?>> resources) |
97 | |
{ |
98 | |
|
99 | 0 | for (ExceptionMapper<?> exceptionMapper : exceptionMappers) |
100 | |
{ |
101 | 0 | resources.add(exceptionMapper.getClass()); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
protected DefaultResourceConfig createConfiguration(final Set<Class<?>> resources) |
106 | |
{ |
107 | 0 | return new DefaultResourceConfig(resources); |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
protected Object doInvoke(MuleEvent event) throws Exception |
112 | |
{ |
113 | 0 | MuleMessage message = event.getMessage(); |
114 | |
|
115 | 0 | String path = (String) message.getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY); |
116 | 0 | String contextPath = (String) message.getInboundProperty(HttpConnector.HTTP_CONTEXT_PATH_PROPERTY); |
117 | 0 | String query = null; |
118 | 0 | int queryIdx = path.indexOf('?'); |
119 | 0 | if (queryIdx != -1) |
120 | |
{ |
121 | 0 | query = path.substring(queryIdx + 1); |
122 | 0 | path = path.substring(0, queryIdx); |
123 | |
} |
124 | |
|
125 | 0 | EndpointURI endpointUri = event.getEndpoint().getEndpointURI(); |
126 | 0 | String host = message.getInboundProperty("Host", endpointUri.getHost()); |
127 | 0 | String method = message.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY); |
128 | 0 | InBoundHeaders headers = new InBoundHeaders(); |
129 | 0 | for (Object prop : message.getInboundPropertyNames()) |
130 | |
{ |
131 | 0 | Object property = message.getInboundProperty(prop.toString()); |
132 | 0 | if (property != null) |
133 | |
{ |
134 | 0 | headers.add(prop.toString(), property.toString()); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
String scheme; |
139 | 0 | if ("servlet".equals(endpointUri.getScheme())) |
140 | |
{ |
141 | 0 | scheme = "http"; |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | scheme = endpointUri.getScheme(); |
146 | |
} |
147 | |
|
148 | 0 | URI baseUri = getBaseUri(endpointUri, scheme, host, contextPath); |
149 | 0 | URI completeUri = getCompleteUri(endpointUri, scheme, host, path, query); |
150 | 0 | ContainerRequest req = new ContainerRequest(application, method, baseUri, completeUri, headers, |
151 | |
getInputStream(message)); |
152 | 0 | if (logger.isDebugEnabled()) |
153 | |
{ |
154 | 0 | logger.debug("Base URI: " + baseUri); |
155 | 0 | logger.debug("Complete URI: " + completeUri); |
156 | |
} |
157 | |
|
158 | 0 | MuleResponseWriter writer = new MuleResponseWriter(message); |
159 | 0 | ContainerResponse res = new ContainerResponse(application, req, writer); |
160 | |
|
161 | 0 | application.handleRequest(req, res); |
162 | |
|
163 | 0 | return writer.getResponse(); |
164 | |
} |
165 | |
|
166 | |
protected static InputStream getInputStream(MuleMessage message) throws TransformerException |
167 | |
{ |
168 | 0 | return message.getPayload(InputStream.class); |
169 | |
} |
170 | |
|
171 | |
protected IoCComponentProviderFactory getComponentProvider() |
172 | |
{ |
173 | 0 | return new MuleComponentProviderFactory(muleContext, components); |
174 | |
} |
175 | |
|
176 | |
protected static URI getCompleteUri(EndpointURI endpointUri, |
177 | |
String scheme, |
178 | |
String host, |
179 | |
String path, |
180 | |
String query) throws URISyntaxException |
181 | |
{ |
182 | 0 | String uri = scheme + "://" + host + path; |
183 | 0 | if (query != null) |
184 | |
{ |
185 | 0 | uri += "?" + query; |
186 | |
} |
187 | |
|
188 | 0 | return new URI(uri); |
189 | |
} |
190 | |
|
191 | |
protected static URI getBaseUri(EndpointURI endpointUri, String scheme, String host, String contextPath) |
192 | |
throws URISyntaxException |
193 | |
{ |
194 | 0 | if (!contextPath.endsWith("/")) |
195 | |
{ |
196 | 0 | contextPath += "/"; |
197 | |
} |
198 | |
|
199 | 0 | return new URI(scheme + "://" + host + contextPath); |
200 | |
} |
201 | |
|
202 | |
public List<JavaComponent> getComponents() |
203 | |
{ |
204 | 0 | return components; |
205 | |
} |
206 | |
|
207 | |
public void setComponents(List<JavaComponent> components) |
208 | |
{ |
209 | 0 | this.components = components; |
210 | 0 | } |
211 | |
|
212 | |
public void setMessageProcessors(List<MessageProcessor> messageProcessors) |
213 | |
{ |
214 | 0 | List<JavaComponent> javaComponents = new ArrayList<JavaComponent>(); |
215 | 0 | for (MessageProcessor mp : messageProcessors) |
216 | |
{ |
217 | 0 | if (mp instanceof JavaComponent) |
218 | |
{ |
219 | 0 | javaComponents.add((JavaComponent) mp); |
220 | |
} |
221 | |
else |
222 | |
{ |
223 | 0 | throw new IllegalStateException("Only JavaComponents are allowed as MessageProcessors. Type " |
224 | |
+ mp.getClass().getName() + " is not allowed."); |
225 | |
} |
226 | |
} |
227 | 0 | setComponents(javaComponents); |
228 | 0 | } |
229 | |
|
230 | |
public void setExceptionMapper(ExceptionMapper<?> exceptionMapper) |
231 | |
{ |
232 | 0 | exceptionMappers.add(exceptionMapper); |
233 | 0 | } |
234 | |
} |