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"));
}
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);
}
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);
}
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);
}
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;
}
Aggregations