View Javadoc

1   /*
2    * $Id: HttpRequestToSoapRequest.java 10086 2007-12-14 10:25:20Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.soap.transformers;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.transformers.AbstractEventAwareTransformer;
16  import org.mule.umo.UMOEventContext;
17  import org.mule.umo.UMOException;
18  import org.mule.umo.transformer.TransformerException;
19  import org.mule.util.PropertiesUtils;
20  import org.mule.util.StringMessageUtils;
21  
22  import java.io.UnsupportedEncodingException;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  /**
28   * A simple transformer for converting an Http GET request into a SOAP request.
29   * Usually, you would POST a SOAP document, but this Transformer can be useful for
30   * making simple SOAP requests
31   */
32  public class HttpRequestToSoapRequest extends AbstractEventAwareTransformer
33  {
34      public static final String SOAP_HEADER = "<?xml version=\"1.0\" encoding=\"{0}\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soap:Body>";
35      public static final String SOAP_FOOTER = "</soap:Body></soap:Envelope>";
36      public static final String DEFAULT_NAMESPACE = "http://www.muleumo.org/soap";
37  
38      public HttpRequestToSoapRequest()
39      {
40          registerSourceType(String.class);
41          registerSourceType(byte[].class);
42          setReturnClass(String.class);
43      }
44  
45      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
46      {
47          String data = src.toString();
48          if (src instanceof byte[])
49          {
50              try
51              {
52                  data = new String((byte[])src, encoding);
53              }
54              catch (UnsupportedEncodingException e)
55              {
56                  throw new TransformerException(this, e);
57              }
58              // Data is already Xml
59              if (data.startsWith("<") || data.startsWith("&lt;"))
60              {
61                  return data;
62              }
63          }
64          String httpMethod = context.getMessage().getStringProperty("http.method", "GET");
65          String request = context.getMessage().getStringProperty("http.request", null);
66  
67          int i = request.indexOf('?');
68          String query = request.substring(i + 1);
69          Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
70  
71          String method = (String)p.remove(MuleProperties.MULE_METHOD_PROPERTY);
72          if (method == null)
73          {
74              throw new TransformerException(
75                  CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this);
76          }
77  
78          if (httpMethod.equals("POST"))
79          {
80  
81              try
82              {
83                  p.setProperty(method, context.getMessageAsString());
84              }
85              catch (UMOException e)
86              {
87                  throw new TransformerException(this, e);
88              }
89          }
90  
91          StringBuffer result = new StringBuffer(8192);
92          String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[]{encoding});
93  
94          result.append(header);
95          result.append('<').append(method).append(" xmlns=\"");
96          result.append(DEFAULT_NAMESPACE).append("\">");
97          for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();)
98          {
99              Map.Entry entry = (Map.Entry)iterator.next();
100             result.append('<').append(entry.getKey()).append('>');
101             result.append(entry.getValue());
102             result.append("</").append(entry.getKey()).append('>');
103         }
104         result.append("</").append(method).append('>');
105         result.append(SOAP_FOOTER);
106 
107         return result.toString();
108     }
109 }