You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@synapse.apache.org by zimny rado <ra...@gmail.com> on 2008/12/15 11:56:36 UTC

REST needs XML header

Hello,

I have problem with sending REST to extenral service. My endpoint is
definied:

...
                <send>
                    <endpoint>
                        <address uri="http://extenral.service.com:9090/"
format="pox" />
                    </endpoint>
                </send>
...

When I log message just before sending it has following content:

...
<soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <name>RAZU</name>
</soapenv:Body>
...

and only <name>RAZU</name> is send to extenal service, but I need to send:

<?xml version="1.0"?>
<name>RAZU</name>

What is your suggestions?

Re: REST needs XML header

Posted by "Asankha C. Perera" <as...@apache.org>.
Hi Zimny
> OK, I solved my problem. I wrote
>   
Cool :-) .. glad it helped!

cheers
asankha

-- 
Asankha C. Perera
http://adroitlogic.org

http://esbmagic.blogspot.com


Re: REST needs XML header

Posted by zimny rado <ra...@gmail.com>.
OK, I solved my problem. I wrote

...
                    byte[] bytes = "<?xml version=\"1.0\"
encoding=\"utf-8\"?>".getBytes();
                    outputStream.write(bytes);
...

instead of
...
                   PrintWriter out = new PrintWriter(new
OutputStreamWriter(outputStream));
                   out.println("<?xml version=\"1.0\"
encoding=\"utf-8\"?>");\
...

cheers


2008/12/16 zimny rado <ra...@gmail.com>

> Hello Asankha,
>
> I have done your proposed solution, but another problem arrived, this is my
> ApplicationXMLFormatter class:
>
> package razu.synapse;
>
> import org.apache.axiom.om.OMElement;
> import org.apache.axiom.om.OMOutputFormat;
> import org.apache.axiom.soap.SOAP11Constants;
> import org.apache.axiom.soap.SOAP12Constants;
> import org.apache.axiom.soap.SOAPFault;
> import org.apache.axiom.soap.SOAPFaultDetail;
> import org.apache.axis2.AxisFault;
> import org.apache.axis2.Constants;
> import org.apache.axis2.context.MessageContext;
> import org.apache.axis2.transport.MessageFormatter;
> import org.apache.axis2.transport.http.util.URLTemplatingUtil;
> import org.apache.axis2.util.JavaUtils;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
>
> import javax.xml.stream.XMLStreamException;
> import java.io.ByteArrayOutputStream;
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.io.OutputStream;
> import java.io.OutputStreamWriter;
> import java.io.PrintWriter;
> import java.net.URL;
>
> public class ApplicationXMLFormatter extends
> org.apache.axis2.transport.http.ApplicationXMLFormatter {
>
>     private static final Log log =
> LogFactory.getLog(ApplicationXMLFormatter.class);
>
>     @Override
>     public void writeTo(MessageContext messageContext, OMOutputFormat
> format,
>             OutputStream outputStream, boolean preserve) throws AxisFault {
>
>         if (log.isDebugEnabled()) {
>             log.debug("start writeTo()");
>         }
>         try {
>             OMElement omElement = null;
>
>             if (messageContext.getFLOW() == MessageContext.OUT_FAULT_FLOW)
> {
>                 SOAPFault fault = messageContext.getEnvelope().getBody()
>                         .getFault();
>                 SOAPFaultDetail soapFaultDetail = fault.getDetail();
>                 if (soapFaultDetail != null) {
>                     omElement = soapFaultDetail.getFirstElement();
>                 }
>                 if (omElement == null) {
>                     omElement = fault.getReason();
>                 }
>
>             } else {
>                 omElement = messageContext.getEnvelope().getBody()
>                         .getFirstElement();
>             }
>             if (omElement != null) {
>                 try {
>
>                     PrintWriter out = new PrintWriter(new
> OutputStreamWriter(outputStream));
>                     out.println("<?xml version=\"1.0\"
> encoding=\"utf-8\"?>");
>
>                     if (preserve) {
>                         omElement.serialize(outputStream, format);
>                     } else {
>                         omElement.serializeAndConsume(outputStream,
> format);
>                     }
>                 } catch (XMLStreamException e) {
>                     throw AxisFault.makeFault(e);
>                 } catch (FileNotFoundException ex) {
>                     ex.printStackTrace();
>                 }
>             }
>             try {
>                 outputStream.flush();
>             } catch (IOException e) {
>                 throw AxisFault.makeFault(e);
>             }
>         } finally {
>             if (log.isDebugEnabled()) {
>                 log.debug("end writeTo()");
>             }
>         }
>     }
> }
>
> This is rewrited method from original ApplicationXMLFormatter, I only added
>
>                     PrintWriter out = new PrintWriter(new
> OutputStreamWriter(outputStream));
>                     out.println("<?xml version=\"1.0\"
> encoding=\"utf-8\"?>");
>
> I created JAR with this class and copy it to /lib directory
>
> Another thing to do was change axis2.xml :
>
> ...
>         <messageFormatter contentType="application/xml"
>                          class="razu.synapse.ApplicationXMLFormatter"/>
> ...
>
> My class is used by Apache Synapse, but output XML is still without XML
> header.
>
> Have you any idea what I done wrong?
>
>
>
> 2008/12/16 Asankha C. Perera <as...@apache.org>
>
> Hi Zimny
>>
>>> ...
>>> <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
>>>    <name>RAZU</name>
>>> </soapenv:Body>
>>> ...
>>>
>>> and only <name>RAZU</name> is send to extenal service, but I need to
>>> send:
>>>
>>> <?xml version="1.0"?>
>>> <name>RAZU</name>
>>>
>>> What is your suggestions?
>>>
>>>
>> Usually the XML declaration is not sent, nor expected.. One solution is to
>> extend the message formatter to include the declaration..
>>
>> cheers
>> asankha
>>
>> --
>> Asankha C. Perera
>> http://adroitlogic.org
>>
>> http://esbmagic.blogspot.com
>>
>>
>

Re: REST needs XML header

Posted by zimny rado <ra...@gmail.com>.
Hello Asankha,

I have done your proposed solution, but another problem arrived, this is my
ApplicationXMLFormatter class:

package razu.synapse;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPFaultDetail;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.MessageFormatter;
import org.apache.axis2.transport.http.util.URLTemplatingUtil;
import org.apache.axis2.util.JavaUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.stream.XMLStreamException;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;

public class ApplicationXMLFormatter extends
org.apache.axis2.transport.http.ApplicationXMLFormatter {

    private static final Log log =
LogFactory.getLog(ApplicationXMLFormatter.class);

    @Override
    public void writeTo(MessageContext messageContext, OMOutputFormat
format,
            OutputStream outputStream, boolean preserve) throws AxisFault {

        if (log.isDebugEnabled()) {
            log.debug("start writeTo()");
        }
        try {
            OMElement omElement = null;

            if (messageContext.getFLOW() == MessageContext.OUT_FAULT_FLOW) {
                SOAPFault fault = messageContext.getEnvelope().getBody()
                        .getFault();
                SOAPFaultDetail soapFaultDetail = fault.getDetail();
                if (soapFaultDetail != null) {
                    omElement = soapFaultDetail.getFirstElement();
                }
                if (omElement == null) {
                    omElement = fault.getReason();
                }

            } else {
                omElement = messageContext.getEnvelope().getBody()
                        .getFirstElement();
            }
            if (omElement != null) {
                try {

                    PrintWriter out = new PrintWriter(new
OutputStreamWriter(outputStream));
                    out.println("<?xml version=\"1.0\"
encoding=\"utf-8\"?>");

                    if (preserve) {
                        omElement.serialize(outputStream, format);
                    } else {
                        omElement.serializeAndConsume(outputStream, format);
                    }
                } catch (XMLStreamException e) {
                    throw AxisFault.makeFault(e);
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
            try {
                outputStream.flush();
            } catch (IOException e) {
                throw AxisFault.makeFault(e);
            }
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("end writeTo()");
            }
        }
    }
}

This is rewrited method from original ApplicationXMLFormatter, I only added

                    PrintWriter out = new PrintWriter(new
OutputStreamWriter(outputStream));
                    out.println("<?xml version=\"1.0\"
encoding=\"utf-8\"?>");

I created JAR with this class and copy it to /lib directory

Another thing to do was change axis2.xml :

...
        <messageFormatter contentType="application/xml"
                         class="razu.synapse.ApplicationXMLFormatter"/>
...

My class is used by Apache Synapse, but output XML is still without XML
header.

Have you any idea what I done wrong?



2008/12/16 Asankha C. Perera <as...@apache.org>

> Hi Zimny
>
>> ...
>> <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
>>    <name>RAZU</name>
>> </soapenv:Body>
>> ...
>>
>> and only <name>RAZU</name> is send to extenal service, but I need to send:
>>
>> <?xml version="1.0"?>
>> <name>RAZU</name>
>>
>> What is your suggestions?
>>
>>
> Usually the XML declaration is not sent, nor expected.. One solution is to
> extend the message formatter to include the declaration..
>
> cheers
> asankha
>
> --
> Asankha C. Perera
> http://adroitlogic.org
>
> http://esbmagic.blogspot.com
>
>

Re: REST needs XML header

Posted by "Asankha C. Perera" <as...@apache.org>.
Hi Zimny
> ...
> <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
>     <name>RAZU</name>
> </soapenv:Body>
> ...
>
> and only <name>RAZU</name> is send to extenal service, but I need to send:
>
> <?xml version="1.0"?>
> <name>RAZU</name>
>
> What is your suggestions?
>   
Usually the XML declaration is not sent, nor expected.. One solution is 
to extend the message formatter to include the declaration..

cheers
asankha

-- 
Asankha C. Perera
http://adroitlogic.org

http://esbmagic.blogspot.com