Search in sources :

Example 11 with WebServiceFeature

use of javax.xml.ws.WebServiceFeature in project Payara by payara.

the class WebServiceReferenceManagerImpl method resolveWSReference.

public Object resolveWSReference(ServiceReferenceDescriptor desc, Context context) throws NamingException {
    // Taken from NamingManagerImpl.getClientServiceObject
    Class serviceInterfaceClass = null;
    Object returnObj = null;
    WsUtil wsUtil = new WsUtil();
    // Implementation for new lookup element in WebserviceRef
    InitialContext iContext = new InitialContext();
    if (desc.hasLookupName()) {
        return iContext.lookup(desc.getLookupName());
    }
    try {
        WSContainerResolver.set(desc);
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        serviceInterfaceClass = cl.loadClass(desc.getServiceInterface());
        resolvePortComponentLinks(desc);
        javax.xml.rpc.Service serviceDelegate = null;
        javax.xml.ws.Service jaxwsDelegate = null;
        Object injValue = null;
        if (desc.hasGeneratedServiceInterface() || desc.hasWsdlFile()) {
            String serviceImplName = desc.getServiceImplClassName();
            if (serviceImplName != null) {
                Class serviceImplClass = cl.loadClass(serviceImplName);
                serviceDelegate = (javax.xml.rpc.Service) serviceImplClass.newInstance();
            } else {
                // as the interface through DD
                if (javax.xml.ws.Service.class.isAssignableFrom(serviceInterfaceClass) && !javax.xml.ws.Service.class.equals(serviceInterfaceClass)) {
                    // OK - the interface class is indeed the generated service class; get an instance
                    injValue = initiateInstance(serviceInterfaceClass, desc);
                } else {
                    // interface, therefore I take the first one.
                    if (desc.isInjectable()) {
                        InjectionTarget target = desc.getInjectionTargets().iterator().next();
                        Class serviceType = null;
                        if (target.isFieldInjectable()) {
                            java.lang.reflect.Field f = target.getField();
                            if (f == null) {
                                String fName = target.getFieldName();
                                Class targetClass = cl.loadClass(target.getClassName());
                                try {
                                    f = targetClass.getDeclaredField(fName);
                                }// ignoring exception
                                 catch (java.lang.NoSuchFieldException nsfe) {
                                }
                            }
                            if (f != null) {
                                serviceType = f.getType();
                            }
                        }
                        if (target.isMethodInjectable()) {
                            Method m = target.getMethod();
                            if (m == null) {
                                String mName = target.getMethodName();
                                Class targetClass = cl.loadClass(target.getClassName());
                                try {
                                    m = targetClass.getDeclaredMethod(mName);
                                }// ignoring exception
                                 catch (java.lang.NoSuchMethodException nsfe) {
                                }
                            }
                            if (m != null && m.getParameterTypes().length == 1) {
                                serviceType = m.getParameterTypes()[0];
                            }
                        }
                        if (serviceType != null) {
                            Class loadedSvcClass = cl.loadClass(serviceType.getCanonicalName());
                            injValue = initiateInstance(loadedSvcClass, desc);
                        }
                    }
                }
                // Unable to get hold of generated service class -> try the Service.create avenue to get a Service
                if (injValue == null) {
                    // Here create the service with WSDL (overridden wsdl if wsdl-override is present)
                    // so that JAXWS runtime uses this wsdl @ runtime
                    javax.xml.ws.Service svc = javax.xml.ws.Service.create((new WsUtil()).privilegedGetServiceRefWsdl(desc), desc.getServiceName());
                    jaxwsDelegate = new JAXWSServiceDelegate(desc, svc, cl);
                }
            }
            if (desc.hasHandlers()) {
                // We need the service's ports to configure the
                // handler chain (since service-ref handler chain can
                // optionally specify handler-port association)
                // so create a configured service and call getPorts
                javax.xml.rpc.Service configuredService = wsUtil.createConfiguredService(desc);
                Iterator ports = configuredService.getPorts();
                wsUtil.configureHandlerChain(desc, serviceDelegate, ports, cl);
            }
            // check if this is a post 1.1 web service
            if (javax.xml.ws.Service.class.isAssignableFrom(serviceInterfaceClass)) {
                // This is a JAXWS based webservice client;
                // process handlers and mtom setting
                // moved test for handlers into wsUtil, in case
                // we have to add system handler
                javax.xml.ws.Service service = (injValue != null ? (javax.xml.ws.Service) injValue : jaxwsDelegate);
                if (service != null) {
                    // Now configure client side handlers
                    wsUtil.configureJAXWSClientHandlers(service, desc);
                }
                // the requested resource is not the service but one of its port.
                if (injValue != null && desc.getInjectionTargetType() != null) {
                    Class requestedPortType = service.getClass().getClassLoader().loadClass(desc.getInjectionTargetType());
                    ArrayList<WebServiceFeature> wsFeatures = getWebServiceFeatures(desc);
                    if (wsFeatures.size() > 0) {
                        injValue = service.getPort(requestedPortType, wsFeatures.toArray(new WebServiceFeature[wsFeatures.size()]));
                    } else {
                        injValue = service.getPort(requestedPortType);
                    }
                }
            }
        } else {
            // Generic service interface / no WSDL
            QName serviceName = desc.getServiceName();
            if (serviceName == null) {
                // ServiceFactory API requires a service-name.
                // However, 109 does not allow getServiceName() to be
                // called, so it's ok to use a dummy value.
                serviceName = new QName("urn:noservice", "servicename");
            }
            ServiceFactory serviceFac = ServiceFactory.newInstance();
            serviceDelegate = serviceFac.createService(serviceName);
        }
        // Create a proxy for the service object.
        // Get a proxy only in jaxrpc case because in jaxws the service class is not
        // an interface any more
        InvocationHandler handler = null;
        if (serviceDelegate != null) {
            handler = new ServiceInvocationHandler(desc, serviceDelegate, cl);
            returnObj = Proxy.newProxyInstance(cl, new Class[] { serviceInterfaceClass }, handler);
        } else if (jaxwsDelegate != null) {
            returnObj = jaxwsDelegate;
        } else if (injValue != null) {
            returnObj = injValue;
        }
    } catch (PrivilegedActionException pae) {
        logger.log(Level.WARNING, LogUtils.EXCEPTION_THROWN, pae);
        NamingException ne = new NamingException();
        ne.initCause(pae.getCause());
        throw ne;
    } catch (Exception e) {
        logger.log(Level.WARNING, LogUtils.EXCEPTION_THROWN, e);
        NamingException ne = new NamingException();
        ne.initCause(e);
        throw ne;
    } finally {
        WSContainerResolver.unset();
    }
    return returnObj;
}
Also used : ServiceFactory(javax.xml.rpc.ServiceFactory) java.lang.reflect(java.lang.reflect) Iterator(java.util.Iterator) NamingException(javax.naming.NamingException) PrivilegedActionException(java.security.PrivilegedActionException) QName(javax.xml.namespace.QName) Service(org.jvnet.hk2.annotations.Service) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) PrivilegedActionException(java.security.PrivilegedActionException) WebServiceException(javax.xml.ws.WebServiceException) WebServiceFeature(javax.xml.ws.WebServiceFeature)

Example 12 with WebServiceFeature

use of javax.xml.ws.WebServiceFeature in project Payara by payara.

the class WebServiceReferenceManagerImpl method getWebServiceFeatures.

private ArrayList<WebServiceFeature> getWebServiceFeatures(ServiceReferenceDescriptor desc) {
    /**
     * JAXWS 2.2 enables @MTOM, @Addressing @RespectBinding
     * on WebServiceRef
     * If these are present use the
     * Service(url,wsdl,features) constructor
     */
    ArrayList<WebServiceFeature> wsFeatures = new ArrayList<WebServiceFeature>();
    if (desc.isMtomEnabled()) {
        wsFeatures.add(new MTOMFeature(true, desc.getMtomThreshold()));
    }
    com.sun.enterprise.deployment.Addressing add = desc.getAddressing();
    if (add != null) {
        wsFeatures.add(new AddressingFeature(add.isEnabled(), add.isRequired(), getResponse(add.getResponses())));
    }
    com.sun.enterprise.deployment.RespectBinding rb = desc.getRespectBinding();
    if (rb != null) {
        wsFeatures.add(new RespectBindingFeature(rb.isEnabled()));
    }
    Map<Class<? extends Annotation>, Annotation> otherAnnotations = desc.getOtherAnnotations();
    Iterator it = otherAnnotations.values().iterator();
    while (it.hasNext()) {
        wsFeatures.add(getWebServiceFeatureBean((Annotation) it.next()));
    }
    return wsFeatures;
}
Also used : AddressingFeature(javax.xml.ws.soap.AddressingFeature) MTOMFeature(javax.xml.ws.soap.MTOMFeature) ArrayList(java.util.ArrayList) WebServiceFeatureAnnotation(javax.xml.ws.spi.WebServiceFeatureAnnotation) Annotation(java.lang.annotation.Annotation) RespectBindingFeature(javax.xml.ws.RespectBindingFeature) WebServiceFeature(javax.xml.ws.WebServiceFeature) Iterator(java.util.Iterator) com.sun.enterprise.deployment(com.sun.enterprise.deployment)

Example 13 with WebServiceFeature

use of javax.xml.ws.WebServiceFeature in project Payara by payara.

the class WebServiceReferenceManagerImpl method getWebServiceFeatureBean.

private WebServiceFeature getWebServiceFeatureBean(Annotation a) {
    WebServiceFeatureAnnotation wsfa = a.annotationType().getAnnotation(WebServiceFeatureAnnotation.class);
    Class<? extends WebServiceFeature> beanClass = wsfa.bean();
    WebServiceFeature bean;
    Constructor ftrCtr = null;
    String[] paramNames = null;
    for (Constructor con : beanClass.getConstructors()) {
        FeatureConstructor ftrCtrAnn = (FeatureConstructor) con.getAnnotation(FeatureConstructor.class);
        if (ftrCtrAnn != null) {
            if (ftrCtr == null) {
                ftrCtr = con;
                paramNames = ftrCtrAnn.value();
            } else {
                throw new WebServiceException(ModelerMessages.RUNTIME_MODELER_WSFEATURE_MORETHANONE_FTRCONSTRUCTOR(a, beanClass));
            }
        }
    }
    if (ftrCtr == null) {
        throw new WebServiceException(ModelerMessages.RUNTIME_MODELER_WSFEATURE_NO_FTRCONSTRUCTOR(a, beanClass));
    }
    if (ftrCtr.getParameterTypes().length != paramNames.length) {
        throw new WebServiceException(ModelerMessages.RUNTIME_MODELER_WSFEATURE_ILLEGAL_FTRCONSTRUCTOR(a, beanClass));
    }
    try {
        Object[] params = new Object[paramNames.length];
        for (int i = 0; i < paramNames.length; i++) {
            Method m = a.annotationType().getDeclaredMethod(paramNames[i]);
            params[i] = m.invoke(a);
        }
        bean = (WebServiceFeature) ftrCtr.newInstance(params);
    } catch (Exception e) {
        throw new WebServiceException(e);
    }
    return bean;
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) FeatureConstructor(com.sun.xml.ws.api.FeatureConstructor) FeatureConstructor(com.sun.xml.ws.api.FeatureConstructor) NamingException(javax.naming.NamingException) PrivilegedActionException(java.security.PrivilegedActionException) WebServiceException(javax.xml.ws.WebServiceException) WebServiceFeatureAnnotation(javax.xml.ws.spi.WebServiceFeatureAnnotation) WebServiceFeature(javax.xml.ws.WebServiceFeature)

Example 14 with WebServiceFeature

use of javax.xml.ws.WebServiceFeature in project cxf by apache.

the class JaxWsServiceFactoryBeanTest method testMtomFeature.

@Test
public void testMtomFeature() throws Exception {
    JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
    bean.setBus(getBus());
    bean.setServiceClass(GreeterImpl.class);
    bean.setWsdlURL(getClass().getResource("/wsdl/hello_world.wsdl"));
    bean.setWsFeatures(Arrays.asList(new WebServiceFeature[] { new MTOMFeature() }));
    Service service = bean.create();
    Endpoint endpoint = service.getEndpoints().values().iterator().next();
    assertTrue(endpoint instanceof JaxWsEndpointImpl);
    Binding binding = ((JaxWsEndpointImpl) endpoint).getJaxwsBinding();
    assertTrue(binding instanceof SOAPBinding);
    assertTrue(((SOAPBinding) binding).isMTOMEnabled());
}
Also used : Binding(javax.xml.ws.Binding) SOAPBinding(javax.xml.ws.soap.SOAPBinding) Endpoint(org.apache.cxf.endpoint.Endpoint) MTOMFeature(javax.xml.ws.soap.MTOMFeature) WebServiceFeature(javax.xml.ws.WebServiceFeature) Service(org.apache.cxf.service.Service) WebService(javax.jws.WebService) SOAPBinding(javax.xml.ws.soap.SOAPBinding) Test(org.junit.Test) AbstractJaxWsTest(org.apache.cxf.jaxws.AbstractJaxWsTest)

Example 15 with WebServiceFeature

use of javax.xml.ws.WebServiceFeature in project cxf by apache.

the class EndpointReferenceTest method testServiceGetPortUsingEndpointReference.

/*
     * Any JAX-WS supported epr metadata MUST match the Service instances
     * ServiceName, otherwise a WebServiceExeption MUST be thrown. Any JAX-WS
     * supported epr metadata MUST match the PortName for the sei, otherwise a
     * WebServiceException MUST be thrown. If the Service instance has an
     * associated WSDL, its WSDL MUST be used to determine any binding
     * information, anyWSDL in a JAX-WS suppported epr metadata MUST be ignored.
     * If the Service instance does not have a WSDL, then any WSDL inlined in
     * the JAX-WS supported metadata of the epr MUST be used to determine
     * binding information. If there is not enough metadata in the Service
     * instance or in the epr metadata to determine a port, then a
     * WebServiceException MUST be thrown.
     */
@Test
public void testServiceGetPortUsingEndpointReference() throws Exception {
    BusFactory.setDefaultBus(getBus());
    GreeterImpl greeter1 = new GreeterImpl();
    try (EndpointImpl endpoint = new EndpointImpl(getBus(), greeter1, (String) null)) {
        endpoint.publish("http://localhost:8080/test");
        javax.xml.ws.Service s = javax.xml.ws.Service.create(new QName("http://apache.org/hello_world_soap_http", "SoapPort"));
        InputStream is = getClass().getResourceAsStream("resources/hello_world_soap_http_infoset.xml");
        Document doc = StaxUtils.read(is);
        DOMSource erXML = new DOMSource(doc);
        EndpointReference endpointReference = EndpointReference.readFrom(erXML);
        WebServiceFeature[] wfs = new WebServiceFeature[] {};
        Greeter greeter = s.getPort(endpointReference, Greeter.class, wfs);
        String response = greeter.greetMe("John");
        assertEquals("Hello John", response);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) EndpointReference(javax.xml.ws.EndpointReference) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) Greeter(org.apache.hello_world_soap_http.Greeter) GreeterImpl(org.apache.hello_world_soap_http.GreeterImpl) WebServiceFeature(javax.xml.ws.WebServiceFeature) Test(org.junit.Test)

Aggregations

WebServiceFeature (javax.xml.ws.WebServiceFeature)30 ArrayList (java.util.ArrayList)11 MTOMFeature (javax.xml.ws.soap.MTOMFeature)9 QName (javax.xml.namespace.QName)8 URL (java.net.URL)7 WebServiceException (javax.xml.ws.WebServiceException)7 AddressingFeature (javax.xml.ws.soap.AddressingFeature)7 MalformedURLException (java.net.MalformedURLException)6 RespectBindingFeature (javax.xml.ws.RespectBindingFeature)6 LinkedList (java.util.LinkedList)4 Service (javax.xml.ws.Service)4 InputStream (java.io.InputStream)3 NamingException (javax.naming.NamingException)3 DOMSource (javax.xml.transform.dom.DOMSource)3 EndpointReference (javax.xml.ws.EndpointReference)3 Bus (org.apache.cxf.Bus)3 Test (org.junit.Test)3 WSBinding (com.sun.xml.ws.api.WSBinding)2 PrivilegedActionException (java.security.PrivilegedActionException)2 Iterator (java.util.Iterator)2