3

I am a newbie to web services and I am trying to create java client using the auto generated stub by Eclipse Web Service Client using Axis2. Following is my

client code. public static void main(String[] args) {
        new wsClient2().runService();
    }

public void runService(){

    try {

    CoreStub.COREEnvelopeRealTimeRequest req = new CoreStub.COREEnvelopeRealTimeRequest();
    CoreStub.COREEnvelopeRealTimeResponse res = new CoreStub.COREEnvelopeRealTimeResponse();

    req.setCORERuleVersion("2.2.0");
    req.setPayload("....some data over here...... Can't disclose");
    req.setPayloadID("..payload id goes here... can't disclose");
    req.setPayloadType("X12_276_Request_005010X212");

    RealTimeMode rtm = new RealTimeMode();
    rtm.setRealTimeMode("RealTime");
    req.setProcessingMode(rtm);
    req.setReceiverID("myreceiverid");
    req.setSenderID("mysenderid");
    req.setTimeStamp("2015-04-14 10:27:47");



    HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator();
    basicAuthentication.setUsername("myusername");
    basicAuthentication.setPassword("mypassword");
    basicAuthentication.setPreemptiveAuthentication(true);

    CoreStub _stub = new CoreStub();
    ServiceClient clientservice = _stub._getServiceClient();


    _stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthentication);
    _stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, "false");


    OMFactory factory2 = OMAbstractFactory.getOMFactory();

    OMNamespace SecurityElementNamespace = factory2.createOMNamespace("http://schemas.xmlsoap.org/ws/2002/12/secext", "wsse");

    OMElement omSecurityElement = factory2.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);

    //OMElement usernameEl = factory2.createOMElement(new QName("", "Username", "wsse"), null);
    OMElement usernameEl = factory2.createOMElement(new QName("", "Username", "wsse"), null);

    usernameEl.setText("myusername");

    OMElement passwordEl = factory2.createOMElement(new QName("", "Password", "wsse"), null);
    passwordEl.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
    passwordEl.setText("mypassword");

    OMElement usernameTokenEl = factory2.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);

    usernameTokenEl.addChild(usernameEl);
    usernameTokenEl.addChild(passwordEl);

    omSecurityElement.addChild(usernameTokenEl);

    clientservice.addHeader(omSecurityElement);

    try {
        DisableSSLCertificateCheck();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Test");
    //System.out.println(clientservice.getLastOperationContext().getMessageContext("Out").getEnvelope().toString());

    System.out.println("Request created as: " + req.toString());

    res = _stub.realTimeTransaction(req);

    System.out.println("Version : " + res.getCORERuleVersion());
    System.out.println("Error Code :" + res.getErrorCode());
    System.out.println("Error Message:" + res.getErrorMessage());
    //System.out.println("Pay Load :" + res.getPayload());
    //System.out.println("Pay Load Type :" + res.getPayloadType());
    System.out.println("Receiver Id :" + res.getReceiverID());
    System.out.println("Sender Id :" + res.getSenderID());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

And I am getting following exception

java.lang.IllegalArgumentException: Cannot create a prefixed element with an empty namespace name at org.apache.axiom.om.impl.llom.OMElementImpl.handleNamespace(OMElementImpl.java:186) at org.apache.axiom.om.impl.llom.OMElementImpl.(OMElementImpl.java:161) at org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory.createOMElement(OMLinkedListImplFactory.java:126) at org.myownpackage.www.soap.wsdl.wsClient2.runService(wsClient2.java:81) at org.myownpackage.www.soap.wsdl.wsClient2.main(wsClient2.java:37)

In the following lines, having blank string is not allowed. What should I have have here to resolve this issue

  OMElement usernameEl = factory2.createOMElement(
                               new QName("", "Username", "wsse"), null
                             );

Faster help will be greatly appreciated. Thank you in advance.

2 Answers2

1

Did you try wrapping the username and password nodes in a user token node as in this question

Also, looking at the oasis docs which suggests:

<wsse:UsernameToken ...>
  <wsse:Username> ... </wsse:Username>
  <wsse:Password Type="..."> ... </wsse:Password>
  ...
</wsse:UsernameToken>
Community
  • 1
  • 1
Vic
  • 417
  • 2
  • 7
  • check the statement: omSecurityElement.addChild(usernameTokenEl); from the code I placed in the question. – user3130143 May 03 '15 at 04:42
  • Oh yes, I see. Try setting it to `createOMElement(new QName("", "Username", "wsu"), null)` because **wsu** is set by the parent `usernameTokenEl`. Why it does not know that **wsse** is set by `omSecurityElement`, I don't know. – Vic May 04 '15 at 16:04
0

You shouldn't use

new QName("", "Username", "wsse")

for producing the without xmlns explicitly set to the XML tag. Instead, you should set the namespace value for the Username exactly the same as it was used for it's parent, UserToken. When the XML will be generated this coinciding namespaces will be taken inot account and the child element will be produced without any xmlns part prduced to the tags, exactly as you desired. So this dirty code below within the Stub class, generated by the axis2:wsdl2code, helped me:

    public void addWsSecurityHeader(String wsUser, String wsPass)
        {

    OMFactory omFactory = OMAbstractFactory.getOMFactory();
    OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);
            omSecurityElement.addAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-%20wssecurity-utility-1.0.xsd",  null);


    OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken", "wsse"), null);
    omusertoken.addAttribute("wsu:Id","UsernameToken-87",null );

    OMElement omuserName = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Username", "wsse"), null);
    omuserName.setText(wsUser);

    OMElement omPassword = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Password", "wsse"), null);
    omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
    omPassword.setText(wsPass);

    omusertoken.addChild(omuserName);
    omusertoken.addChild(omPassword);
    omSecurityElement.addChild(omusertoken);
    this._getServiceClient().addHeader(omSecurityElement);
}

But i haven't fount how to set several xmlns for the same tag, so I made a dirty hack to create the second xmlns as a tag attribute. May be I was wrong, but it worked.

Ilya Yevlampiev
  • 2,330
  • 2
  • 28
  • 47