servicemix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Håkon Sagehaug <Hakon.Sageh...@bccs.uib.no>
Subject Re: Extracting soap message body inside a cxf interceptor in cxf-bc
Date Tue, 03 Nov 2009 13:51:28 GMT
Hi

Sorry for late reply, I tried first to create a string from the cached
output stream like this

 String soapMessage = new String(bos.getBytes());

But that did not work, the bos was alway zero in lenght. Then I tried how it
was done in the  org.apache.cxf.interceptor.StaxInInterceptor, now I can
extract the hole soap body, but still I get error when I've finiseh the
work in my interceptor. My code now looks like this

public void handleMessage(SoapMessage message) throws Fault {
InputStream is = message.getContent(InputStream.class);
    assert is != null;

    String soapMessage = null;
    try {
        soapMessage = IOUtils.toString(is, 10000);
    } catch (IOException e) {
        e.printStackTrace();

    }

    String encoding = (String) message.get(Message.ENCODING);

    XMLStreamReader reader = null;
    try {
        XMLInputFactory factory = getXMLInputFactory(message);
        if (factory == null) {
        reader = StaxUtils.createXMLStreamReader(is, encoding);
        } else {
        synchronized (factory) {
            reader = factory.createXMLStreamReader(is, encoding);
        }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }

  System.out.print(sopaMessage)
   message.setContent(XMLStreamReader.class, reader);

}

But still I get exception like this

INFO  - PhaseInterceptorChain          - Interceptor has thrown exception,
unwinding now Error reading XMLStreamReader.
INFO  - URIMappingInterceptor          - class
org.apache.cxf.binding.soap.interceptor.Soap12FaultOutInterceptorapplication/soap+xml

Any last tips, before  I almost give up this, it would be real nice for us
to get it to work.

help appreciated

cheers, håkon


2009/10/28 Freeman Fang <freeman.fang@gmail.com>

> Hi,
>
> You also need recreate the XMLStreamReader from the InputStream, and put it
> to the message, take a look at org.apache.cxf.interceptor.StaxInInterceptor.
>
> Or if you just want to get the soap message, I think in your interceptor,
> you can do something like
>
>
>        InputStream is = message.getContent(InputStream.class);
>        if (is != null) {
>            CachedOutputStream bos = new CachedOutputStream();
>            try {
>                IOUtils.copy(is, bos);
>
>                bos.flush();
>                is.close();
>                message.setContent(InputStream.class, bos.getInputStream());
>                bos.close();
>
>                //then parse the soapMessage to get what you want (soap body
> in your case)
>
>            } catch (IOException e) {
>                throw new Fault(e);
>            }
>        }
> This way you avoid consuming XMLStreamReader
>
> Freeman
>
>
> On 2009-10-28, at 下午7:52, Håkon Sagehaug wrote:
>
>  Hi
>>
>> Is there any other tricks or tips that can be tried? I would have thought
>> that other have done this before me, intercepting the message in their own
>> interceptors and read out both the soap header and soap body.
>>
>>
>> thanks,
>> Håkon
>> 2009/10/27 Håkon Sagehaug <Hakon.Sagehaug@bccs.uib.no>
>>
>>  Hi
>>>
>>> I now have this in my xbean
>>>
>>> <cxfbc:inInterceptors>
>>>       <bean
>>>               class="org.apache.cxf.interceptor.LoggingInInterceptor" />
>>>           <bean
>>>
>>> class="no.uib.bccs.esysbio.hakont.soap.interceptor.SoapHeaderInterceptor"
>>> />
>>>       </cxfbc:inInterceptors>
>>>
>>> as opposed to this before
>>>
>>> <cxfbc:inInterceptors>
>>>           <bean
>>>
>>> class="no.uib.bccs.esysbio.hakont.soap.interceptor.SoapHeaderInterceptor"
>>> />
>>>       </cxfbc:inInterceptors>
>>>
>>> But I get the same error message.  My costructor for the interceptor
>>> calls
>>> looks like this
>>>
>>>  public SoapHeaderInterceptor() {
>>>   super(Phase.READ);
>>>
>>>   }
>>>
>>> Do I add my interceptor in the wrong phase or?
>>>
>>>
>>> Håkon
>>>
>>> 2009/10/27 Freeman Fang <freeman.fang@gmail.com>
>>>
>>>  Hi,
>>>>
>>>> I think you need an interceptor to save re-readable inputstream(just
>>>> like
>>>> LoggingInInterceptor do)  at very early phase,  before create
>>>> XMLStreamReader(you can see this class is actually used to extract soap
>>>> body
>>>> in getBodyElement()method) from the InputStream.
>>>>
>>>> So just add LoggingInInterceptor with your customer interceptor for
>>>> inInterceptors list and do a quick test.
>>>>
>>>> Freeman
>>>>
>>>> On 2009-10-27, at 下午5:10, Håkon Sagehaug wrote:
>>>>
>>>> Hi,
>>>>
>>>>>
>>>>> No luck still the same error. I think I tried this before also.  I'm
>>>>> using
>>>>> smx 3.3.1 and the logging interceptor is working fine for me, any other
>>>>> tips
>>>>> ;) ?
>>>>>
>>>>> Håkon
>>>>> 2009/10/27 Freeman Fang <freeman.fang@gmail.com>
>>>>>
>>>>> Hi,
>>>>>
>>>>>>
>>>>>> You need set re-readable inputstream before the the getBodyElement
>>>>>>
>>>>>>
>>>>>>
>>>>>> On 2009-10-27, at 下午4:38, Håkon Sagehaug wrote:
>>>>>>
>>>>>> Hi
>>>>>>
>>>>>>
>>>>>>> thanks for the pointer, but it did not seem to help fro some
reason,
>>>>>>> the
>>>>>>> content of the message is always null i seems. This is how it
looks
>>>>>>> in
>>>>>>> my
>>>>>>> code
>>>>>>>
>>>>>>> //Get the stream
>>>>>>> InputStream is = arg0.getContent(InputStream.class);
>>>>>>>   CachedOutputStream bos = null;
>>>>>>>   if (is != null) {
>>>>>>>
>>>>>>>       bos = new CachedOutputStream();
>>>>>>>       //Copy it to the cachedoutput stream
>>>>>>>       IOUtils.copy(is, bos);
>>>>>>>       log.info("Set the new content ");
>>>>>>>
>>>>>>>   }
>>>>>>>
>>>>>>> so add
>>>>>>>
>>>>>>
>>>>>> bos.flush();
>>>>>>   is.close();
>>>>>>  // Set the new content
>>>>>>   arg0.setContent(InputStream.class, bos.getInputStream());
>>>>>>   bos.close();
>>>>>> before getBodyElement (as this method actually will consume the
>>>>>> inputstream, if the inputstream isn't re-readable, then you get null
>>>>>> afterwords)
>>>>>>
>>>>>> Freeman
>>>>>>
>>>>>>    //Get the content for the body
>>>>>>
>>>>>>    log.info("body info {}", getBodyElement(arg0));
>>>>>>>
>>>>>>>   bos.flush();
>>>>>>>   is.close();
>>>>>>>  // Set the new content
>>>>>>>   arg0.setContent(InputStream.class, bos.getInputStream());
>>>>>>>   bos.close();
>>>>>>>
>>>>>>>
>>>>>>> I'm I doing anything wrong here? Tried to place the point where
I
>>>>>>> extract
>>>>>>> the body almost everywhere in my code, but nothing seems to help.
If
>>>>>>> I
>>>>>>> remove the getBodyElement call everything works fine.
>>>>>>>
>>>>>>> Håkon
>>>>>>>
>>>>>>>
>>>>>>> 2009/10/27 Freeman Fang <freeman.fang@gmail.com>
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>>
>>>>>>>> I think the error comes from that the inputstream of message
is not
>>>>>>>> re-readable, so if you want to read the message content yourself,
>>>>>>>> ensure
>>>>>>>> that you already copy the inputstream and save it before
hand,
>>>>>>>> something
>>>>>>>> like
>>>>>>>>
>>>>>>>>  InputStream is = message.getContent(InputStream.class);
>>>>>>>>  if (is != null) {
>>>>>>>>      CachedOutputStream bos = new CachedOutputStream();
>>>>>>>>      try {
>>>>>>>>          IOUtils.copy(is, bos);
>>>>>>>>
>>>>>>>>          bos.flush();
>>>>>>>>          is.close();
>>>>>>>>
>>>>>>>>          message.setContent(InputStream.class,
>>>>>>>> bos.getInputStream());
>>>>>>>>
>>>>>>>>
>>>>>>>>          bos.close();
>>>>>>>>      } catch (IOException e) {
>>>>>>>>          throw new Fault(e);
>>>>>>>>      }
>>>>>>>>  }
>>>>>>>>
>>>>>>>> You can take a look at LoggingInInterceptor[1] as an example,
this
>>>>>>>> interceptor just print out the message content when receive
it and
>>>>>>>> will
>>>>>>>> not
>>>>>>>> affect other process afterwords, it's should be similar as
your
>>>>>>>> requirement
>>>>>>>> [1]
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> https://svn.apache.org/repos/asf/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
>>>>>>>>
>>>>>>>> Freeman
>>>>>>>>
>>>>>>>>
>>>>>>>> On 2009-10-27, at 上午12:12, Håkon Sagehaug wrote:
>>>>>>>>
>>>>>>>> hi,
>>>>>>>>
>>>>>>>>
>>>>>>>>  I copied the method and i was able to extract the message,
but then
>>>>>>>>> I
>>>>>>>>> got
>>>>>>>>> a
>>>>>>>>> new exception, this one
>>>>>>>>>
>>>>>>>>> ERROR - CxfBcComponent                 - Error processing
exchange
>>>>>>>>> InOut[
>>>>>>>>> id: ID:129.177.20.229-12490bff30f-2:27
>>>>>>>>> status: Active
>>>>>>>>> role: provider
>>>>>>>>> interface: {http://www.bccs.uib.no/
>>>>>>>>> EchoService.wsdl}EchoServicePortType
>>>>>>>>> service: {http://www.bccs.uib.no/EchoService.wsdl}EchoService<http://www.bccs.uib.no/EchoService.wsdl%7DEchoService>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DEchoService>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DEchoService>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DEchoService>
>>>>>>>>> endpoint: EchoServiceProxy
>>>>>>>>> operation: {http://www.bccs.uib.no/EchoService.wsdl}SayHi<http://www.bccs.uib.no/EchoService.wsdl%7DSayHi>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DSayHi>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DSayHi>
>>>>>>>>> <http://www.bccs.uib.no/EchoService.wsdl%7DSayHi>
>>>>>>>>>
>>>>>>>>> in: null
>>>>>>>>> ]
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> I guess the message for outgoing for some reason is null,
so I
>>>>>>>>> looked
>>>>>>>>> at
>>>>>>>>> [1]
>>>>>>>>> some more and added this to my code
>>>>>>>>>
>>>>>>>>> Document messageDocument = DomUtil.createDocument();
>>>>>>>>>
>>>>>>>>>  Element soapEnv = DomUtil.createElement(messageDocument,
>>>>>>>>>     new QName(soapVersion.getEnvelope().getNamespaceURI(),
>>>>>>>>>         soapVersion.getEnvelope().getLocalPart(),
>>>>>>>>>         soapVersion.getPrefix()));
>>>>>>>>>  Element soapBody = DomUtil.createElement(soapEnv, new
QName(
>>>>>>>>>     soapVersion.getBody().getNamespaceURI(), soapVersion
>>>>>>>>>         .getBody().getLocalPart(), soapVersion
>>>>>>>>>         .getPrefix()));
>>>>>>>>>  soapEnv.appendChild(soapBody);
>>>>>>>>>  Element body = getBodyElement(arg0);
>>>>>>>>>
>>>>>>>>>  soapBody.appendChild(soapBody.getOwnerDocument().importNode(
>>>>>>>>>     body, true));
>>>>>>>>>
>>>>>>>>>  arg0.setContent(Source.class, new DOMSource(messageDocument));
>>>>>>>>>
>>>>>>>>> But this gives also gives me an error,
>>>>>>>>>
>>>>>>>>> INFO  - PhaseInterceptorChain          - Interceptor
has thrown
>>>>>>>>> exception,
>>>>>>>>> unwinding now null
>>>>>>>>>
>>>>>>>>> Not sure what to do, I've tried changing the Phase I
have my
>>>>>>>>> Interceptor,
>>>>>>>>> first I had it in Phase.READ, and tried some others but
no luck.
>>>>>>>>>
>>>>>>>>> Any tips on how to solve it??
>>>>>>>>>
>>>>>>>>> cheers, Håkon
>>>>>>>>>
>>>>>>>>> 2009/10/26 Freeman Fang <freeman.fang@gmail.com>
>>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>  You can take a look at JbiInWsdl1Interceptor.getBodyElement()
[1]
>>>>>>>>>> as
>>>>>>>>>> an
>>>>>>>>>> example.
>>>>>>>>>> [1]
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> https://svn.apache.org/repos/asf/servicemix/components/bindings/servicemix-cxf-bc/trunk/src/main/java/org/apache/servicemix/cxfbc/interceptors/JbiInWsdl1Interceptor.java
>>>>>>>>>>
>>>>>>>>>> Freeman
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On 2009-10-26, at 下午10:09, Håkon Sagehaug wrote:
>>>>>>>>>>
>>>>>>>>>> Hi all,
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> I know this is not a specific servicemix question,
but hopefully
>>>>>>>>>>
>>>>>>>>>>> someone
>>>>>>>>>>> can
>>>>>>>>>>> answer it. I'm having cxf-bc that proxy out a
web service, and
>>>>>>>>>>> having
>>>>>>>>>>> a
>>>>>>>>>>> interceptor in he xbean configuration for  looking
for a pattern
>>>>>>>>>>> in
>>>>>>>>>>> the
>>>>>>>>>>> soap
>>>>>>>>>>> message. I can extract the soap header in the
soap envelope fine,
>>>>>>>>>>> but
>>>>>>>>>>> I
>>>>>>>>>>> couldn't figure out how to extract the soap body.
So does anyone
>>>>>>>>>>> have
>>>>>>>>>>> a
>>>>>>>>>>> tip
>>>>>>>>>>> how to easily extracting the soap body inside
the cxf
>>>>>>>>>>> interceptor?
>>>>>>>>>>>
>>>>>>>>>>> cheers, Håkon
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> Håkon Sagehaug, Scientific Programmer
>>>>>>>>>>> Parallab, Bergen Center for Computational Science
(BCCS)
>>>>>>>>>>> UNIFOB AS (University of Bergen Research Company)
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>>
>>>>>>>>>> Freeman Fang
>>>>>>>>>> ------------------------
>>>>>>>>>> Open Source SOA: http://fusesource.com
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>>
>>>>>>>>> Håkon Sagehaug, Scientific Programmer
>>>>>>>>> Parallab, Bergen Center for Computational Science (BCCS)
>>>>>>>>> UNIFOB AS (University of Bergen Research Company)
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>  --
>>>>>>>> Freeman Fang
>>>>>>>> ------------------------
>>>>>>>> Open Source SOA: http://fusesource.com
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>  --
>>>>>>> Håkon Sagehaug, Scientific Programmer
>>>>>>> Parallab, Bergen Center for Computational Science (BCCS)
>>>>>>> UNIFOB AS (University of Bergen Research Company)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> --
>>>>>> Freeman Fang
>>>>>> ------------------------
>>>>>> Open Source SOA: http://fusesource.com
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>> --
>>>>> Håkon Sagehaug, Scientific Programmer
>>>>> Parallab, Bergen Center for Computational Science (BCCS)
>>>>> UNIFOB AS (University of Bergen Research Company)
>>>>>
>>>>>
>>>>
>>>> --
>>>> Freeman Fang
>>>> ------------------------
>>>> Open Source SOA: http://fusesource.com
>>>>
>>>>
>>>>
>>>
>>> --
>>> Håkon Sagehaug, Scientific Programmer
>>> Parallab, Bergen Center for Computational Science (BCCS)
>>> UNIFOB AS (University of Bergen Research Company)
>>>
>>>
>>
>>
>> --
>> Håkon Sagehaug, Scientific Programmer
>> Parallab, Bergen Center for Computational Science (BCCS)
>> UNIFOB AS (University of Bergen Research Company)
>> Hakon.Sagehaug@bccs.uib.no, phone +47 55584125
>>
>
>
> --
> Freeman Fang
> ------------------------
> Open Source SOA: http://fusesource.com
>
>


-- 
Håkon Sagehaug, Scientific Programmer
Parallab, Bergen Center for Computational Science (BCCS)
UNIFOB AS (University of Bergen Research Company)
Hakon.Sagehaug@bccs.uib.no, phone +47 55584125

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message