Search code examples
web-servicesjakarta-eesoapwebspherewebservice-client

getting an error with webservices


Im doing restfull webservices with soap using websphere and RAD. After generating my javaclient classes when I run the test class I get the following error. Ive been searching the web but not finding the correct solution. PLEASE HELP!

ERROR:

Check method call
org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
    at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
    at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
    at com.ibm.ws.webservices.engine.xmlsoap.SOAPPart.appendChild(SOAPPart.java:282)
    at com.sun.xml.internal.bind.marshaller.SAX2DOMEx.startElement(SAX2DOMEx.java:177)
    at com.sun.xml.internal.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:159)
    at com.sun.xml.internal.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:194)
    at com.sun.xml.internal.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:80)
    at test.CustomSoapHandler.handleMessage(CustomSoapHandler.java:37)
    at test.CustomSoapHandler.handleMessage(CustomSoapHandler.java:1)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:293)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:134)
    at com.sun.xml.internal.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:139)
    at com.sun.xml.internal.ws.handler.HandlerTube.processRequest(HandlerTube.java:117)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:599)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:558)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:543)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:440)
    at com.sun.xml.internal.ws.client.Stub.process(Stub.java:223)
    at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:136)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:110)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:90)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:119)
    at $Proxy33.getHistory(Unknown Source)
    at test.ServiceTest.historyTest(ServiceTest.java:64)
    at test.ServiceTest.main(ServiceTest.java:100)
Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:227)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:90)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:119)
    at $Proxy33.getHistory(Unknown Source)
    at test.ServiceTest.historyTest(ServiceTest.java:64)
    at test.ServiceTest.main(ServiceTest.java:100)
Caused by: java.lang.ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl incompatible with com.sun.xml.internal.bind.api.JAXBRIContext
    at java.lang.ClassCastException.<init>(ClassCastException.java:58)
    at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.<clinit>(SOAPFaultBuilder.java:545)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:205)
    ... 6 more

Test Class:

 package test;

import be.ipc.css.ws.GcssWebServiceService;
import be.ipc.css.ws.IGcssWebService;
import be.ipc.css.ws.InvalidItemIdStructureFault_Exception;
import be.ipc.css.ws.ProductNotAllowedFault_Exception;
import be.ipc.css.ws.common.Product;
import be.ipc.css.ws.history.GetHistoryInput;
import be.ipc.css.ws.history_output.GetHistoryOutput;
import be.ipc.css.ws.history_output.HistoryItem;
import be.ipc.css.ws.history_output.Type;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * <strong>Project: TODO: project name</strong><br/>
 * <b>Url:</b> TODO: url<br/>
 * <b>Date:</b> 15.05.14<br/>
 * <b>Time:</b> 23:42 <br/>
 * Copyright(C) 2014 <a href="https://itserviceplus.net">IT Service Plus</a> <br/>
 * <b>Description:</b><br/>
 * TODO: description
 */
public class ServiceTest {
    private static Service webService;
    private static IGcssWebService servicePort;

    public void initService() throws MalformedURLException {
        URL url = new URL("http://cs-demo.ipc.be/CSS_UA2/services/gcssWebService/1.0");
        QName qname = new QName("http://ws.css.ipc.be/", "GcssWebServiceService");

        /*java.util.Properties props = System.getProperties();
        props.setProperty("http.proxyHost", "proxy.test.com");
        props.setProperty("http.proxyPort", "8080");*/

        webService = GcssWebServiceService.create(url, qname);
        servicePort = webService.getPort(IGcssWebService.class);

        try {
            CustomSoapHandler sh = new CustomSoapHandler("user_us", "*******");
            List<Handler> new_handlerChain = new ArrayList<Handler>();
            new_handlerChain.add(sh);
            ((BindingProvider)servicePort).getBinding().setHandlerChain(new_handlerChain);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        //((BindingProvider)servicePort).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user_us");
        //((BindingProvider)servicePort).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "*******");
    }


    public void historyTest() throws ProductNotAllowedFault_Exception, InvalidItemIdStructureFault_Exception {
        GetHistoryInput input = new GetHistoryInput();
        input.setItemId("CC027607063NL");
        input.setProduct(Product.EPG);
        System.out.println("Check method call");
        GetHistoryOutput output = servicePort.getHistory(input);
        System.out.println("Check result");
        //Assert.assertNotNull(output);
        //Assert.assertNotNull(output.getHistory());
        //Assert.assertNotNull(output.getHistory().getHistoryItem());

        System.out.println("Received: " + output.getHistory().getHistoryItem().size() + "elements:");
        System.out.println("-----------------------");
        for(int i=0;i<output.getHistory().getHistoryItem().size();i++) {
            HistoryItem it = output.getHistory().getHistoryItem().get(i);

            System.out.println("#" + i + ": id=" + it.getId() + ", type=" + it.getType().value());
        }

        System.out.println("Check 3 history items");
        //Assert.assertEquals(3, output.getHistory().getHistoryItem().size());

        HistoryItem it1 = output.getHistory().getHistoryItem().get(0);
        HistoryItem it2 = output.getHistory().getHistoryItem().get(1);
        HistoryItem it3 = output.getHistory().getHistoryItem().get(2);

        /*Assert.assertEquals(Type.QUMQ, it1.getType());
        Assert.assertEquals(161L, it1.getId());

        Assert.assertEquals(Type.SUM, it2.getType());
        Assert.assertEquals(652L, it2.getId());

        Assert.assertEquals(Type.L_1_Q, it3.getType());
        Assert.assertEquals(13742L, it3.getId());*/
    }

    public static void main(String[] args) {
        ServiceTest test = new ServiceTest();

        try {
            test.initService();
            test.historyTest();
        } catch(Exception e ){
            e.printStackTrace();
        }
    }
}

Soap Handler:

    package test;

import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.util.Set;

/**
 * <strong>Project: TODO: project name</strong><br/>
 * <b>Url:</b> TODO: url<br/>
 * <b>Date:</b> 16.05.14<br/>
 * <b>Time:</b> 0:48 <br/>
 * Copyright(C) 2014 <a href="https://itserviceplus.net">IT Service Plus</a> <br/>
 * <b>Description:</b><br/>
 * TODO: description
 */
public class CustomSoapHandler implements SOAPHandler<SOAPMessageContext> {

    private static final String AUTH_PREFIX = "wsse";
    private static final String AUTH_NS =
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

    private String username;
    private String password;

    public CustomSoapHandler(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean handleMessage(SOAPMessageContext context) {

        try {
            SOAPEnvelope envelope =
                    context.getMessage().getSOAPPart().getEnvelope();
            SOAPFactory soapFactory = SOAPFactory.newInstance();
            SOAPElement wsSecHeaderElm =
                    soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS);
            Name wsSecHdrMustUnderstandAttr =
                    soapFactory.createName("mustUnderstand", "S",
                            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            wsSecHeaderElm.addAttribute(wsSecHdrMustUnderstandAttr, "1");
            SOAPElement userNameTokenElm =
                    soapFactory.createElement("UsernameToken", AUTH_PREFIX,
                            AUTH_NS);
            Name userNameTokenIdName =
                    soapFactory.createName("id", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
            userNameTokenElm.addAttribute(userNameTokenIdName,
                    "UsernameToken-ORbTEPzNsEMDfzrI9sscVA22");
            SOAPElement userNameElm =
                    soapFactory.createElement("Username", AUTH_PREFIX, AUTH_NS);
            userNameElm.addTextNode(username);
            SOAPElement passwdElm =
                    soapFactory.createElement("Password", AUTH_PREFIX, AUTH_NS);
            Name passwdTypeAttr = soapFactory.createName("Type");
            passwdElm.addAttribute(passwdTypeAttr,
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            passwdElm.addTextNode(password);
            userNameTokenElm.addChildElement(userNameElm);
            userNameTokenElm.addChildElement(passwdElm);
            wsSecHeaderElm.addChildElement(userNameTokenElm);
            if (envelope.getHeader() == null) {
                SOAPHeader sh = envelope.addHeader();
                sh.addChildElement(wsSecHeaderElm);
            } else {
                SOAPHeader sh = envelope.getHeader();
                sh.addChildElement(wsSecHeaderElm);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void close(MessageContext context) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Set<QName> getHeaders() {
        return null;
    }
}

Solution

  • The main reason for your error is incompatible JAXB implementation classes:

    aused by: java.lang.ClassCastException: com.ibm.xml.xlxp2.jaxb.JAXBContextImpl incompatible with com.sun.xml.internal.bind.api.JAXBRIContext
    

    Probably the best way to fix this is packaging your own version of JAXB within your application(inside the lib folder) and than change the class loader from WebSphere to be Parent Last.

    After that, restart and try it again. If still doesn't work you can try adding your JAXB implementation libraries directly to the Application Server classloader. You can do that creating a directory under $WEBSPHERE_HOME/AppServer/classes and placing your JAXB implementation classes there. Be aware that this approach adds the dropped jars to all WebSphere instances running using this binary codebase.

    You can learn more about WebSphere classloaders.

    Hope this helps.