use of org.apache.cxf.jaxrs.client.Client in project camel by apache.
the class CxfRsProducer method invokeProxyClient.
protected void invokeProxyClient(Exchange exchange) throws Exception {
Message inMessage = exchange.getIn();
Object[] varValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
String methodName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
Client target = null;
JAXRSClientFactoryBean cfb = clientFactoryBeanCache.get(CxfEndpointUtils.getEffectiveAddress(exchange, ((CxfRsEndpoint) getEndpoint()).getAddress()));
Bus bus = ((CxfRsEndpoint) getEndpoint()).getBus();
// We need to apply the bus setting from the CxfRsEndpoint which is not use the default bus
if (bus != null) {
cfb.setBus(bus);
}
if (varValues == null) {
target = cfb.create();
} else {
target = cfb.createWithValues(varValues);
}
setupClientHeaders(target, exchange);
// find out the method which we want to invoke
JAXRSServiceFactoryBean sfb = cfb.getServiceFactory();
sfb.getResourceClasses();
// check the null body first
Object[] parameters = null;
if (inMessage.getBody() != null) {
parameters = inMessage.getBody(Object[].class);
}
// get the method
Method method = findRightMethod(sfb.getResourceClasses(), methodName, getParameterTypes(parameters));
// handle cookies
CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
loadCookies(exchange, target, cookieHandler);
// Will send out the message to
// Need to deal with the sub resource class
Object response = method.invoke(target, parameters);
int statesCode = target.getResponse().getStatus();
// handle cookies
saveCookies(exchange, target, cookieHandler);
if (throwException) {
if (response instanceof Response) {
Integer respCode = ((Response) response).getStatus();
if (respCode > 207) {
throw populateCxfRsProducerException(exchange, (Response) response, respCode);
}
}
}
CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
CxfRsBinding binding = cxfRsEndpoint.getBinding();
if (exchange.getPattern().isOutCapable()) {
LOG.trace("Response body = {}", response);
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
exchange.getOut().setBody(binding.bindResponseToCamelBody(response, exchange));
exchange.getOut().getHeaders().putAll(binding.bindResponseHeadersToCamelHeaders(response, exchange));
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, statesCode);
} else {
// just close the input stream of the response object
if (response instanceof Response) {
((Response) response).close();
}
}
}
use of org.apache.cxf.jaxrs.client.Client in project camel by apache.
the class CxfRsProducer method invokeAsyncProxyClient.
protected void invokeAsyncProxyClient(Exchange exchange, final AsyncCallback callback) throws Exception {
Message inMessage = exchange.getIn();
Object[] varValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
String methodName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
Client target;
JAXRSClientFactoryBean cfb = clientFactoryBeanCache.get(CxfEndpointUtils.getEffectiveAddress(exchange, ((CxfRsEndpoint) getEndpoint()).getAddress()));
Bus bus = ((CxfRsEndpoint) getEndpoint()).getBus();
// We need to apply the bus setting from the CxfRsEndpoint which is not use the default bus
if (bus != null) {
cfb.setBus(bus);
}
if (varValues == null) {
target = cfb.create();
} else {
target = cfb.createWithValues(varValues);
}
setupClientHeaders(target, exchange);
// find out the method which we want to invoke
JAXRSServiceFactoryBean sfb = cfb.getServiceFactory();
sfb.getResourceClasses();
// check the null body first
Object[] parameters = null;
if (inMessage.getBody() != null) {
parameters = inMessage.getBody(Object[].class);
}
// get the method
Method method = findRightMethod(sfb.getResourceClasses(), methodName, getParameterTypes(parameters));
CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
final CxfProxyInvocationCallback invocationCallback = new CxfProxyInvocationCallback(target, exchange, cxfRsEndpoint, callback);
WebClient.getConfig(target).getRequestContext().put(InvocationCallback.class.getName(), invocationCallback);
// handle cookies
CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
loadCookies(exchange, target, cookieHandler);
method.invoke(target, parameters);
}
use of org.apache.cxf.jaxrs.client.Client in project ddf by codice.
the class SecureCxfClientFactoryTest method testInsecureClient.
@Test
public void testInsecureClient() throws SecurityServiceException {
// positive case
SecureCxfClientFactory<IDummy> secureCxfClientFactory = new SecureCxfClientFactory<>(INSECURE_ENDPOINT, IDummy.class);
Client unsecuredClient = WebClient.client(secureCxfClientFactory.getClient());
assertTrue(unsecuredClient.getBaseURI().toASCIIString().equals(INSECURE_ENDPOINT));
// negative cases
boolean subject = true;
secureCxfClientFactory.getClientForSubject(getSubject());
assertTrue(subject);
boolean system = true;
secureCxfClientFactory.getClient();
assertTrue(system);
boolean unsecured = true;
secureCxfClientFactory.getClient();
assertTrue(unsecured);
}
use of org.apache.cxf.jaxrs.client.Client in project ddf by codice.
the class SecureCxfClientFactoryTest method testHttpsWebClient.
@Test
public void testHttpsWebClient() throws SecurityServiceException {
// positive case
SecureCxfClientFactory<IDummy> secureCxfClientFactory = new SecureCxfClientFactory<>(SECURE_ENDPOINT, IDummy.class);
Client unsecuredClient = WebClient.client(secureCxfClientFactory.getClient());
assertTrue(unsecuredClient.getBaseURI().toASCIIString().equals(SECURE_ENDPOINT));
// negative cases
boolean subject = true;
secureCxfClientFactory.getWebClientForSubject(getSubject());
assertTrue(subject);
boolean system = true;
secureCxfClientFactory.getWebClient();
assertTrue(system);
boolean unsecured = true;
secureCxfClientFactory.getWebClient();
assertTrue(unsecured);
}
use of org.apache.cxf.jaxrs.client.Client in project ddf by codice.
the class SecureCxfClientFactoryTest method testHttpsClient.
@Test
public void testHttpsClient() throws SecurityServiceException {
// positive case
SecureCxfClientFactory<IDummy> secureCxfClientFactory = new SecureCxfClientFactory<>(SECURE_ENDPOINT, IDummy.class);
Client unsecuredClient = WebClient.client(secureCxfClientFactory.getClient());
assertTrue(unsecuredClient.getBaseURI().toASCIIString().equals(SECURE_ENDPOINT));
// negative cases
boolean subject = true;
secureCxfClientFactory.getClientForSubject(getSubject());
assertTrue(subject);
boolean system = true;
secureCxfClientFactory.getClient();
assertTrue(system);
boolean unsecured = true;
secureCxfClientFactory.getClient();
assertTrue(unsecured);
}
Aggregations