Search in sources :

Example 1 with JAXRSClientFactoryBean

use of org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean 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();
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.camel.Message) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) Method(java.lang.reflect.Method) Response(javax.ws.rs.core.Response) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) WebClient(org.apache.cxf.jaxrs.client.WebClient) Client(org.apache.cxf.jaxrs.client.Client) CookieHandler(org.apache.camel.http.common.cookie.CookieHandler)

Example 2 with JAXRSClientFactoryBean

use of org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean 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);
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.camel.Message) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) Method(java.lang.reflect.Method) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) InvocationCallback(javax.ws.rs.client.InvocationCallback) WebClient(org.apache.cxf.jaxrs.client.WebClient) Client(org.apache.cxf.jaxrs.client.Client) CookieHandler(org.apache.camel.http.common.cookie.CookieHandler)

Example 3 with JAXRSClientFactoryBean

use of org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean in project camel by apache.

the class CxfRsProducerClientFactoryBeanTest method testProducerInOutInterceptors.

@Test
public void testProducerInOutInterceptors() throws Exception {
    CxfRsEndpoint e = context.getEndpoint("cxfrs://bean://rsClientHttpInterceptors", CxfRsEndpoint.class);
    CxfRsProducer p = new CxfRsProducer(e);
    CxfRsProducer.ClientFactoryBeanCache cache = p.getClientFactoryBeanCache();
    JAXRSClientFactoryBean bean = cache.get("http://localhost:8080/CxfRsProducerClientFactoryBeanInterceptors/");
    List<Interceptor<?>> ins = bean.getInInterceptors();
    assertEquals(1, ins.size());
    assertTrue(ins.get(0) instanceof LoggingInInterceptor);
    List<Interceptor<?>> outs = bean.getOutInterceptors();
    assertEquals(1, outs.size());
    assertTrue(outs.get(0) instanceof LoggingOutInterceptor);
}
Also used : JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) LoggingOutInterceptor(org.apache.cxf.interceptor.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.interceptor.LoggingInInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) LoggingOutInterceptor(org.apache.cxf.interceptor.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.interceptor.LoggingInInterceptor) Test(org.junit.Test)

Example 4 with JAXRSClientFactoryBean

use of org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean in project camel by apache.

the class CxfRsEndpointTest method testCxfRsEndpointSetProvider.

@Test
public void testCxfRsEndpointSetProvider() throws Exception {
    String endpointUri = "cxfrs://http://localhost:" + CTX + "" + "?resourceClass=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
    CxfRsComponent component = new CxfRsComponent(context);
    CxfRsEndpoint endpoint = (CxfRsEndpoint) component.createEndpoint(endpointUri);
    JSONProvider<?> jsonProvider = new JSONProvider<Object>();
    jsonProvider.setDropRootElement(true);
    jsonProvider.setSupportUnwrapped(true);
    endpoint.setProvider(jsonProvider);
    JAXRSServerFactoryBean sfb = endpoint.createJAXRSServerFactoryBean();
    assertEquals("Get a wrong proider size", 1, sfb.getProviders().size());
    JAXRSClientFactoryBean cfb = endpoint.createJAXRSClientFactoryBean();
    assertEquals("Get a wrong proider size", 1, cfb.getProviders().size());
}
Also used : JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Test(org.junit.Test)

Example 5 with JAXRSClientFactoryBean

use of org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean in project jersey by jersey.

the class Jersey2776ITCase method testThatMultipartServerSupportsBoundaryQuotesEvenWithInterferingRuntimeDelegate.

@Test
public void testThatMultipartServerSupportsBoundaryQuotesEvenWithInterferingRuntimeDelegate() {
    final JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(getBaseUri().toString());
    bean.setServiceClass(ApacheCxfMultipartClient.class);
    final ApacheCxfMultipartClient cxfClient = bean.create(ApacheCxfMultipartClient.class);
    final String originalContent = "abc";
    final byte[] content = originalContent.getBytes(StandardCharsets.US_ASCII);
    final Attachment fileAttachment = new AttachmentBuilder().object(content).contentDisposition(new ContentDisposition("form-data; filename=\"abc-file\"; name=\"file_path\"")).build();
    final String fileContentReturnedFromServer = cxfClient.uploadDocument(new MultipartBody(fileAttachment));
    assertThat(fileContentReturnedFromServer, equalTo(originalContent));
}
Also used : JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) AttachmentBuilder(org.apache.cxf.jaxrs.ext.multipart.AttachmentBuilder) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)9 Message (org.apache.camel.Message)4 CookieHandler (org.apache.camel.http.common.cookie.CookieHandler)4 Bus (org.apache.cxf.Bus)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)4 Test (org.junit.Test)4 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 GenericType (javax.ws.rs.core.GenericType)2 Response (javax.ws.rs.core.Response)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 JAXRSServiceFactoryBean (org.apache.cxf.jaxrs.JAXRSServiceFactoryBean)2 Client (org.apache.cxf.jaxrs.client.Client)2 InvocationCallback (javax.ws.rs.client.InvocationCallback)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 CxfRsEndpoint (org.apache.camel.component.cxf.jaxrs.CxfRsEndpoint)1 Interceptor (org.apache.cxf.interceptor.Interceptor)1 LoggingInInterceptor (org.apache.cxf.interceptor.LoggingInInterceptor)1 LoggingOutInterceptor (org.apache.cxf.interceptor.LoggingOutInterceptor)1