Coverage Report - org.mule.providers.soap.transformers.HttpRequestToSoapRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpRequestToSoapRequest
0%
0/40
0%
0/18
6.5
 
 1  
 /*
 2  
  * $Id: HttpRequestToSoapRequest.java 10086 2007-12-14 10:25:20Z holger $
 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 CPAL v1.0
 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.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  
  * A simple transformer for converting an Http GET request into a SOAP request.
 29  
  * Usually, you would POST a SOAP document, but this Transformer can be useful for
 30  
  * making simple SOAP requests
 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  0
     {
 40  0
         registerSourceType(String.class);
 41  0
         registerSourceType(byte[].class);
 42  0
         setReturnClass(String.class);
 43  0
     }
 44  
 
 45  
     public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
 46  
     {
 47  0
         String data = src.toString();
 48  0
         if (src instanceof byte[])
 49  
         {
 50  
             try
 51  
             {
 52  0
                 data = new String((byte[])src, encoding);
 53  
             }
 54  0
             catch (UnsupportedEncodingException e)
 55  
             {
 56  0
                 throw new TransformerException(this, e);
 57  0
             }
 58  
             // Data is already Xml
 59  0
             if (data.startsWith("<") || data.startsWith("&lt;"))
 60  
             {
 61  0
                 return data;
 62  
             }
 63  
         }
 64  0
         String httpMethod = context.getMessage().getStringProperty("http.method", "GET");
 65  0
         String request = context.getMessage().getStringProperty("http.request", null);
 66  
 
 67  0
         int i = request.indexOf('?');
 68  0
         String query = request.substring(i + 1);
 69  0
         Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
 70  
 
 71  0
         String method = (String)p.remove(MuleProperties.MULE_METHOD_PROPERTY);
 72  0
         if (method == null)
 73  
         {
 74  0
             throw new TransformerException(
 75  
                 CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this);
 76  
         }
 77  
 
 78  0
         if (httpMethod.equals("POST"))
 79  
         {
 80  
 
 81  
             try
 82  
             {
 83  0
                 p.setProperty(method, context.getMessageAsString());
 84  
             }
 85  0
             catch (UMOException e)
 86  
             {
 87  0
                 throw new TransformerException(this, e);
 88  0
             }
 89  
         }
 90  
 
 91  0
         StringBuffer result = new StringBuffer(8192);
 92  0
         String header = StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[]{encoding});
 93  
 
 94  0
         result.append(header);
 95  0
         result.append('<').append(method).append(" xmlns=\"");
 96  0
         result.append(DEFAULT_NAMESPACE).append("\">");
 97  0
         for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();)
 98  
         {
 99  0
             Map.Entry entry = (Map.Entry)iterator.next();
 100  0
             result.append('<').append(entry.getKey()).append('>');
 101  0
             result.append(entry.getValue());
 102  0
             result.append("</").append(entry.getKey()).append('>');
 103  0
         }
 104  0
         result.append("</").append(method).append('>');
 105  0
         result.append(SOAP_FOOTER);
 106  
 
 107  0
         return result.toString();
 108  
     }
 109  
 }