KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > transformers > HttpRequestToSoapRequest


1 /*
2  * $Id: HttpRequestToSoapRequest.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.transformers.AbstractEventAwareTransformer;
17 import org.mule.umo.UMOEventContext;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.transformer.TransformerException;
20 import org.mule.util.PropertiesUtils;
21 import org.mule.util.StringMessageUtils;
22
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * A simple transformer for converting an Http GET request into a SOAP request.
30  * Usually, you would POST a soap document, but this Transformer can be useful for
31  * making simple soap requests
32  *
33  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
34  * @version $Revision: 3798 $
35  */

36 public class HttpRequestToSoapRequest extends AbstractEventAwareTransformer
37 {
38     /**
39      * Serial version
40      */

41     private static final long serialVersionUID = -4958196124440362142L;
42
43     public static final String JavaDoc 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>";
44     public static final String JavaDoc SOAP_FOOTER = "</soap:Body></soap:Envelope>";
45     public static final String JavaDoc DEFAULT_NAMESPACE = "http://www.muleumo.org/soap";
46
47     public HttpRequestToSoapRequest()
48     {
49         registerSourceType(String JavaDoc.class);
50         registerSourceType(byte[].class);
51     }
52
53     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
54     {
55         String JavaDoc data = src.toString();
56         if (src instanceof byte[])
57         {
58             try
59             {
60                 data = new String JavaDoc((byte[])src, encoding);
61             }
62             catch (UnsupportedEncodingException JavaDoc e)
63             {
64                 throw new TransformerException(this, e);
65             }
66             // Data is already Xml
67
if (data.startsWith("<") || data.startsWith("&lt;"))
68             {
69                 return data;
70             }
71         }
72         String JavaDoc httpMethod = context.getMessage().getStringProperty("http.method", "GET");
73         String JavaDoc request = context.getMessage().getStringProperty("http.request", null);
74
75         int i = request.indexOf('?');
76         String JavaDoc query = request.substring(i + 1);
77         Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
78
79         String JavaDoc method = (String JavaDoc)p.remove(MuleProperties.MULE_METHOD_PROPERTY);
80         if (method == null)
81         {
82             throw new TransformerException(new Message(Messages.PROPERTIES_X_NOT_SET,
83                 MuleProperties.MULE_METHOD_PROPERTY), this);
84         }
85
86         if (httpMethod.equals("POST"))
87         {
88
89             try
90             {
91                 p.setProperty(method, context.getMessageAsString());
92             }
93             catch (UMOException e)
94             {
95                 throw new TransformerException(this, e);
96             }
97         }
98
99         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8192);
100         String JavaDoc header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object JavaDoc[]{encoding});
101
102         if (p.size() > 0)
103         {
104             result.append(header);
105             result.append('<').append(method).append(" xmlns=\"");
106             result.append(DEFAULT_NAMESPACE).append("\">");
107             for (Iterator JavaDoc iterator = p.entrySet().iterator(); iterator.hasNext();)
108             {
109                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
110                 result.append('<').append(entry.getKey()).append('>');
111                 result.append(entry.getValue());
112                 result.append("</").append(entry.getKey()).append('>');
113             }
114             result.append("</").append(method).append('>');
115             result.append(SOAP_FOOTER);
116         }
117         else
118         {
119             throw new TransformerException(new Message(Messages.PROPERTIES_X_NOT_SET,
120                 MuleProperties.MULE_METHOD_PROPERTY), this);
121         }
122
123         return result.toString();
124     }
125 }
126
Popular Tags