0

I've been struggling recently to create a simple soap web service client in Java. I used several tools from different implementations of jax-ws to generate a client from WSDL. Even though the client gets generated, whenever I try to call a method on that service I never receive the result from that method (there's never a return after calling such method).

Sample code:

try {
      TerytWs1Locator locator = new TerytWs1Locator();
      ITerytWs1 service = locator.getcustom();
      // If authorization is required
      ((CustomStub)service).setUsername("XXX");
      ((CustomStub)service).setPassword("YYY");
      // invoke business method
      System.out.println(service.czyZalogowany());
    } catch (javax.xml.rpc.ServiceException ex) {
      ex.printStackTrace();
    } catch (java.rmi.RemoteException ex) {
      ex.printStackTrace();
    }

So in this case println is never called as the program stucks on service.czyZalogowany() method.

I thought that maybe the service is not running, so I generated the sample client in .NET:

try
    {
    var proxy = new ChannelFactory<ServiceReference1.ITerytWs1>("custom");
    proxy.Credentials.UserName.UserName = "XXX";
    proxy.Credentials.UserName.Password = "YYY";
    var result = proxy.CreateChannel(); 
    var test = result.CzyZalogowany();
    }catch (Exception ex) { }

And I can easly call any method with the desired outcome.

What is wrong with my Java code?

Here's the wsdl for that service: https://uslugaterytws1test.stat.gov.pl/wsdl/terytws1.wsdl

vers
  • 29
  • 5

1 Answers1

0

This one works for me.

private String user = "XXX";
private String passwordSTR = "XXX";

private final String WSSE = "wsse";
private final String WSA = "wsa";

private final String TEM_URI="http://tempuri.org/";
private final String TEM="tem";
private final String IS_LOGGED = "CzyZalogowany";
private String SERVICE_PATH = "https://uslugaterytws1test.stat.gov.pl/TerytWs1.svc";

TerytServiceWS(){
}

public boolean isLogged(){
    WebServiceTemplate wsTempl = new WebServiceTemplate();
    wsTempl.setDefaultUri(SERVICE_PATH);
    String result = "false";
    WebServiceMessage message;
    try {
        message = (WebServiceMessage) wsTempl.sendAndReceive(new WebServiceMessageCallback() {
            public void doWithMessage(WebServiceMessage message) {
                try {
                    SaajSoapMessage soapMessage = (SaajSoapMessage) message;
                    SOAPMessage smc = soapMessage.getSaajMessage();
                    SOAPHeader header = smc.getSOAPHeader();

                    SOAPElement security =
                            header.addChildElement("Security", WSSE, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

                    SOAPElement usernameToken =
                            security.addChildElement("UsernameToken", WSSE);
                    usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                    SOAPElement username =
                            usernameToken.addChildElement("Username", WSSE);
                    username.addTextNode(user);

                    SOAPElement password =
                            usernameToken.addChildElement("Password", WSSE);
                    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
                    password.addTextNode(passwordSTR);

                    SOAPElement action =
                            header.addChildElement("Action", WSA, "http://www.w3.org/2005/08/addressing");
                    action.addTextNode("http://tempuri.org/ITerytWs1/"+IS_LOGGED);

                    SOAPBody soapBody = smc.getSOAPBody();
                    QName isLoggedQName = new QName(TEM_URI, IS_LOGGED, TEM);
                    soapBody.addChildElement(isLoggedQName);


                    ((SaajSoapMessage) message).setSaajMessage(smc);


                } catch (Exception e) {

                }
            }
        }, new WebServiceMessageExtractor() {
            public Object extractData(WebServiceMessage message) throws IOException {
                return message;
            }
        });

        SOAPMessage saajMessage = ((SaajSoapMessage) message).getSaajMessage();
        SOAPBody soapBody = saajMessage.getSOAPBody();
        NodeList responseNodeList = soapBody.getChildNodes();
        for (int count = 0; count < responseNodeList.getLength(); count++) {
            org.w3c.dom.Node tempNode = responseNodeList.item(count);
            if (tempNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                if (tempNode.getLocalName().equals("CzyZalogowanyResponse")) {
                    org.w3c.dom.Node responseNode = tempNode.getFirstChild();
                    if (responseNode.getLocalName().equals("CzyZalogowanyResult")) {
                        if (responseNode.getFirstChild()!=null)
                            result = responseNode.getFirstChild().getTextContent();
                    }
                }
            }
            else{
                return false;
            }
        }
    }catch (Exception e){

    }
    return Boolean.valueOf(result);
}