View Javadoc

1   /*
2    * $Id: HttpRequestToSoapRequest.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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      }
43  
44      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
45      {
46          String data = src.toString();
47          if (src instanceof byte[])
48          {
49              try
50              {
51                  data = new String((byte[])src, encoding);
52              }
53              catch (UnsupportedEncodingException e)
54              {
55                  throw new TransformerException(this, e);
56              }
57              // Data is already Xml
58              if (data.startsWith("<") || data.startsWith("&lt;"))
59              {
60                  return data;
61              }
62          }
63          String httpMethod = context.getMessage().getStringProperty("http.method", "GET");
64          String request = context.getMessage().getStringProperty("http.request", null);
65  
66          int i = request.indexOf('?');
67          String query = request.substring(i + 1);
68          Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
69  
70          String method = (String)p.remove(MuleProperties.MULE_METHOD_PROPERTY);
71          if (method == null)
72          {
73              throw new TransformerException(
74                  CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this);
75          }
76  
77          if (httpMethod.equals("POST"))
78          {
79  
80              try
81              {
82                  p.setProperty(method, context.getMessageAsString());
83              }
84              catch (UMOException e)
85              {
86                  throw new TransformerException(this, e);
87              }
88          }
89  
90          StringBuffer result = new StringBuffer(8192);
91          String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[]{encoding});
92  
93          result.append(header);
94          result.append('<').append(method).append(" xmlns=\"");
95          result.append(DEFAULT_NAMESPACE).append("\">");
96          for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();)
97          {
98              Map.Entry entry = (Map.Entry)iterator.next();
99              result.append('<').append(entry.getKey()).append('>');
100             result.append(entry.getValue());
101             result.append("</").append(entry.getKey()).append('>');
102         }
103         result.append("</").append(method).append('>');
104         result.append(SOAP_FOOTER);
105 
106         return result.toString();
107     }
108 }