Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AXIS2 Java Client for a SOAP wsdl with basic authentication

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.

like image 599
user3130143 Avatar asked Jul 04 '26 00:07

user3130143


1 Answers

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>
like image 104
Vic Avatar answered Jul 05 '26 14:07

Vic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!