jared
jared

Reputation: 1364

WSO2 XML Declaration

I'm using WSO2 ESB 4.0.3 to deploy a simple service. I have a service returning the following XML:

<Employees>
    <Employee>
        <EmployeeID>JOHNDOE1</EmployeeID>
        <FirstName>JOHN</FirstName>
        <LastName>DOE</LastName>
    </Employee>
    <Status>1</Status>
</Employees>

The problem I'm having is that there is no XML declaration. Is there a setting that will return the response with the XML declaration included, or do I need to use the ESB response to add it? I was hoping for something like:

<?xml version="1.0" encoding="utf-8"?>
<Employees>
    <Employee>
        <EmployeeID>JOHNDOE1</EmployeeID>
        <FirstName>JOHN</FirstName>
        <LastName>DOE</LastName>
    </Employee>
    <Status>1</Status>
</Employees>

Any help is appreciated.

Upvotes: 2

Views: 3157

Answers (4)

Rajith
Rajith

Reputation: 171

Just putting this here in case someone else stumbles upon the same issue, WSO2 now support this OOTB with the following axis2 property

<property name="WRITE_XML_DECLARATION" scope="axis2" value="true"/>

source for this - https://github.com/wso2/wso2-axis2/blob/master/modules/kernel/src/org/apache/axis2/transport/http/ApplicationXMLFormatter.java#L53

Upvotes: 0

Alexey Kakunin
Alexey Kakunin

Reputation: 157

As Kallja answered before - messageFormatter should be fixed.

I've implemented his fix as a maven-based project - available here: https://github.com/akakunin/custom-appxml-message-formatter

I've tested it with WSO2 EI 6.0.0 - it works fine for me.

Upvotes: 1

Kallja
Kallja

Reputation: 5472

This is an old question, but seeing as I ran into the same thing just now I'll post my solution.

I needed to have a proxy service return a plain XML message without the enclosing soap envelope. I tried using application/xml and text/xml (org.apache.axis2.transport.http.ApplicationXMLFormatter and org.wso2.carbon.relay.ExpandingMessageFormatter respectively) content types to no avail. Neither of these content types returned the message with the XML declaration.

The solution is to write a custom message formatter. Here's my implementation that behaves like org.apache.axis2.transport.http.ApplicationXMLFormatter but properly writes the XML declaration to the message.

package com.example.axis2.messageformatter;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.axiom.om.OMOutputFormat;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.ApplicationXMLFormatter;

public class CustomApplicationXmlFormatter extends ApplicationXMLFormatter {

  @Override
  public void writeTo(MessageContext context, OMOutputFormat format, OutputStream out, boolean preserve) throws AxisFault {
    String xmlHeader = "<?xml version=\"1.0\" encoding=\"" + format.getCharSetEncoding() + "\"?>";
    try {
      out.write(xmlHeader.getBytes());
    } catch (IOException e) {
      throw new AxisFault("Unable to write XML declaration to output stream.", e);
    }
    super.writeTo(context, format, out, preserve);
  }
}

You can drop the class in a jar file to <ESB_ROOT>/repository/components/lib. Additionally you need to refer to the class from the axis2 config (<ESB_ROOT>/repository/conf/axis2/axis2.xml) by adding the following into the message formatters portion of the file:

<messageFormatter contentType="application/xml" class="com.example.axis2.messageformatter.CustomApplicationXmlFormatter"/>

Upvotes: 2

Shelan Perera
Shelan Perera

Reputation: 1753

How you captured SOAP message? You can use Tcpmon or just add a log mediator with log level full to observe the complete the complete message. I assume you what you have observed is SOAP body. You do not need to add xml declaration etc. manually.

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>

Upvotes: -1

Related Questions