You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by wascjg <ja...@daon.com> on 2010/08/06 20:51:38 UTC

XML Declaration not being added in SOAP Response

I’m rewriting a Web service in Java (originally .NET).

I’m having an issue where the returned SOAP envelope returned is not coming
back with an XML Declaration <?xml version="1.0" encoding="UTF-8"?>

Normally this would not be an issue but the .NET client to the Web Service
is lame and it expects that XML Declaration.  Because there is no
declaration, the client code crashes.  Because the .NET client can’t change,
there’s no simple fix there.

Is there any way to tell CXF to include the XML Declaration when the Web
Service gets called?

I tried creating a JAX-WS Handler which included the following section of
code, and this code gets printed out in System.out correctly, but somehow
the XML declaration never makes it to TCPMON.

	public boolean handleMessage(SOAPMessageContext soapMessageContext)
	{
		Boolean outboundProperty = (Boolean) soapMessageContext
			.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

		SOAPMessage message = soapMessageContext.getMessage();
		if (outboundProperty.booleanValue())
		{
			try
			{
				message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
				soapMessageContext.setMessage(message);
				soapMessageContext.getMessage().writeTo(System.out);
			}

			catch (Exception e)
			{

			}
		}
		return true;
	}


Below is the TCPMON logs of what is being requested and returned:
REQUEST
POST /SpmlProvisioningService/SPMLHandler.asmx HTTP/1.1
Content-Type: text/xml; charset=UTF-8
User-Agent: SPMLClient
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: SPMLLookupRequest
Host: localhost:1111
Content-Length: 364
Expect: 100-continue
Connection: Keep-Alive

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><spml:lookupRequest
returnData="everything" requestID="984"
xmlns:spml="urn:oasis:names:tc:SPML:2:0"><psoID ID="11223344" targetID="123"
eaHIDBadgeID="123"
/></spml:lookupRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>


RESPONSE
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=UTF-8
Content-Length: 442
Date: Fri, 06 Aug 2010 13:03:36 GMT

<!-- NOTE NO XML DECLARATION -->
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><SPMLLookupRequestResult
xmlns="urn:oasis:names:tc:SPML:2:0">&lt;?xml version=&quot;1.0&quot;
encoding=&quot;UTF-8&quot;?&gt;&lt;lookupResponse status=&quot;success&quot;
xmlns=&quot;urn:oasis:names:tc:SPML:2:0&quot;&gt;&lt;pso&gt;&lt;psoID
ID=&quot;1122334455&quot;
/&gt;&lt;/pso&gt;&lt;/lookupResponse&gt;</SPMLLookupRequestResult></soap:Body></soap:Envelope>

Thanks,
James

-- 
View this message in context: http://cxf.547215.n5.nabble.com/XML-Declaration-not-being-added-in-SOAP-Response-tp2266979p2266979.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: XML Declaration not being added in SOAP Response

Posted by wascjg <ja...@daon.com>.
Worked like a charm.  Thank you.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/XML-Declaration-not-being-added-in-SOAP-Response-tp2266979p2268824.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: XML Declaration not being added in SOAP Response

Posted by Daniel Kulp <dk...@apache.org>.
Actually, with any recent version of CXF, it's a bit easier.   You just need 
to configure in a property:

"org.apache.cxf.stax.force-start-document"

with a Boolean.TRUE instance.     That's a bit tricky to do with Spring so I'm 
going to make a minor change for 2.2.10 that will allow the "true" string to 
be used.

 

Dan




On Saturday 07 August 2010 12:10:27 am Rich Newcomb wrote:
> This question was answered by Dan Kulp in 2007:
> 
> http://cxf.547215.n5.nabble.com/Responses-don-t-get-xml-tag-and-encoding-ty
> pe-td551777.html#a551777
> 
> Things may have changed a little since then; however, I put together a
> quick implementation of the interceptor Dan suggested, and it works as
> described.
> 
> Here's the Java code for the interceptor:
> 
> public class XMLDefinitionWriter extends AbstractSoapInterceptor {
> 
>     public XMLDefinitionWriter() {
>         super(Phase.PRE_STREAM);
>         addAfter(StaxOutInterceptor.class.getName());
>     }
> 
>     @Override
>     public void handleMessage(SoapMessage message) throws Fault {
>         try {
>             OutputStream os = message.getContent(OutputStream.class);
>             XMLStreamWriter writer =
>                 message.getContent(XMLStreamWriter.class);
>             String encoding = (String)message.get(Message.ENCODING);
>             writer.writeStartDocument(encoding, "1.0");
>         } catch (XMLStreamException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         }
>     }
> }
> 
> 
> Note that if you need such an interceptor, you should probably add it
> to both the JAX-WS Out Interceptors and Out Fault Interceptors.
> 
> On Fri, Aug 6, 2010 at 2:51 PM, wascjg <ja...@daon.com> wrote:
> > I’m rewriting a Web service in Java (originally .NET).
> > 
> > I’m having an issue where the returned SOAP envelope returned is not
> > coming back with an XML Declaration <?xml version="1.0"
> > encoding="UTF-8"?>
> > 
> > Normally this would not be an issue but the .NET client to the Web
> > Service is lame and it expects that XML Declaration.  Because there is
> > no declaration, the client code crashes.  Because the .NET client can’t
> > change, there’s no simple fix there.
> > 
> > Is there any way to tell CXF to include the XML Declaration when the Web
> > Service gets called?
> > 
> > I tried creating a JAX-WS Handler which included the following section of
> > code, and this code gets printed out in System.out correctly, but somehow
> > the XML declaration never makes it to TCPMON.
> > 
> >        public boolean handleMessage(SOAPMessageContext
> > soapMessageContext) {
> >                Boolean outboundProperty = (Boolean) soapMessageContext
> >                        .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
> > 
> >                SOAPMessage message = soapMessageContext.getMessage();
> >                if (outboundProperty.booleanValue())
> >                {
> >                        try
> >                        {
> >                              
> >  message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
> > soapMessageContext.setMessage(message);
> > soapMessageContext.getMessage().writeTo(System.out); }
> > 
> >                        catch (Exception e)
> >                        {
> > 
> >                        }
> >                }
> >                return true;
> >        }
> > 
> > 
> > Below is the TCPMON logs of what is being requested and returned:
> > REQUEST
> > POST /SpmlProvisioningService/SPMLHandler.asmx HTTP/1.1
> > Content-Type: text/xml; charset=UTF-8
> > User-Agent: SPMLClient
> > Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
> > SOAPAction: SPMLLookupRequest
> > Host: localhost:1111
> > Content-Length: 364
> > Expect: 100-continue
> > Connection: Keep-Alive
> > 
> > <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope
> > xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Head
> > er/><SOAP-ENV:Body><spml:lookupRequest returnData="everything"
> > requestID="984"
> > xmlns:spml="urn:oasis:names:tc:SPML:2:0"><psoID ID="11223344"
> > targetID="123" eaHIDBadgeID="123"
> > /></spml:lookupRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>
> > 
> > 
> > RESPONSE
> > HTTP/1.1 100 Continue
> > 
> > HTTP/1.1 200 OK
> > Server: Apache-Coyote/1.1
> > Content-Type: text/xml;charset=UTF-8
> > Content-Length: 442
> > Date: Fri, 06 Aug 2010 13:03:36 GMT
> > 
> > <!-- NOTE NO XML DECLARATION -->
> > <soap:Envelope
> > xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><SPMLLo
> > okupRequestResult xmlns="urn:oasis:names:tc:SPML:2:0">&lt;?xml
> > version=&quot;1.0&quot;
> > encoding=&quot;UTF-8&quot;?&gt;&lt;lookupResponse
> > status=&quot;success&quot;
> > xmlns=&quot;urn:oasis:names:tc:SPML:2:0&quot;&gt;&lt;pso&gt;&lt;psoID
> > ID=&quot;1122334455&quot;
> > /&gt;&lt;/pso&gt;&lt;/lookupResponse&gt;</SPMLLookupRequestResult></soap:
> > Body></soap:Envelope>
> > 
> > Thanks,
> > James
> > 
> > --
> > View this message in context:
> > http://cxf.547215.n5.nabble.com/XML-Declaration-not-being-added-in-SOAP-
> > Response-tp2266979p2266979.html Sent from the cxf-user mailing list
> > archive at Nabble.com.

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: XML Declaration not being added in SOAP Response

Posted by Rich Newcomb <ri...@gmail.com>.
This question was answered by Dan Kulp in 2007:

http://cxf.547215.n5.nabble.com/Responses-don-t-get-xml-tag-and-encoding-type-td551777.html#a551777

Things may have changed a little since then; however, I put together a
quick implementation of the interceptor Dan suggested, and it works as
described.

Here's the Java code for the interceptor:

public class XMLDefinitionWriter extends AbstractSoapInterceptor {

    public XMLDefinitionWriter() {
        super(Phase.PRE_STREAM);
        addAfter(StaxOutInterceptor.class.getName());
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        try {
            OutputStream os = message.getContent(OutputStream.class);
            XMLStreamWriter writer =
                message.getContent(XMLStreamWriter.class);
            String encoding = (String)message.get(Message.ENCODING);
            writer.writeStartDocument(encoding, "1.0");
        } catch (XMLStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


Note that if you need such an interceptor, you should probably add it
to both the JAX-WS Out Interceptors and Out Fault Interceptors.


On Fri, Aug 6, 2010 at 2:51 PM, wascjg <ja...@daon.com> wrote:
>
> I’m rewriting a Web service in Java (originally .NET).
>
> I’m having an issue where the returned SOAP envelope returned is not coming
> back with an XML Declaration <?xml version="1.0" encoding="UTF-8"?>
>
> Normally this would not be an issue but the .NET client to the Web Service
> is lame and it expects that XML Declaration.  Because there is no
> declaration, the client code crashes.  Because the .NET client can’t change,
> there’s no simple fix there.
>
> Is there any way to tell CXF to include the XML Declaration when the Web
> Service gets called?
>
> I tried creating a JAX-WS Handler which included the following section of
> code, and this code gets printed out in System.out correctly, but somehow
> the XML declaration never makes it to TCPMON.
>
>        public boolean handleMessage(SOAPMessageContext soapMessageContext)
>        {
>                Boolean outboundProperty = (Boolean) soapMessageContext
>                        .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
>
>                SOAPMessage message = soapMessageContext.getMessage();
>                if (outboundProperty.booleanValue())
>                {
>                        try
>                        {
>                                message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
>                                soapMessageContext.setMessage(message);
>                                soapMessageContext.getMessage().writeTo(System.out);
>                        }
>
>                        catch (Exception e)
>                        {
>
>                        }
>                }
>                return true;
>        }
>
>
> Below is the TCPMON logs of what is being requested and returned:
> REQUEST
> POST /SpmlProvisioningService/SPMLHandler.asmx HTTP/1.1
> Content-Type: text/xml; charset=UTF-8
> User-Agent: SPMLClient
> Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
> SOAPAction: SPMLLookupRequest
> Host: localhost:1111
> Content-Length: 364
> Expect: 100-continue
> Connection: Keep-Alive
>
> <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><spml:lookupRequest
> returnData="everything" requestID="984"
> xmlns:spml="urn:oasis:names:tc:SPML:2:0"><psoID ID="11223344" targetID="123"
> eaHIDBadgeID="123"
> /></spml:lookupRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>
>
>
> RESPONSE
> HTTP/1.1 100 Continue
>
> HTTP/1.1 200 OK
> Server: Apache-Coyote/1.1
> Content-Type: text/xml;charset=UTF-8
> Content-Length: 442
> Date: Fri, 06 Aug 2010 13:03:36 GMT
>
> <!-- NOTE NO XML DECLARATION -->
> <soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><SPMLLookupRequestResult
> xmlns="urn:oasis:names:tc:SPML:2:0">&lt;?xml version=&quot;1.0&quot;
> encoding=&quot;UTF-8&quot;?&gt;&lt;lookupResponse status=&quot;success&quot;
> xmlns=&quot;urn:oasis:names:tc:SPML:2:0&quot;&gt;&lt;pso&gt;&lt;psoID
> ID=&quot;1122334455&quot;
> /&gt;&lt;/pso&gt;&lt;/lookupResponse&gt;</SPMLLookupRequestResult></soap:Body></soap:Envelope>
>
> Thanks,
> James
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/XML-Declaration-not-being-added-in-SOAP-Response-tp2266979p2266979.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>