Ejemplo n.º 1
0
  public Object transform(UMOMessage message, String outputEncoding) throws TransformerException {
    Object src = message.getPayload();

    String data = src.toString();
    if (src instanceof InputStream) {
      InputStream is = (InputStream) src;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try {
        try {
          IOUtils.copy(is, bos);
        } finally {
          is.close();
        }
      } catch (IOException e) {
        throw new TransformerException(this, e);
      }

      src = bos.toByteArray();
    }

    if (src instanceof byte[]) {
      try {
        data = new String((byte[]) src, outputEncoding);
      } catch (UnsupportedEncodingException e) {
        throw new TransformerException(this, e);
      }
      // Data is already Xml
      if (data.startsWith("<") || data.startsWith("&lt;")) {
        return data;
      }
    }

    String httpMethod = message.getStringProperty("http.method", "GET");
    String request = message.getStringProperty("http.request", null);

    int i = request.indexOf('?');
    String query = request.substring(i + 1);
    Properties p = PropertiesUtils.getPropertiesFromQueryString(query);

    String method = (String) p.remove(MuleProperties.MULE_METHOD_PROPERTY);
    if (method == null) {
      throw new TransformerException(
          CoreMessages.propertiesNotSet(MuleProperties.MULE_METHOD_PROPERTY), this);
    }

    if (httpMethod.equals("POST")) {
      p.setProperty(method, data);
    }

    StringBuffer result = new StringBuffer(8192);
    String header =
        StringMessageUtils.getFormattedMessage(SOAP_HEADER, new Object[] {outputEncoding});

    result.append(header);
    result.append('<').append(method).append(" xmlns=\"");
    result.append(DEFAULT_NAMESPACE).append("\">");
    for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext(); ) {
      Map.Entry entry = (Map.Entry) iterator.next();
      result.append('<').append(entry.getKey()).append('>');
      result.append(entry.getValue());
      result.append("</").append(entry.getKey()).append('>');
    }
    result.append("</").append(method).append('>');
    result.append(SOAP_FOOTER);

    return result.toString();
  }