4

How to remove mustunderstand attribute from soap header in axis client.even i dont set it especially, when i set soap header info mustundertand and actor attributes are automatically added to soap message.Does anybody know how to remove them ? I am using Axis2 1.4 version's wsdl2java to create my ws client.

cacert
  • 2,677
  • 10
  • 33
  • 56

6 Answers6

8

None of those solutions worked for me, as:

Looking at the answer to "Adding ws-security to wsdl2java generated classes" helped me to write a solution that worked for me:

void addSecurityHeader(Stub stub, final String username, final String password) {
  QName headerName = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");  // Or any other namespace that fits in your case
  AtomicReference<SOAPHeaderElement> header 
    = new AtomicReference<SOAPHeaderElement>
        (new SOAPHeaderElement(headerName) {                       
           {  
             SOAPElement utElem = addChildElement("UsernameToken");
             utElem.addChildElement("Username").setValue(username);
             utElem.addChildElement("Password").setValue(password);
           }
           @Override
           public void setAttribute(String namespace, String localName, String value) {
             if (!Constants.ATTR_MUST_UNDERSTAND.equals(localName)) {  // Or any other attribute name you'd want to avoid
               super.setAttribute(namespace, localName, value);
             }
           }
        });
  SOAPHeaderElement soapHeaderElement = header.get();
  soapHeaderElement.setActor(null);      // No intermediate actors are involved.
  stub.setHeader(soapHeaderElement);  // Finally, attach the header to the stub
}
Community
  • 1
  • 1
Jérôme Beau
  • 10,608
  • 5
  • 48
  • 52
  • saves my life... thnx – masay Dec 26 '13 at 10:32
  • This worked perfectly! Thanks. I didn't want any child nodes, hence I used this: new SOAPHeaderElement( headerName) { { setValue(value); } – atripathi May 13 '14 at 11:37
  • Thanks! This one should be accepted as a valid answer for this question. The SOAPHeaderElement class adds by default mustUnderstand and actor attributes to the header, and this is (so far) the only way to remove it, by filtering adding/setting it in the first place. – Cutberto Ocampo Jan 06 '17 at 16:53
6

If you want to disable the must understand check in the AXIS client you have to add the following line to your code:

_call.setProperty(Call.CHECK_MUST_UNDERSTAND, new Boolean(false));

then the MustUnderstandChecker of the AXIS Client is never invoked.

Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
Reinhard
  • 61
  • 1
  • 2
2

In my case it worked manually remove the attribute from the SOAPHeader

SOAPHeader header = env.getHeader();
OMChildrenQNameIterator childrenWithName = (OMChildrenQNameIterator) header.getChildrenWithName(omElementauthentication.getQName());
    while (childrenWithName.hasNext()) {
        org.apache.axiom.om.OMElement omElement = (org.apache.axiom.om.OMElement) childrenWithName.next();
        QName mustAnderstandQName = omElement.resolveQName("soapenv:mustUnderstand");
        if (mustAnderstandQName == null) {
            continue;
        }
        OMAttribute mustAnderstandAttribute = omElement.getAttribute(mustAnderstandQName);
            if (mustAnderstandAttribute == null) {
                continue;
            }
        omElement.removeAttribute(mustAnderstandAttribute);
    }
joselufo
  • 3,393
  • 3
  • 23
  • 37
1

I was recently struggling with similar situation and by doing some google-ing I managed to find the following solution.

Having used Axis2 you would've probably generated a MyWSStub file that contains the calls to your service. Create an wrapper class (better not touch the auto-generated stub files) that extends your stub e.g. MyWSStubWrapper:

public class MyWSStubWrapper extends MyWSStub {

/**
 * @throws AxisFault
 */
public MyWSStubWrapper() throws AxisFault {
}

/**
 * @param configurationContext
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext) throws AxisFault {
    super(configurationContext);
}

/**
 * @param targetEndpoint
 * @throws AxisFault
 */
public MyWSStubWrapper(String targetEndpoint) throws AxisFault {
    super(targetEndpoint);
}

/**
 * @param configurationContext
 * @param targetEndpoint
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext, String targetEndpoint) throws AxisFault {
    super(configurationContext, targetEndpoint);
}

/**
 * @param configurationContext
 * @param targetEndpoint
 * @param useSeparateListener
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext, String targetEndpoint, boolean useSeparateListener) throws AxisFault {
    super(configurationContext, targetEndpoint, useSeparateListener);
}

@Override
protected void addHeader(OMElement omElementToadd, SOAPEnvelope envelop, boolean mustUnderstand) {
    SOAPHeaderBlock soapHeaderBlock = envelop.getHeader().addHeaderBlock(omElementToadd.getLocalName(), omElementToadd.getNamespace());
    OMNode omNode = null;

    // add child elements
    for (Iterator iter = omElementToadd.getChildren(); iter.hasNext();) {
        omNode = (OMNode) iter.next();
        iter.remove();
        soapHeaderBlock.addChild(omNode);
    }

    OMAttribute omatribute = null;
    // add attributes
    for (Iterator iter = omElementToadd.getAllAttributes(); iter.hasNext();) {
        omatribute = (OMAttribute) iter.next();
        soapHeaderBlock.addAttribute(omatribute);
    }
}

}

Bear in mind that the above code will completely remove the soapenv:mustUnderstand="0|1" from your headers if you wish to added you need to call soapHeaderBlock.setMustUnderstand(true|false); somewhere in your code.

tazarov
  • 41
  • 3
0

i am using axis 1.4 client with ws security

in my case as Reinhard said this worked

MyService service = new MyServiceLocator(); 
MyServicePortType port = service.getMyServiceHttpsSoap11Endpoint();

((Stub) port)._setProperty(Call.CHECK_MUST_UNDERSTAND, Boolean.FALSE);
A C S
  • 137
  • 8
0

1) Are you using the Axis SOAPHeaderElement and if so, are you setting the mustUnderstand setter to false?

2) Since you're generating your client with wsdl2java, it's quite possible that the WSDL (or more accurately, the schema) contains the mustUnderstand attribute on an element referenced in your SOAP binding. So when wsdlToJava generates the client code, those attributes will naturally be added. See here for a description of the mustUnderstand attribute. If modifying the WSDL is out of the question, and you must remove this attribute from the header, then I suppose you can try to do it with a handler

3) Not advisable, but if you really MUST remove this attribute then I suppose you can add a client side handler that alters the header: http://ws.apache.org/axis/java/apiDocs/org/apache/axis/handlers/package-summary.html

Simeon G
  • 1,188
  • 8
  • 20
  • 1)Yes I am using SOAPHeaderElement but i tried with both with not setting mustunderstand and settign it to false.nothing changed actually. my code like this: SOAPHeaderElement header = new SOAPHeaderElement( "ns1://x.x.x.x/abc.asmx", "AuthHeader"); SOAPElement node = header.addChildElement("user_1"); node.addTextNode("abc"); SOAPElement node2 = header.addChildElement("pass_1"); node2.addTextNode("123"); _call.addHeader(header); – cacert Sep 19 '11 at 16:49
  • When you say nothing changed, do you mean that the SOAP header is always showing up with mustUnderstand=false? Do you have the WSSDL/schema you could post (don't post the whole thing if it's very long... just relevant sections). – Simeon G Sep 19 '11 at 17:44
  • yes i mean exactly that.i think axis codes add mustunderstand and actor properties in default behaviour when soap header parameter set.When i remove the code for soap header than mustunderstand and actor parameters also removed. – cacert Sep 20 '11 at 07:57
  • related wsdl part may be – cacert Sep 20 '11 at 08:27