vendredi 4 août 2017

CXF Proxy Web Service

I am a newbie to java servlet filters and I have a CXF web service A.

The address of A is: http://localhost:8080/AdditionWS/services/additioncls?wsdl

I want to implement a proxy web service B.

The address of B is: http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl

To forward the requests to A through B, I added a servlet filter to B:

public class RequestFilter implements Filter {
public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain)
        throws java.io.IOException, javax.servlet.ServletException {

    HttpServletResponse resp = (HttpServletResponse) response;
    String redirect="http://localhost:8080/AdditionWS/services/additioncls";
    Enumeration<String> enumeration=request.getParameterNames();
    String parameter=null;
    while(enumeration.hasMoreElements())
    {
        parameter=enumeration.nextElement();
        if(parameter.toLowerCase()=="wsdl")
        break;
    }
    // if the parameter 'wsdl' exists forward to http://localhost:8080/AdditionWS/services/additioncls?wsdl
    if(parameter!=null)
        redirect="http://localhost:8080/AdditionWS/services/additioncls?"+"wsdl";
    redirect=resp.encodeRedirectURL(redirect);
    resp.sendRedirect(redirect);
 }
}

and I updated the web address at the client code like the following:

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;
//before: 
wsdlLocation=http://localhost:8080/AdditionWS/services/additioncls?wsdl
@WebServiceClient(name = "AdditionClsService", 
              wsdlLocation = "http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl",
              targetNamespace = "http://logic/") 
public class AdditionClsService extends Service {
public final static URL WSDL_LOCATION;

public final static QName SERVICE = new QName("http://logic/", "AdditionClsService");
public final static QName AdditionClsPort = new QName("http://logic/", "AdditionClsPort");
static {
    URL url = null;
    try {
// before url=new URL("http://localhost:8080/AdditionWS/services/additioncls?wsdl");
        url = new URL("http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl");
    } catch (MalformedURLException e) {
        java.util.logging.Logger.getLogger(AdditionClsService.class.getName())
            .log(java.util.logging.Level.INFO, 
                 "Can not initialize the default wsdl from {0}", "http://localhost:8080/AdditionWS/services/additioncls?wsdl");
    }
    WSDL_LOCATION = url;
}

public AdditionClsService(URL wsdlLocation) {
    super(wsdlLocation, SERVICE);
}

public AdditionClsService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

public AdditionClsService() {
    super(WSDL_LOCATION, SERVICE);
}

public AdditionClsService(WebServiceFeature ... features) {
    super(WSDL_LOCATION, SERVICE, features);
}

public AdditionClsService(URL wsdlLocation, WebServiceFeature ... features) {
    super(wsdlLocation, SERVICE, features);
}

public AdditionClsService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
    super(wsdlLocation, serviceName, features);
}    

/**
 *
 * @return
 *     returns AdditionWSSEI
 */
@WebEndpoint(name = "AdditionClsPort")
public AdditionWSSEI getAdditionClsPort() {
    return super.getPort(AdditionClsPort, AdditionWSSEI.class);
}

/**
 * 
 * @param features
 *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
 * @return
 *     returns AdditionWSSEI
 */
@WebEndpoint(name = "AdditionClsPort")
public AdditionWSSEI getAdditionClsPort(WebServiceFeature... features) {
    return super.getPort(AdditionClsPort, AdditionWSSEI.class, features);
}
}

For some reason, the client requests are not forwarded to A and I got the following errors:

Exception in thread "main" javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
    at org.apache.cxf.jaxws.ServiceImpl.initialize(ServiceImpl.java:162)
    at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:129)
    at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:82)
    at javax.xml.ws.Service.<init>(Service.java:77)
    at business.AdditionClsService.<init>(AdditionClsService.java:43)
    at business.AdditionWSSEI_AdditionClsPort_Client.main(AdditionWSSEI_AdditionClsPort_Client.java:49)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
    at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:87)
    at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:217)
    at org.apache.cxf.jaxws.ServiceImpl.initialize(ServiceImpl.java:160)
    ... 5 more
Caused by: javax.wsdl.WSDLException: WSDLException: faultCode=PARSER_ERROR: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
 at [row,col,system-id]: [1,0,"http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl"]
    at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:228)
    at org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition(WSDLManagerImpl.java:163)
    at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:85)
    ... 7 more
Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
 at [row,col,system-id]: [1,0,"http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl"]
    at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:685)
    at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2141)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2047)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
    at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1369)
    at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1263)
    at org.apache.cxf.staxutils.StaxUtils.read(StaxUtils.java:1191)
    at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:219)
    ... 9 more

Did I miss something or is there a different way to implement a proxy cxf web service that will forward client requests to A or one of its duplicates? Thank you!

Aucun commentaire:

Enregistrer un commentaire