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