1 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 ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 36 public class HttpRequestToSoapRequest extends AbstractEventAwareTransformer 37 { 38 41 private static final long serialVersionUID = -4958196124440362142L; 42 43 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>"; 44 public static final String SOAP_FOOTER = "</soap:Body></soap:Envelope>"; 45 public static final String DEFAULT_NAMESPACE = "http://www.muleumo.org/soap"; 46 47 public HttpRequestToSoapRequest() 48 { 49 registerSourceType(String .class); 50 registerSourceType(byte[].class); 51 } 52 53 public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException 54 { 55 String data = src.toString(); 56 if (src instanceof byte[]) 57 { 58 try 59 { 60 data = new String ((byte[])src, encoding); 61 } 62 catch (UnsupportedEncodingException e) 63 { 64 throw new TransformerException(this, e); 65 } 66 if (data.startsWith("<") || data.startsWith("<")) 68 { 69 return data; 70 } 71 } 72 String httpMethod = context.getMessage().getStringProperty("http.method", "GET"); 73 String request = context.getMessage().getStringProperty("http.request", null); 74 75 int i = request.indexOf('?'); 76 String query = request.substring(i + 1); 77 Properties p = PropertiesUtils.getPropertiesFromQueryString(query); 78 79 String method = (String )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 result = new StringBuffer (8192); 100 String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object []{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 iterator = p.entrySet().iterator(); iterator.hasNext();) 108 { 109 Map.Entry entry = (Map.Entry )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 |