You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by mahesh <ma...@mayoo.com> on 2011/07/06 18:42:53 UTC

Issue with modifying mesage via interceptor

Hi Colm,
   I recently tried to modify the soap prefixes using an interceptor and a
setcontent method for message. I thought this will update the message but it
appears that it is not doing that.Take a look at the code you can try this
interceptor on any client request.
   I choose this phase as at PRE-STREAM I get encrypted message.if you have
any examples that show how to modify the message (raw) that would be
Great!.Thanks for all your help.

/*---------------------------PrintOutMsg
.java-------------------------------- */

package com.trails.ws;

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.soap.SOAPMessage;



import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;

public class PrintOutMsg extends AbstractSoapInterceptor { 

    public PrintOutMsg() {
        super(Phase.USER_PROTOCOL);
    }

     public void handleMessage(SoapMessage message) throws Fault {
        SOAPMessage sm = message.getContent(SOAPMessage.class);
       
    
        try {
            //String values = sm.getSOAPBody().getTextContent(); 
           // System.out.println("Included values:" + values);  
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            sm.writeTo(out);
            String mysoapMessage = new String(out.toByteArray());
            System.out.println("------Soap Message START -------\n");
            System.out.println(mysoapMessage);
            System.out.println("------Soap Message END -------\n");
            System.out.println("------Soap Message ALTER START -------\n");
            mysoapMessage=mysoapMessage.replaceAll("soap:","s:");
            mysoapMessage=mysoapMessage.replaceAll("soap=","s=");
            System.out.println(mysoapMessage);
            System.out.println("------Soap Message ALTER END -------\n");
            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
            out2.write(mysoapMessage.getBytes("UTF-8"));
            message.setContent(String.class,mysoapMessage);
            // message.setContent(InputStream.class,new
ByteArrayInputStream(mysoapMessage.getBytes()));
            //message.setContent(OutputStream.class, out2);
            SOAPMessage sm2 = message.getContent(SOAPMessage.class);
            sm2.writeTo(System.out);
            //System.out.println("---UNENCRYPTED OUT MESSAGE START----\n");
            //sm.writeTo(System.out);
            //System.out.println("\n---UNENCRYPTED OUT MESSAGE END----\n"); 
        } catch (Exception e) {
            throw new Fault(e);
        } 
    }
}

//-----------------------End ------------------------------------------
Thank You
Mahesh


--
View this message in context: http://cxf.547215.n5.nabble.com/CXF-2-4-1-Client-is-giving-the-signature-or-decryption-was-invalid-tp4507027p4557345.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Issue with modifying mesage via interceptor

Posted by mahesh <ma...@mayoo.com>.
Aki the .Net Service I am calling requires specific prefix and namesapaces
removed
I did this finally with the code below.however there are additional two
namespaces of parent node appearing in child node at some phase when
ws-policy is being applied.I would like them removed
I am not what phase i can remove those. This seems to be the only issue
remaining for me 
I have fixed the prefix and namespaces for envelop and body using code
below.

<ExecuteRequest xmlns="http://www.xxx.gov/2008/09"
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

To

<ExecuteRequest xmlns="http://www.xxx.gov/2008/09">

public class PrintOutMsg extends AbstractSoapInterceptor { 

    public PrintOutMsg() {
        super(Phase.USER_PROTOCOL);
    }

     public void handleMessage(SoapMessage message) throws Fault {
        SOAPMessage sm = message.getContent(SOAPMessage.class);
       
    
        try {
            SOAPPart soapPart = sm.getSOAPPart();
            SOAPEnvelope soapEnv = soapPart.getEnvelope();
            soapEnv.setPrefix("s");
            soapEnv.removeNamespaceDeclaration("soap"); 
            soapEnv.addNamespaceDeclaration("s",
"http://www.w3.org/2003/05/soap-envelope") ; 
            SOAPHeader sh = sm.getSOAPHeader();
            sh.setPrefix("s");
            sh.removeNamespaceDeclaration("soap");
            SOAPBody sb = sm.getSOAPBody();
            sb.removeNamespaceDeclaration("soap"); 
            sb.setPrefix("s");
/* Does not impact at this phase need to know what phase to do rest above
works fine. Some time after initila security implemented and before body
encryption*/
            //NodeList list  = sb.getElementsByTagName("*");
            NodeList nl= sb.getChildNodes();
            //Node nd = nl.item(0);
            Element element = (Element)nl.item(0);
           
element.removeAttributeNS("http://www.w3.org/2003/05/soap-envelope","xmlns:s");
           
element.removeAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd","xmlns:wsu");
            System.out.println("NodeName:->" + element.getNodeName());
          
            
            
            
            //String values = sm.getSOAPBody().getTextContent(); 
           // System.out.println("Included values:" + values);  
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            sm.writeTo(out);
            String mysoapMessage = new String(out.toByteArray());
            System.out.println("------Soap Message START -------\n");
            System.out.println(mysoapMessage);

            /*
            System.out.println("------Soap Message END -------\n");
            System.out.println("------Soap Message ALTER START -------\n");
            mysoapMessage=mysoapMessage.replaceAll("soap:","s:");
            mysoapMessage=mysoapMessage.replaceAll("soap=","s=");
            System.out.println(mysoapMessage);
            System.out.println("------Soap Message ALTER END -------\n");
            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
            out2.write(mysoapMessage.getBytes("UTF-8"));
            message.setContent(String.class,mysoapMessage);
            */
            // message.setContent(InputStream.class,new
ByteArrayInputStream(mysoapMessage.getBytes()));
            //message.setContent(OutputStream.class, out2);
           /* SOAPMessage sm2 = message.getContent(SOAPMessage.class);
            sm2.writeTo(System.out);
            */
            //System.out.println("---UNENCRYPTED OUT MESSAGE START----\n");
            //sm.writeTo(System.out);
            //System.out.println("\n---UNENCRYPTED OUT MESSAGE END----\n"); 
        } catch (Exception e) {
            throw new Fault(e);
        } 
    }
}

--
View this message in context: http://cxf.547215.n5.nabble.com/CXF-2-4-1-Client-is-giving-the-signature-or-decryption-was-invalid-tp4507027p4561817.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Issue with modifying mesage via interceptor

Posted by Aki Yoshida <el...@googlemail.com>.
Hi,
Treating a SOAP message as a plain text and manipulating its namespace
prefixes using the string-replacing operaiton is not a good idea. You
are likely to corrupt the message.

If your objective is to use different prefixes for certain standard
namespaces in your serialized SOAP message, you may want to look at
the approach mentioned in the mail thread
http://cxf.547215.n5.nabble.com/Namespace-Prefixes-td4364266.html

regards, aki

2011/7/6 mahesh <ma...@mayoo.com>:
> Hi Colm,
>   I recently tried to modify the soap prefixes using an interceptor and a
> setcontent method for message. I thought this will update the message but it
> appears that it is not doing that.Take a look at the code you can try this
> interceptor on any client request.
>   I choose this phase as at PRE-STREAM I get encrypted message.if you have
> any examples that show how to modify the message (raw) that would be
> Great!.Thanks for all your help.
>
> /*---------------------------PrintOutMsg
> .java-------------------------------- */
>
> package com.trails.ws;
>
> import java.io.BufferedOutputStream;
>
> import java.io.ByteArrayInputStream;
> import java.io.ByteArrayOutputStream;
> import java.io.InputStream;
> import java.io.OutputStream;
>
> import javax.xml.soap.SOAPMessage;
>
>
>
> import org.apache.cxf.binding.soap.SoapMessage;
> import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
> import org.apache.cxf.interceptor.Fault;
> import org.apache.cxf.phase.Phase;
>
> public class PrintOutMsg extends AbstractSoapInterceptor {
>
>    public PrintOutMsg() {
>        super(Phase.USER_PROTOCOL);
>    }
>
>     public void handleMessage(SoapMessage message) throws Fault {
>        SOAPMessage sm = message.getContent(SOAPMessage.class);
>
>
>        try {
>            //String values = sm.getSOAPBody().getTextContent();
>           // System.out.println("Included values:" + values);
>            ByteArrayOutputStream out = new ByteArrayOutputStream();
>            sm.writeTo(out);
>            String mysoapMessage = new String(out.toByteArray());
>            System.out.println("------Soap Message START -------\n");
>            System.out.println(mysoapMessage);
>            System.out.println("------Soap Message END -------\n");
>            System.out.println("------Soap Message ALTER START -------\n");
>            mysoapMessage=mysoapMessage.replaceAll("soap:","s:");
>            mysoapMessage=mysoapMessage.replaceAll("soap=","s=");
>            System.out.println(mysoapMessage);
>            System.out.println("------Soap Message ALTER END -------\n");
>            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
>            out2.write(mysoapMessage.getBytes("UTF-8"));
>            message.setContent(String.class,mysoapMessage);
>            // message.setContent(InputStream.class,new
> ByteArrayInputStream(mysoapMessage.getBytes()));
>            //message.setContent(OutputStream.class, out2);
>            SOAPMessage sm2 = message.getContent(SOAPMessage.class);
>            sm2.writeTo(System.out);
>            //System.out.println("---UNENCRYPTED OUT MESSAGE START----\n");
>            //sm.writeTo(System.out);
>            //System.out.println("\n---UNENCRYPTED OUT MESSAGE END----\n");
>        } catch (Exception e) {
>            throw new Fault(e);
>        }
>    }
> }
>
> //-----------------------End ------------------------------------------
> Thank You
> Mahesh
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/CXF-2-4-1-Client-is-giving-the-signature-or-decryption-was-invalid-tp4507027p4557345.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>