1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.soap.transformers;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.api.config.MuleProperties;
15 import org.mule.api.transformer.TransformerException;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.transformer.AbstractMessageAwareTransformer;
18 import org.mule.util.IOUtils;
19 import org.mule.util.PropertiesUtils;
20 import org.mule.util.StringMessageUtils;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.UnsupportedEncodingException;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Properties;
29
30
31
32
33
34
35 public class HttpRequestToSoapRequest extends AbstractMessageAwareTransformer
36 {
37 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>";
38 public static final String SOAP_FOOTER = "</soap:Body></soap:Envelope>";
39 public static final String DEFAULT_NAMESPACE = "http://www.muleumo.org/soap";
40
41 public HttpRequestToSoapRequest()
42 {
43 registerSourceType(String.class);
44 registerSourceType(InputStream.class);
45 registerSourceType(byte[].class);
46 setReturnClass(String.class);
47 }
48
49 public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
50 {
51 Object src = message.getPayload();
52
53 String data = src.toString();
54 if (src instanceof InputStream)
55 {
56 InputStream is = (InputStream)src;
57 ByteArrayOutputStream bos = new ByteArrayOutputStream();
58 try
59 {
60 try
61 {
62 IOUtils.copy(is, bos);
63 }
64 finally
65 {
66 is.close();
67 }
68 }
69 catch (IOException e)
70 {
71 throw new TransformerException(this, e);
72 }
73
74 src = bos.toByteArray();
75 }
76
77 if (src instanceof byte[])
78 {
79 try
80 {
81 data = new String((byte[])src, outputEncoding);
82 }
83 catch (UnsupportedEncodingException e)
84 {
85 throw new TransformerException(this, e);
86 }
87
88 if (data.startsWith("<") || data.startsWith("<"))
89 {
90 return data;
91 }
92 }
93
94 String httpMethod = message.getStringProperty("http.method", "GET");
95 String request = message.getStringProperty("http.request", null);
96
97 int i = request.indexOf('?');
98 String query = request.substring(i + 1);
99 Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
100
101 String method = (String)p.remove(MuleProperties.MULE_METHOD_PROPERTY);
102 if (method == null)
103 {
104 throw new TransformerException(
105 CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this);
106 }
107
108 if (httpMethod.equals("POST"))
109 {
110 p.setProperty(method, data);
111 }
112
113 StringBuffer result = new StringBuffer(8192);
114 String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[]{outputEncoding});
115
116 result.append(header);
117 result.append('<').append(method).append(" xmlns=\"");
118 result.append(DEFAULT_NAMESPACE).append("\">");
119 for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();)
120 {
121 Map.Entry entry = (Map.Entry)iterator.next();
122 result.append('<').append(entry.getKey()).append('>');
123 result.append(entry.getValue());
124 result.append("</").append(entry.getKey()).append('>');
125 }
126 result.append("</").append(method).append('>');
127 result.append(SOAP_FOOTER);
128
129 return result.toString();
130 }
131 }