Search in sources :

Example 41 with Handler

use of javax.xml.ws.handler.Handler in project cxf by apache.

the class JaxWsClientTest method testLogicalHandler.

@Test
public void testLogicalHandler() {
    URL url = getClass().getResource("/wsdl/hello_world.wsdl");
    javax.xml.ws.Service s = javax.xml.ws.Service.create(url, SERVICE_NAME);
    Greeter greeter = s.getPort(PORT_NAME, Greeter.class);
    d.setMessageObserver(new MessageReplayObserver("sayHiResponse.xml"));
    // JAX-WS api doesn't specify this as List<Handler<? extends MessageContext>>
    @SuppressWarnings("rawtypes") List<Handler> chain = ((BindingProvider) greeter).getBinding().getHandlerChain();
    chain.add(new LogicalHandler<LogicalMessageContext>() {

        public void close(MessageContext arg0) {
        }

        public boolean handleFault(LogicalMessageContext arg0) {
            return true;
        }

        public boolean handleMessage(LogicalMessageContext context) {
            Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outbound) {
                headers = CastUtils.cast((Map<?, ?>) context.get(MessageContext.HTTP_REQUEST_HEADERS));
                if (headers == null) {
                    headers = new HashMap<>();
                    context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
                }
                headers.put("My-Custom-Header", Collections.singletonList("value"));
            }
            return true;
        }
    });
    ((BindingProvider) greeter).getBinding().setHandlerChain(chain);
    String response = greeter.sayHi();
    assertNotNull(response);
    assertTrue("custom header should be present", headers.containsKey("My-Custom-Header"));
    assertTrue("existing SOAPAction header should not be removed", headers.containsKey("SOAPAction"));
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) HashMap(java.util.HashMap) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) InvocationHandler(java.lang.reflect.InvocationHandler) URL(java.net.URL) Greeter(org.apache.hello_world_soap_http.Greeter) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 42 with Handler

use of javax.xml.ws.handler.Handler in project cxf by apache.

the class ServiceImpl method createPort.

protected <T> T createPort(QName portName, EndpointReferenceType epr, Class<T> serviceEndpointInterface, WebServiceFeature... features) {
    LOG.log(Level.FINE, "creating port for portName", portName);
    LOG.log(Level.FINE, "endpoint reference:", epr);
    LOG.log(Level.FINE, "endpoint interface:", serviceEndpointInterface);
    JaxWsProxyFactoryBean proxyFac = new JaxWsProxyFactoryBean();
    JaxWsClientFactoryBean clientFac = (JaxWsClientFactoryBean) proxyFac.getClientFactoryBean();
    JaxWsServiceFactoryBean serviceFactory = (JaxWsServiceFactoryBean) proxyFac.getServiceFactory();
    List<WebServiceFeature> f = getAllFeatures(features);
    proxyFac.initFeatures();
    if (f != null) {
        serviceFactory.setWsFeatures(f);
    }
    proxyFac.setBus(bus);
    proxyFac.setServiceClass(serviceEndpointInterface);
    proxyFac.setServiceName(serviceName);
    if (epr != null && epr.getAddress() != null && epr.getAddress().getValue() != null) {
        clientFac.setAddress(epr.getAddress().getValue());
    }
    if (wsdlURL != null) {
        proxyFac.setWsdlURL(wsdlURL);
    }
    configureObject(proxyFac);
    configureObject(clientFac);
    if (portName == null) {
        QName portTypeName = getPortTypeName(serviceEndpointInterface);
        Service service = serviceFactory.getService();
        if (service == null) {
            serviceFactory.setServiceClass(serviceEndpointInterface);
            serviceFactory.setBus(getBus());
            service = serviceFactory.create();
        }
        EndpointInfo ei = ServiceModelUtil.findBestEndpointInfo(portTypeName, service.getServiceInfos());
        if (ei != null) {
            portName = ei.getName();
        } else {
            portName = serviceFactory.getEndpointName();
        }
    }
    serviceFactory.setEndpointName(portName);
    if (epr != null) {
        clientFac.setEndpointReference(epr);
    }
    PortInfoImpl portInfo = portInfos.get(portName);
    if (portInfo != null) {
        clientFac.setBindingId(portInfo.getBindingID());
        clientFac.setAddress(portInfo.getAddress());
    }
    // configureObject(portName.toString() + ".jaxws-client.proxyFactory", proxyFac);
    if (clazz != ServiceImpl.class) {
        // handlerchain should be on the generated Service object
        proxyFac.setLoadHandlers(false);
    }
    Object obj = proxyFac.create();
    // Configure the Service
    Service service = serviceFactory.getService();
    configureObject(service);
    // Configure the JaxWsEndpoitnImpl
    Client client = ClientProxy.getClient(obj);
    client.getEndpoint().setExecutor(executor);
    client.setExecutor(executor);
    JaxWsEndpointImpl jaxwsEndpoint = (JaxWsEndpointImpl) client.getEndpoint();
    configureObject(jaxwsEndpoint);
    @SuppressWarnings("rawtypes") List<Handler> hc = jaxwsEndpoint.getJaxwsBinding().getHandlerChain();
    hc.addAll(handlerResolver.getHandlerChain(portInfos.get(portName)));
    jaxwsEndpoint.getJaxwsBinding().setHandlerChain(hc);
    LOG.log(Level.FINE, "created proxy", obj);
    if (portInfo == null) {
        addPort(portName, clientFac.getBindingId(), clientFac.getAddress());
    }
    return serviceEndpointInterface.cast(obj);
}
Also used : JaxWsServiceFactoryBean(org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) WebService(javax.jws.WebService) Handler(javax.xml.ws.handler.Handler) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) JaxWsEndpointImpl(org.apache.cxf.jaxws.support.JaxWsEndpointImpl) WebServiceFeature(javax.xml.ws.WebServiceFeature) PortInfoImpl(org.apache.cxf.jaxws.handler.PortInfoImpl) Client(org.apache.cxf.endpoint.Client)

Example 43 with Handler

use of javax.xml.ws.handler.Handler in project cxf by apache.

the class JaxWsServerFactoryBean method buildHandlerChain.

/**
 * Obtain handler chain from annotations.
 * @param server
 */
private void buildHandlerChain(Server server) {
    AnnotationHandlerChainBuilder builder = new AnnotationHandlerChainBuilder();
    @SuppressWarnings("rawtypes") List<Handler> chain = new ArrayList<>(handlers);
    chain.addAll(builder.buildHandlerChainFromClass(getServiceBeanClass(), server.getEndpoint().getEndpointInfo().getName(), server.getEndpoint().getService().getName(), this.getBindingId()));
    for (Handler<?> h : chain) {
        injectResources(h);
    }
    ((JaxWsEndpointImpl) server.getEndpoint()).getJaxwsBinding().setHandlerChain(chain);
}
Also used : AnnotationHandlerChainBuilder(org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder) ArrayList(java.util.ArrayList) Handler(javax.xml.ws.handler.Handler)

Example 44 with Handler

use of javax.xml.ws.handler.Handler in project cxf by apache.

the class AnnotationHandlerChainBuilder method buildHandlerChainFromClass.

/**
 * @param clz
 * @param existingHandlers
 * @return
 */
public List<Handler> buildHandlerChainFromClass(Class<?> clz, List<Handler> existingHandlers, QName portQName, QName serviceQName, String bindingID) {
    LOG.fine("building handler chain");
    HandlerChainAnnotation hcAnn = findHandlerChainAnnotation(clz, true);
    final List<Handler> chain;
    if (hcAnn == null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("no HandlerChain annotation on " + clz);
        }
        chain = new ArrayList<>();
    } else {
        hcAnn.validate();
        try {
            URL handlerFileURL = resolveHandlerChainFile(clz, hcAnn.getFileName());
            if (handlerFileURL == null) {
                throw new WebServiceException(new Message("HANDLER_CFG_FILE_NOT_FOUND_EXC", BUNDLE, hcAnn.getFileName()).toString());
            }
            Document doc = StaxUtils.read(handlerFileURL.openStream());
            Element el = doc.getDocumentElement();
            boolean isJavaEENamespace = JavaeeHandlerChainBuilder.JAVAEE_NS.equals(el.getNamespaceURI());
            boolean isJakartaEENamespace = JakartaeeHandlerChainBuilder.JAKARTAEE_NS.equals(el.getNamespaceURI());
            if (!isJavaEENamespace && !isJakartaEENamespace) {
                throw new WebServiceException(BundleUtils.getFormattedString(BUNDLE, "NOT_VALID_NAMESPACE", el.getNamespaceURI()));
            }
            final ClassLoader classLoader = getClassLoader(clz);
            final DelegatingHandlerChainBuilder delegate = ht -> buildHandlerChain(ht, classLoader);
            if (isJavaEENamespace) {
                chain = new JavaeeHandlerChainBuilder(BUNDLE, handlerFileURL, delegate).build(el, portQName, serviceQName, bindingID);
            } else {
                chain = new JakartaeeHandlerChainBuilder(BUNDLE, handlerFileURL, delegate).build(el, portQName, serviceQName, bindingID);
            }
        } catch (WebServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new WebServiceException(BUNDLE.getString("CHAIN_NOT_SPECIFIED_EXC"), e);
        }
    }
    assert chain != null;
    if (existingHandlers != null) {
        chain.addAll(existingHandlers);
    }
    return sortHandlers(chain);
}
Also used : WebService(javax.jws.WebService) Bus(org.apache.cxf.Bus) URL(java.net.URL) StringUtils(org.apache.cxf.common.util.StringUtils) PrivilegedAction(java.security.PrivilegedAction) Logger(java.util.logging.Logger) ClassLoaderUtils(org.apache.cxf.common.classloader.ClassLoaderUtils) Message(org.apache.cxf.common.i18n.Message) HandlerChain(javax.jws.HandlerChain) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) List(java.util.List) Handler(javax.xml.ws.handler.Handler) BundleUtils(org.apache.cxf.common.i18n.BundleUtils) WebServiceException(javax.xml.ws.WebServiceException) Element(org.w3c.dom.Element) ResourceBundle(java.util.ResourceBundle) LogUtils(org.apache.cxf.common.logging.LogUtils) Document(org.w3c.dom.Document) QName(javax.xml.namespace.QName) AccessController(java.security.AccessController) StaxUtils(org.apache.cxf.staxutils.StaxUtils) WebServiceException(javax.xml.ws.WebServiceException) Message(org.apache.cxf.common.i18n.Message) Element(org.w3c.dom.Element) Handler(javax.xml.ws.handler.Handler) Document(org.w3c.dom.Document) URL(java.net.URL) WebServiceException(javax.xml.ws.WebServiceException)

Example 45 with Handler

use of javax.xml.ws.handler.Handler in project cxf by apache.

the class FirstClient method newInstance.

public static FirstServiceAT newInstance() throws Exception {
    URL wsdlLocation = new URL("http://localhost:8081/Service/FirstServiceAT?wsdl");
    QName serviceName = new QName("http://service.ws.sample", "FirstServiceATService");
    QName portName = new QName("http://service.ws.sample", "FirstServiceAT");
    Service service = Service.create(wsdlLocation, serviceName);
    FirstServiceAT client = service.getPort(portName, FirstServiceAT.class);
    List<Handler> handlerChain = new ArrayList<>();
    JaxWSTxOutboundBridgeHandler txOutboundBridgeHandler = new JaxWSTxOutboundBridgeHandler();
    EnabledWSTXHandler wstxHandler = new EnabledWSTXHandler();
    handlerChain.add(txOutboundBridgeHandler);
    handlerChain.add(wstxHandler);
    ((BindingProvider) client).getBinding().setHandlerChain(handlerChain);
    return client;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Service(javax.xml.ws.Service) Handler(javax.xml.ws.handler.Handler) EnabledWSTXHandler(com.arjuna.mw.wst11.client.EnabledWSTXHandler) JaxWSTxOutboundBridgeHandler(org.jboss.jbossts.txbridge.outbound.JaxWSTxOutboundBridgeHandler) JaxWSTxOutboundBridgeHandler(org.jboss.jbossts.txbridge.outbound.JaxWSTxOutboundBridgeHandler) EnabledWSTXHandler(com.arjuna.mw.wst11.client.EnabledWSTXHandler) URL(java.net.URL)

Aggregations

Handler (javax.xml.ws.handler.Handler)100 QName (javax.xml.namespace.QName)47 ArrayList (java.util.ArrayList)46 BindingProvider (javax.xml.ws.BindingProvider)36 URL (java.net.URL)32 Service (javax.xml.ws.Service)29 Test (org.junit.Test)29 LogicalHandler (javax.xml.ws.handler.LogicalHandler)24 Binding (javax.xml.ws.Binding)11 WebServiceException (javax.xml.ws.WebServiceException)11 MessageContext (javax.xml.ws.handler.MessageContext)9 SOAPHandler (javax.xml.ws.handler.soap.SOAPHandler)9 Exchange (org.apache.cxf.message.Exchange)9 IOException (java.io.IOException)8 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)7 HandlerChainInvoker (org.apache.cxf.jaxws.handler.HandlerChainInvoker)7 DataHandler (javax.activation.DataHandler)6 InitialContext (javax.naming.InitialContext)6 Source (javax.xml.transform.Source)6 DOMSource (javax.xml.transform.dom.DOMSource)6