axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sanat Mastan Kumar <sanatmas...@gmail.com>
Subject Re: How to change Axis2 response character encoding
Date Wed, 08 Apr 2009 03:55:22 GMT
Thanks a ton Andreas.... i read the stream completely and filtered invalid
characters and reassign new stream. of course this might not be idle
solution but it works to my application.

Following highlighted code is the one i added to the original SoapBuilder.

public class CustomSOAPBuilder implements Builder {
    static Logger logger =
Logger.getLogger(CustomSOAPBuilder.class.getName());

    public OMElement processDocument(InputStream inputStream, String
contentType,
                                     MessageContext messageContext) throws
AxisFault {
        XMLStreamReader streamReader;
        try {
            logger.info("Calling my builder");
            String charEncoding = "ISO-8859-1";

            //String filename =
"C:\\Java_Source\\Inventory\\temp\\ranked_resp_mar_8_InvalidUTFChar_FromSoapBuilder.xml";
            //FileWriter fw = new FileWriter(filename);

            InputStreamReader isr = new
InputStreamReader(inputStream,charEncoding);
            BufferedReader br = new BufferedReader (isr);

            ByteArrayOutputStream baos = readSocketStream(br);
            ByteArrayInputStream bais = new
ByteArrayInputStream(baos.toByteArray());

            inputStream = bais;

//messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,charEncoding);
            String charSetEncoding = (String) messageContext

.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);

            // Get the actual encoding by looking at the BOM of the
InputStream
            PushbackInputStream pis =
BuilderUtil.getPushbackInputStream(inputStream);
            String actualCharSetEncoding =
BuilderUtil.getCharSetEncoding(pis, charSetEncoding);

            // Get the XMLStreamReader for this input stream
            streamReader = StAXUtils.createXMLStreamReader(pis,
actualCharSetEncoding);

            StAXBuilder builder = new StAXSOAPModelBuilder(streamReader);
            SOAPEnvelope envelope = (SOAPEnvelope)
builder.getDocumentElement();
            BuilderUtil

.validateSOAPVersion(BuilderUtil.getEnvelopeNamespace(contentType),
envelope);
            BuilderUtil.validateCharSetEncoding(charSetEncoding,
builder.getDocument()
                    .getCharsetEncoding(),
envelope.getNamespace().getNamespaceURI());
            return envelope;
        } catch (IOException e) {
            throw AxisFault.makeFault(e);
        } catch (XMLStreamException e) {
            throw AxisFault.makeFault(e);
        }
    }
    public static ByteArrayOutputStream readSocketStream(BufferedReader br){
        char cbuf[] = new char[1];
        int res= -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
        while(true){
            try{
                res= br.read(cbuf);
                if (res == -1) break;
                if (cbuf[0]>=32 && cbuf[0]<=127){
                    baos.write(cbuf[0]);
                }else{
                    //System.out.println("Filtering Invalid
character:"+cbuf[0]+":"+(int)cbuf[0]);
                    logger.info("Filtering Invalid
character:"+cbuf[0]+":"+(int)cbuf[0]);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return baos;
    }


}


=====================================================

The message builder receives the raw stream from the transport and
returns the corresponding Axiom tree. This is the right place to tweak
the character encoding. Message builders are implementations of
org.apache.axis2.builder.Builder and are configured in axis2.xml (in
the messageBuilders section). In your case you will probably have to
implement an alternative SOAPBuilder. The problem is that message
builders are selected by Content-Type alone. You need to take this
into account if your code access services other than the one having
the encoding issue.

Andreas


On Fri, Mar 13, 2009 at 11:27 AM, Sanat Mastan Kumar
<sanatmastan@gmail.com>wrote:

> Hi Andreas,
> The service we are accessing is Omniture Sitecatalyst webservice, which i
> think we (atleast i) dont have any control over them. they are claiming that
> they are sending data in UTF-8 format, i accept it is we who are sending
> these invalid characters(because our site has multi language support ) to
> omniture which Omniture is storing in its database, but omniture should
> takecare of normalizing them when sending data back in UTF-8 format.
>
> I added following code to my client handler to bring required stream, i put
> this handler in clients InFlow Address phase,
>
>         ByteArrayOutputStream byteOutStr = new ByteArrayOutputStream();
>         SOAPEnvelope se = msgContext.getEnvelope();
>         OMOutputFormat outputFormat = new OMOutputFormat();
>         outputFormat.setCharSetEncoding("ISO-8859-1"); //Also tried with
> differnt formats
>         try{
>         se.serializeAndConsume(byteOutStr, outputFormat);
>         }catch (Exception e){
>             e.printStackTrace();
>         }
>
> i got the byte stream but it is copy of the actual stream, the problem with
> this is msgContext.getEnvelope() contains data until it reached invalid
> UTF-8 character, so the same has been copied to ByteArrayOutputStream
> because of this wehave only partial response, As soon as stream is coming
> from the service axis2 is reading it using UTF8Reader as per below exception
> stracktrace. so if we can somehow get the control of reading the actual
> stream this will help us in normalizing the invalid characters before
> converting it into a streamreader, can you please show how we can read raw
> stream from message builder?
>
> Thanks
> Sanat.
>
>
>
> ================================================================================
>
>
> The only place where you have access to the raw stream is in the
> message builder.
>
> I'm just curious: what is preventing you from having the issue fixed
> by the people who implemented the service?
>
> Andreas
>
>
>
> On Thu, Mar 12, 2009 at 11:33 AM, Sanat Mastan Kumar <
> sanatmastan@gmail.com> wrote:
>
>> Hi Andreas thanks for your response,
>>
>> can you please suggest how we can take the raw response steam from axis2
>> with out any char encoding applying to it?
>>
>> Sanat,
>>
>> It is the responsibility of the service to choose the encoding it
>> wants to use in its response, and the service is expected to indicate
>> the encoding in the Content-Type and then to use that encoding in the
>> response. In your case the service fails to do so (unless you have
>> discovered a very rare bug in Axis2) and violates the SOAP specs. You
>> should collect the evidence for this by inspecting the response using
>> TCPMon or Wireshark and tell the people who developed this service to
>> fix it.
>>
>> Andreas
>>
>> On Wed, Mar 11, 2009 at 18:16, yskm <sanatmastan@gmail.com> wrote:
>> >
>> > Hi,
>> >
>> > i am consuming a webservice (dont know the implementation of the
>> service)
>> > for this i have written my client using axis2 1.4 and rampart 1.4, this
>> > client is working fine under normal response payload, if response
>> contains
>> > any special characters like "Hello world ai? " i.e invalid UTF-8
>> characters
>> > myclient is breaking... following is the stacktrace....
>> >
>> > org.apache.axis2.AxisFault: Error in extracting message properties
>> > � � � �at
>> >
>> org.apache.rampart.handler.RampartReceiver.setFaultCodeAndThrowAxisFault(RampartReceiver.java:166)
>> > � � � �at
>> >
>> org.apache.rampart.handler.RampartReceiver.invoke(RampartReceiver.java:99)
>> > � � � �at org.apache.axis2.engine.Phase.invoke(Phase.java:317)
>> > � � � �at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:264)
>> > � � � �at
>> org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:163)
>> > � � � �at
>> >
>> org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:363)
>> > � � � �at
>> >
>> org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
>> > � � � �at
>> >
>> org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
>> > � � � �at
>> >
>> org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
>> > � � � �at
>> >
>> org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:548)
>> > � � � �at
>> >
>> org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:528)
>> > � � � �at
>> >
>> com.paypal.sparta.inbox.ws.OmnitureWSClient.callService(OmnitureWSClient.java:152)
>> > � � � �at
>> >
>> com.paypal.sparta.inbox.ws.OmnitureWSClient.call2(OmnitureWSClient.java:88)
>> > � � � �at
>> com.paypal.sparta.inbox.ws.OmnitureReport.main(OmnitureReport.java:35)
>> > Caused by: org.apache.rampart.RampartException: Error in extracting
>> message
>> > properties
>> > � � � �at
>> >
>> org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:322)
>> > � � � �at
>> org.apache.rampart.RampartEngine.process(RampartEngine.java:70)
>> > � � � �at
>> >
>> org.apache.rampart.handler.RampartReceiver.invoke(RampartReceiver.java:92)
>> > � � � �... 12 more
>> > Caused by: org.apache.ws.security.WSSecurityException: Error in
>> converting
>> > SOAP Envelope to Document; nested exception is:
>> > � � � �org.apache.axiom.om.OMException:
>> com.ctc.wstx.exc.WstxIOException: Invalid
>> > UTF-8 middle byte 0x66 (at char #57163, byte #56398)
>> > � � � �at
>> >
>> org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:161)
>> > � � � �at
>> >
>> org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:158)
>> > � � � �... 14 more
>> > Caused by: org.apache.axiom.om.OMException:
>> > com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 middle byte 0x66 (at
>> char
>> > #57163, byte #56398)
>> > � � � �at
>> >
>> org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:249)
>> > � � � �at
>> org.apache.axiom.om.impl.llom.OMNodeImpl.build(OMNodeImpl.java:327)
>> > � � � �at
>> >
>> org.apache.axiom.om.impl.llom.OMElementImpl.build(OMElementImpl.java:706)
>> > � � � �at
>> >
>> org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:96)
>> > � � � �... 15 more
>> > Caused by: com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 middle byte
>> 0x66
>> > (at char #57163, byte #56398)
>> > � � � �at
>> com.ctc.wstx.sr.StreamScanner.throwFromIOE(StreamScanner.java:683)
>> > � � � �at
>> com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1086)
>> > � � � �at
>> >
>> org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:506)
>> > � � � �at
>> >
>> org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:161)
>> > � � � �... 18 more
>> > Caused by: java.io.CharConversionException: Invalid UTF-8 middle byte
>> 0x66
>> > (at char #57163, byte #56398)
>> > � � � �at
>> com.ctc.wstx.io.UTF8Reader.reportInvalidOther(UTF8Reader.java:313)
>> > � � � �at com.ctc.wstx.io.UTF8Reader.read(UTF8Reader.java:204)
>> > � � � �at com.ctc.wstx.io.MergedReader.read(MergedReader.java:101)
>> > � � � �at com.ctc.wstx.io.ReaderSource.readInto(ReaderSource.java:84)
>> > � � � �at
>> >
>> com.ctc.wstx.io.BranchingReaderSource.readInto(BranchingReaderSource.java:57)
>> > � � � �at
>> >
>> com.ctc.wstx.sr.StreamScanner.loadMoreFromCurrent(StreamScanner.java:1021)
>> > � � � �at
>> com.ctc.wstx.sr.StreamScanner.parseLocalName2(StreamScanner.java:1771)
>> > � � � �at
>> com.ctc.wstx.sr.StreamScanner.parseLocalName(StreamScanner.java:1731)
>> > � � � �at
>> >
>> com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2912)
>> > � � � �at
>> >
>> com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2846)
>> > � � � �at
>> com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1019)
>> > � � � �... 20 more
>> >
>> >
>> > I tried different ways to change the character encoding of the response
>> > input stream
>> >
>> > 1) by adding following options before calling the service method, this
>> > didn't worked.
>> >
>> >
>> options.setProperty(org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,"UTF-16");
>> > at what circumstance does this statement work??
>> >
>> > 2) I tried adding a handler in the InFlow phase inside Transport and
>> > Addressing and with in the handler i added following statements
>> >
>> > � � � � � � � �SOAPEnvelope se = msgContext.getEnvelope();
>> > � � � � � � � �System.out.println(se);
>> > Here :) fortunately sysout is printing entire response payload , but the
>> > problem is it is printing up to valid UTF-8 characters,
>> >
>> > inspite of all these i didnt succeed in getting a response stream,
>> >
>> > i have two questions
>> > 1) can we change the response character encoding? before response stream
>> is
>> > converted in to reader.
>> > 2) if not can we get raw response stream alone? later i will be parsing
>> it
>> > in my program?
>> >
>> > This is very urgent requirement any help would be highly appreciated.
>> >
>> > Thanks
>> > Sanat.
>> > --
>> > View this message in context:
>> http://www.nabble.com/How-to-change-Axis2-response-character-encoding-tp22460000p22460000.html
>> > Sent from the Axis - User mailing list archive at Nabble.com.
>>
>> >
>> >
>>
>>
>> On Thu, Mar 12, 2009 at 1:33 AM, Nabble Alerts <no-reply@nabble.com>
>> wrote:
>> >
>> > Nabble email alert
>> >
>> > New reply to your post How to change Axis2 response character encoding
>> >
>> > Re: How to change Axis2 response character encoding - 3 star
>> > Sanat, It is the responsibility of the service to choose the encoding it
>> wants to use in its response, and the service is expected to indicate th...
>>  Read the entire message
>> > by Andreas Veithen-2 on 2009-03-11 in the Axis - User forum
>> >
>> > Delete this alert
>> >
>> > ________________________________
>> > DO NOT REPLY TO THIS E-MAIL.
>> >
>> > Replies sent to this address are not read or processed. If you want to
>> respond to a Nabble post for which you received this alert, please go to the
>> Nabble website.
>>
>>
>> --
>>
>> Regards,
>>
>> Sanat
>>
>
>
>
> --
>
> Regards,
>
> Sanat
>



-- 

Regards,

Sanat

Mime
View raw message