View Javadoc

1   /*
2    * $Id: ObjectToHttpClientMethodRequest.java 20247 2010-11-18 14:23:04Z dirk.olmes $
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  
11  package org.mule.transport.http.transformers;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.api.transport.OutputHandler;
19  import org.mule.api.transport.PropertyScope;
20  import org.mule.message.ds.StringDataSource;
21  import org.mule.transformer.AbstractMessageTransformer;
22  import org.mule.transformer.types.DataTypeFactory;
23  import org.mule.transport.NullPayload;
24  import org.mule.transport.http.HttpConnector;
25  import org.mule.transport.http.HttpConstants;
26  import org.mule.transport.http.StreamPayloadRequestEntity;
27  import org.mule.transport.http.i18n.HttpMessages;
28  import org.mule.util.IOUtils;
29  import org.mule.util.ObjectUtils;
30  import org.mule.util.StringUtils;
31  
32  import java.io.InputStream;
33  import java.io.Serializable;
34  import java.io.UnsupportedEncodingException;
35  import java.net.URI;
36  import java.net.URISyntaxException;
37  import java.net.URLEncoder;
38  import java.util.Iterator;
39  import java.util.Map;
40  
41  import javax.activation.DataHandler;
42  import javax.activation.FileDataSource;
43  import javax.activation.URLDataSource;
44  
45  import org.apache.commons.httpclient.HttpMethod;
46  import org.apache.commons.httpclient.HttpVersion;
47  import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
48  import org.apache.commons.httpclient.methods.DeleteMethod;
49  import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
50  import org.apache.commons.httpclient.methods.GetMethod;
51  import org.apache.commons.httpclient.methods.HeadMethod;
52  import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
53  import org.apache.commons.httpclient.methods.OptionsMethod;
54  import org.apache.commons.httpclient.methods.PostMethod;
55  import org.apache.commons.httpclient.methods.PutMethod;
56  import org.apache.commons.httpclient.methods.StringRequestEntity;
57  import org.apache.commons.httpclient.methods.TraceMethod;
58  import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
59  import org.apache.commons.httpclient.methods.multipart.FilePart;
60  import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
61  import org.apache.commons.httpclient.methods.multipart.Part;
62  import org.apache.commons.httpclient.methods.multipart.StringPart;
63  import org.apache.commons.httpclient.params.HttpMethodParams;
64  import org.apache.commons.lang.SerializationUtils;
65  
66  /**
67   * <code>ObjectToHttpClientMethodRequest</code> transforms a MuleMessage into a
68   * HttpClient HttpMethod that represents an HttpRequest.
69   */
70  public class ObjectToHttpClientMethodRequest extends AbstractMessageTransformer
71  {
72      public ObjectToHttpClientMethodRequest()
73      {
74          setReturnDataType(DataTypeFactory.create(HttpMethod.class));
75          registerSourceType(DataTypeFactory.MULE_MESSAGE);
76          registerSourceType(DataTypeFactory.BYTE_ARRAY);
77          registerSourceType(DataTypeFactory.STRING);
78          registerSourceType(DataTypeFactory.INPUT_STREAM);
79          registerSourceType(DataTypeFactory.create(OutputHandler.class));
80          registerSourceType(DataTypeFactory.create(NullPayload.class));
81      }
82  
83      @Override
84      public Object transformMessage(MuleMessage msg, String outputEncoding) throws TransformerException
85      {
86          String method = detectHttpMethod(msg);
87          try
88          {
89              HttpMethod httpMethod;
90  
91              if (HttpConstants.METHOD_GET.equals(method))
92              {
93                  httpMethod = createGetMethod(msg, outputEncoding);
94              }
95              else if (HttpConstants.METHOD_POST.equalsIgnoreCase(method))
96              {
97                  httpMethod = createPostMethod(msg, outputEncoding);
98              }
99              else if (HttpConstants.METHOD_PUT.equalsIgnoreCase(method))
100             {
101                 httpMethod = createPutMethod(msg, outputEncoding);
102             }
103             else if (HttpConstants.METHOD_DELETE.equalsIgnoreCase(method))
104             {
105                 httpMethod = createDeleteMethod(msg);
106             }
107             else if (HttpConstants.METHOD_HEAD.equalsIgnoreCase(method))
108             {
109                 httpMethod = createHeadMethod(msg);
110             }
111             else if (HttpConstants.METHOD_OPTIONS.equalsIgnoreCase(method))
112             {
113                 httpMethod = createOptionsMethod(msg);
114             }
115             else if (HttpConstants.METHOD_TRACE.equalsIgnoreCase(method))
116             {
117                 httpMethod = createTraceMethod(msg);
118             }
119             else
120             {
121                 throw new TransformerException(HttpMessages.unsupportedMethod(method));
122             }
123 
124             // Allow the user to set HttpMethodParams as an object on the message
125             final HttpMethodParams params = (HttpMethodParams) msg.removeProperty(
126                 HttpConnector.HTTP_PARAMS_PROPERTY, PropertyScope.OUTBOUND);
127             if (params != null)
128             {
129                 httpMethod.setParams(params);
130             }
131             else
132             {
133                 // TODO we should probably set other properties here
134                 final String httpVersion = msg.getOutboundProperty(HttpConnector.HTTP_VERSION_PROPERTY,
135                     HttpConstants.HTTP11);
136                 if (HttpConstants.HTTP10.equals(httpVersion))
137                 {
138                     httpMethod.getParams().setVersion(HttpVersion.HTTP_1_0);
139                 }
140                 else
141                 {
142                     httpMethod.getParams().setVersion(HttpVersion.HTTP_1_1);
143                 }
144             }
145 
146             setHeaders(httpMethod, msg);
147 
148             return httpMethod;
149         }
150         catch (final Exception e)
151         {
152             throw new TransformerException(this, e);
153         }
154     }
155 
156     protected String detectHttpMethod(MuleMessage msg)
157     {
158         String method = msg.getOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, null);
159         if (method == null)
160         {
161             method = msg.getInvocationProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_POST);
162         }
163         return method;
164     }
165 
166     protected HttpMethod createGetMethod(MuleMessage msg, String outputEncoding) throws Exception
167     {
168         final Object src = msg.getPayload();
169         // TODO It makes testing much harder if we use the endpoint on the
170         // transformer since we need to create correct message types and endpoints
171         // URI uri = getEndpoint().getEndpointURI().getUri();
172         final URI uri = getURI(msg);
173         HttpMethod httpMethod;
174         String query = uri.getRawQuery();
175 
176         httpMethod = new GetMethod(uri.toString());
177         String paramName = msg.getOutboundProperty(HttpConnector.HTTP_GET_BODY_PARAM_PROPERTY, null);
178         if (paramName != null)
179         {
180             paramName = URLEncoder.encode(paramName, outputEncoding);
181 
182             String paramValue;
183             Boolean encode = msg.getInvocationProperty(HttpConnector.HTTP_ENCODE_PARAMVALUE);
184             if (encode == null)
185             {
186                 encode = msg.getOutboundProperty(HttpConnector.HTTP_ENCODE_PARAMVALUE, true);
187             }
188 
189             if (encode)
190             {
191                 paramValue = URLEncoder.encode(src.toString(), outputEncoding);
192             }
193             else
194             {
195                 paramValue = src.toString();
196             }
197 
198             if (!(src instanceof NullPayload) && !StringUtils.EMPTY.equals(src))
199             {
200                 if (query == null)
201                 {
202                     query = paramName + "=" + paramValue;
203                 }
204                 else
205                 {
206                     query += "&" + paramName + "=" + paramValue;
207                 }
208             }
209         }
210 
211         httpMethod.setQueryString(query);
212         return httpMethod;
213     }
214 
215     protected HttpMethod createPostMethod(MuleMessage msg, String outputEncoding) throws Exception
216     {
217         URI uri = getURI(msg);
218         PostMethod postMethod = new PostMethod(uri.toString());
219 
220         String bodyParameterName = getBodyParameterName(msg);
221         Object src = msg.getPayload();
222         if (src instanceof Map)
223         {
224             for (Map.Entry<?, ?> entry : ((Map<?, ?>) src).entrySet())
225             {
226                 postMethod.addParameter(entry.getKey().toString(), entry.getValue().toString());
227             }
228         }
229         else if (bodyParameterName != null)
230         {
231             postMethod.addParameter(bodyParameterName, src.toString());
232 
233         }
234         else
235         {
236             setupEntityMethod(src, outputEncoding, msg, postMethod);
237         }
238 
239         // if a content type was specified on the endpoint, use it
240         String outgoingContentType = msg.getInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE);
241         if (outgoingContentType != null)
242         {
243             postMethod.setRequestHeader(HttpConstants.HEADER_CONTENT_TYPE, outgoingContentType);
244         }
245 
246         return postMethod;
247     }
248 
249     protected String getBodyParameterName(MuleMessage message)
250     {
251         String bodyParameter = message.getOutboundProperty(HttpConnector.HTTP_POST_BODY_PARAM_PROPERTY);
252         if (bodyParameter == null)
253         {
254             bodyParameter = message.getInvocationProperty(HttpConnector.HTTP_POST_BODY_PARAM_PROPERTY);
255         }
256         return bodyParameter;
257     }
258 
259     protected HttpMethod createPutMethod(MuleMessage msg, String outputEncoding) throws Exception
260     {
261         URI uri = getURI(msg);
262         PutMethod putMethod = new PutMethod(uri.toString());
263 
264         Object payload = msg.getPayload();
265         setupEntityMethod(payload, outputEncoding, msg, putMethod);
266 
267         return putMethod;
268     }
269 
270     protected HttpMethod createDeleteMethod(MuleMessage message) throws Exception
271     {
272         URI uri = getURI(message);
273         return new DeleteMethod(uri.toString());
274     }
275 
276     protected HttpMethod createHeadMethod(MuleMessage message) throws Exception
277     {
278         URI uri = getURI(message);
279         return new HeadMethod(uri.toString());
280     }
281 
282     protected HttpMethod createOptionsMethod(MuleMessage message) throws Exception
283     {
284         URI uri = getURI(message);
285         return new OptionsMethod(uri.toString());
286     }
287 
288     protected HttpMethod createTraceMethod(MuleMessage message) throws Exception
289     {
290         URI uri = getURI(message);
291         return new TraceMethod(uri.toString());
292     }
293 
294     protected URI getURI(MuleMessage message) throws URISyntaxException, TransformerException
295     {
296         String endpointAddress = message.getOutboundProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, null);
297         if (endpointAddress == null)
298         {
299             throw new TransformerException(
300                 HttpMessages.eventPropertyNotSetCannotProcessRequest(MuleProperties.MULE_ENDPOINT_PROPERTY),
301                 this);
302         }
303         return new URI(endpointAddress);
304     }
305 
306     protected void setupEntityMethod(Object src, String encoding, MuleMessage msg,
307         EntityEnclosingMethod postMethod) throws UnsupportedEncodingException, TransformerException
308     {
309         // Dont set a POST payload if the body is a Null Payload.
310         // This way client calls can control if a POST body is posted explicitly
311         if (!(msg.getPayload() instanceof NullPayload))
312         {
313             String outboundMimeType = (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE,
314                 PropertyScope.OUTBOUND);
315             if (outboundMimeType == null)
316             {
317                 outboundMimeType = (getEndpoint() != null ? getEndpoint().getMimeType() : null);
318             }
319             if (outboundMimeType == null)
320             {
321                 logger.info("Content-Type not set on outgoing request, defaulting to: " + outboundMimeType);
322                 outboundMimeType = HttpConstants.DEFAULT_CONTENT_TYPE;
323             }
324 
325             if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())
326                 && outboundMimeType.indexOf("charset") == -1)
327             {
328                 outboundMimeType += "; charset=" + encoding;
329             }
330 
331             // Ensure that we have a cached representation of the message if we're
332             // using HTTP 1.0
333             final String httpVersion = msg.getOutboundProperty(HttpConnector.HTTP_VERSION_PROPERTY,
334                 HttpConstants.HTTP11);
335             if (HttpConstants.HTTP10.equals(httpVersion))
336             {
337                 try
338                 {
339                     src = msg.getPayloadAsBytes();
340                 }
341                 catch (final Exception e)
342                 {
343                     throw new TransformerException(this, e);
344                 }
345             }
346 
347             if (msg.getOutboundAttachmentNames() != null && msg.getOutboundAttachmentNames().size() > 0)
348             {
349                 try
350                 {
351                     postMethod.setRequestEntity(createMultiPart(msg, postMethod));
352                     return;
353                 }
354                 catch (final Exception e)
355                 {
356                     throw new TransformerException(this, e);
357                 }
358             }
359             if (src instanceof String)
360             {
361                 postMethod.setRequestEntity(new StringRequestEntity(src.toString(), outboundMimeType, encoding));
362                 return;
363             }
364 
365             if (src instanceof InputStream)
366             {
367                 postMethod.setRequestEntity(new InputStreamRequestEntity((InputStream) src, outboundMimeType));
368             }
369             else if (src instanceof byte[])
370             {
371                 postMethod.setRequestEntity(new ByteArrayRequestEntity((byte[]) src, outboundMimeType));
372             }
373             else if (src instanceof OutputHandler)
374             {
375                 final MuleEvent event = RequestContext.getEvent();
376                 postMethod.setRequestEntity(new StreamPayloadRequestEntity((OutputHandler) src, event));
377             }
378             else
379             {
380                 final byte[] buffer = SerializationUtils.serialize((Serializable) src);
381                 postMethod.setRequestEntity(new ByteArrayRequestEntity(buffer, outboundMimeType));
382             }
383         }
384         else if (msg.getOutboundAttachmentNames() != null && msg.getOutboundAttachmentNames().size() > 0)
385         {
386             try
387             {
388                 postMethod.setRequestEntity(createMultiPart(msg, postMethod));
389             }
390             catch (Exception e)
391             {
392                 throw new TransformerException(this, e);
393             }
394         }
395     }
396 
397     protected void setHeaders(HttpMethod httpMethod, MuleMessage msg) throws TransformerException
398     {
399         for (String headerName : msg.getOutboundPropertyNames())
400         {
401             String headerValue = ObjectUtils.getString(msg.getOutboundProperty(headerName), null);
402 
403             if (headerName.startsWith(MuleProperties.PROPERTY_PREFIX))
404             {
405                 // Define Mule headers a custom headers
406                 headerName = new StringBuffer(30).append("X-").append(headerName).toString();
407                 httpMethod.addRequestHeader(headerName, headerValue);
408 
409             }
410             else if (!HttpConstants.RESPONSE_HEADER_NAMES.containsKey(headerName)
411                      && !HttpConnector.HTTP_INBOUND_PROPERTIES.contains(headerName)
412                      && !HttpConnector.HTTP_COOKIES_PROPERTY.equals(headerName))
413             {
414 
415                 httpMethod.addRequestHeader(headerName, headerValue);
416             }
417         }
418     }
419 
420     protected MultipartRequestEntity createMultiPart(MuleMessage msg, EntityEnclosingMethod method)
421         throws Exception
422     {
423         Part[] parts;
424         int i = 0;
425         if (msg.getPayload() instanceof NullPayload)
426         {
427             parts = new Part[msg.getOutboundAttachmentNames().size()];
428         }
429         else
430         {
431             parts = new Part[msg.getOutboundAttachmentNames().size() + 1];
432             parts[i++] = new FilePart("payload", new ByteArrayPartSource("payload", msg.getPayloadAsBytes()));
433         }
434 
435         for (final Iterator<String> iterator = msg.getOutboundAttachmentNames().iterator(); iterator.hasNext(); i++)
436         {
437             final String name = iterator.next();
438             String fileName = name;
439             final DataHandler dh = msg.getOutboundAttachment(name);
440             if (dh.getDataSource() instanceof StringDataSource)
441             {
442                 final StringDataSource ds = (StringDataSource) dh.getDataSource();
443                 parts[i] = new StringPart(ds.getName(), IOUtils.toString(ds.getInputStream()));
444             }
445             else
446             {
447                 if (dh.getDataSource() instanceof FileDataSource)
448                 {
449                     fileName = ((FileDataSource) dh.getDataSource()).getFile().getName();
450                 }
451                 else if (dh.getDataSource() instanceof URLDataSource)
452                 {
453                     fileName = ((URLDataSource) dh.getDataSource()).getURL().getFile();
454                     // Don't use the whole file path, just the file name
455                     final int x = fileName.lastIndexOf("/");
456                     if (x > -1)
457                     {
458                         fileName = fileName.substring(x + 1);
459                     }
460                 }
461                 parts[i] = new FilePart(dh.getName(), new ByteArrayPartSource(fileName,
462                     IOUtils.toByteArray(dh.getInputStream())), dh.getContentType(), null);
463             }
464         }
465 
466         return new MultipartRequestEntity(parts, method.getParams());
467     }
468 }