1
2
3
4
5
6
7
8
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
29
30
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
59 if (data.startsWith("<") || data.startsWith("<"))
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 }