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 | 0 | { |
43 | 0 | registerSourceType(String.class); |
44 | 0 | registerSourceType(InputStream.class); |
45 | 0 | registerSourceType(byte[].class); |
46 | 0 | setReturnClass(String.class); |
47 | 0 | } |
48 | |
|
49 | |
public Object transform(MuleMessage message, String outputEncoding) throws TransformerException |
50 | |
{ |
51 | 0 | Object src = message.getPayload(); |
52 | |
|
53 | 0 | String data = src.toString(); |
54 | 0 | if (src instanceof InputStream) |
55 | |
{ |
56 | 0 | InputStream is = (InputStream)src; |
57 | 0 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
58 | |
try |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 0 | IOUtils.copy(is, bos); |
63 | |
} |
64 | |
finally |
65 | |
{ |
66 | 0 | is.close(); |
67 | 0 | } |
68 | |
} |
69 | 0 | catch (IOException e) |
70 | |
{ |
71 | 0 | throw new TransformerException(this, e); |
72 | 0 | } |
73 | |
|
74 | 0 | src = bos.toByteArray(); |
75 | |
} |
76 | |
|
77 | 0 | if (src instanceof byte[]) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | data = new String((byte[])src, outputEncoding); |
82 | |
} |
83 | 0 | catch (UnsupportedEncodingException e) |
84 | |
{ |
85 | 0 | throw new TransformerException(this, e); |
86 | 0 | } |
87 | |
|
88 | 0 | if (data.startsWith("<") || data.startsWith("<")) |
89 | |
{ |
90 | 0 | return data; |
91 | |
} |
92 | |
} |
93 | |
|
94 | 0 | String httpMethod = message.getStringProperty("http.method", "GET"); |
95 | 0 | String request = message.getStringProperty("http.request", null); |
96 | |
|
97 | 0 | int i = request.indexOf('?'); |
98 | 0 | String query = request.substring(i + 1); |
99 | 0 | Properties p = PropertiesUtils.getPropertiesFromQueryString(query); |
100 | |
|
101 | 0 | String method = (String)p.remove(MuleProperties.MULE_METHOD_PROPERTY); |
102 | 0 | if (method == null) |
103 | |
{ |
104 | 0 | throw new TransformerException( |
105 | |
CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this); |
106 | |
} |
107 | |
|
108 | 0 | if (httpMethod.equals("POST")) |
109 | |
{ |
110 | 0 | p.setProperty(method, data); |
111 | |
} |
112 | |
|
113 | 0 | StringBuffer result = new StringBuffer(8192); |
114 | 0 | String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[]{outputEncoding}); |
115 | |
|
116 | 0 | result.append(header); |
117 | 0 | result.append('<').append(method).append(" xmlns=\""); |
118 | 0 | result.append(DEFAULT_NAMESPACE).append("\">"); |
119 | 0 | for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();) |
120 | |
{ |
121 | 0 | Map.Entry entry = (Map.Entry)iterator.next(); |
122 | 0 | result.append('<').append(entry.getKey()).append('>'); |
123 | 0 | result.append(entry.getValue()); |
124 | 0 | result.append("</").append(entry.getKey()).append('>'); |
125 | 0 | } |
126 | 0 | result.append("</").append(method).append('>'); |
127 | 0 | result.append(SOAP_FOOTER); |
128 | |
|
129 | 0 | return result.toString(); |
130 | |
} |
131 | |
} |