001: /*
002: * $Id: HttpRequestToSoapRequest.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.soap.transformers;
012:
013: import org.mule.api.MuleMessage;
014: import org.mule.api.config.MuleProperties;
015: import org.mule.api.transformer.TransformerException;
016: import org.mule.config.i18n.CoreMessages;
017: import org.mule.transformer.AbstractMessageAwareTransformer;
018: import org.mule.util.IOUtils;
019: import org.mule.util.PropertiesUtils;
020: import org.mule.util.StringMessageUtils;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.UnsupportedEncodingException;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.Properties;
029:
030: /**
031: * A simple transformer for converting an Http GET request into a SOAP request.
032: * Usually, you would POST a SOAP document, but this Transformer can be useful for
033: * making simple SOAP requests
034: */
035: public class HttpRequestToSoapRequest extends
036: AbstractMessageAwareTransformer {
037: 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>";
038: public static final String SOAP_FOOTER = "</soap:Body></soap:Envelope>";
039: public static final String DEFAULT_NAMESPACE = "http://www.muleumo.org/soap";
040:
041: public HttpRequestToSoapRequest() {
042: registerSourceType(String.class);
043: registerSourceType(InputStream.class);
044: registerSourceType(byte[].class);
045: setReturnClass(String.class);
046: }
047:
048: public Object transform(MuleMessage message, String outputEncoding)
049: throws TransformerException {
050: Object src = message.getPayload();
051:
052: String data = src.toString();
053: if (src instanceof InputStream) {
054: InputStream is = (InputStream) src;
055: ByteArrayOutputStream bos = new ByteArrayOutputStream();
056: try {
057: try {
058: IOUtils.copy(is, bos);
059: } finally {
060: is.close();
061: }
062: } catch (IOException e) {
063: throw new TransformerException(this , e);
064: }
065:
066: src = bos.toByteArray();
067: }
068:
069: if (src instanceof byte[]) {
070: try {
071: data = new String((byte[]) src, outputEncoding);
072: } catch (UnsupportedEncodingException e) {
073: throw new TransformerException(this , e);
074: }
075: // Data is already Xml
076: if (data.startsWith("<") || data.startsWith("<")) {
077: return data;
078: }
079: }
080:
081: String httpMethod = message.getStringProperty("http.method",
082: "GET");
083: String request = message
084: .getStringProperty("http.request", null);
085:
086: int i = request.indexOf('?');
087: String query = request.substring(i + 1);
088: Properties p = PropertiesUtils
089: .getPropertiesFromQueryString(query);
090:
091: String method = (String) p
092: .remove(MuleProperties.MULE_METHOD_PROPERTY);
093: if (method == null) {
094: throw new TransformerException(
095: CoreMessages
096: .propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY),
097: this );
098: }
099:
100: if (httpMethod.equals("POST")) {
101: p.setProperty(method, data);
102: }
103:
104: StringBuffer result = new StringBuffer(8192);
105: String header = StringMessageUtils.getFormattedMessage(
106: SOAP_HEADER, new Object[] { outputEncoding });
107:
108: result.append(header);
109: result.append('<').append(method).append(" xmlns=\"");
110: result.append(DEFAULT_NAMESPACE).append("\">");
111: for (Iterator iterator = p.entrySet().iterator(); iterator
112: .hasNext();) {
113: Map.Entry entry = (Map.Entry) iterator.next();
114: result.append('<').append(entry.getKey()).append('>');
115: result.append(entry.getValue());
116: result.append("</").append(entry.getKey()).append('>');
117: }
118: result.append("</").append(method).append('>');
119: result.append(SOAP_FOOTER);
120:
121: return result.toString();
122: }
123: }
|