Search in sources :

Example 1 with JAXRSServiceFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean 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 JAXRSServiceFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean 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 JAXRSServiceFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project cxf by apache.

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses3.

@Test
public void testSelectBetweenMultipleResourceClasses3() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate4.class, org.apache.cxf.jaxrs.resources.TestResourceTemplate3.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    ClassResourceInfo bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate3.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/test", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate4.class);
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Test(org.junit.Test)

Example 4 with JAXRSServiceFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project cxf by apache.

the class JAXRSUtilsTest method testFindTargetResourceClassWithSubResource.

@Test
public void testFindTargetResourceClassWithSubResource() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/books/sub/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/books/123/true/chapter/1", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getNewBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("addBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 5 with JAXRSServiceFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project cxf by apache.

the class Swagger2Feature method addSwaggerResource.

@Override
protected void addSwaggerResource(Server server, Bus bus) {
    JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    ApplicationInfo appInfo = null;
    if (!isScan()) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            Set<Class<?>> serviceClasses = new HashSet<>();
            for (ClassResourceInfo cri : sfb.getClassResourceInfo()) {
                serviceClasses.add(cri.getServiceClass());
            }
            appInfo = new ApplicationInfo(new DefaultApplication(serviceClasses), bus);
            server.getEndpoint().put(Application.class.getName(), appInfo);
        }
    }
    List<Object> swaggerResources = new LinkedList<>();
    if (customizer == null) {
        customizer = new Swagger2Customizer();
    }
    ApiListingResource apiListingResource = new Swagger2ApiListingResource(customizer);
    swaggerResources.add(apiListingResource);
    List<Object> providers = new ArrayList<>();
    if (isRunAsFilter()) {
        providers.add(new SwaggerContainerRequestFilter(appInfo == null ? null : appInfo.getProvider(), customizer));
    }
    final Properties swaggerProps = getSwaggerProperties(bus);
    final Registration swaggerUiRegistration = getSwaggerUi(bus, swaggerProps, isRunAsFilter());
    if (!isRunAsFilter()) {
        swaggerResources.addAll(swaggerUiRegistration.getResources());
    }
    providers.addAll(swaggerUiRegistration.getProviders());
    sfb.setResourceClassesFromBeans(swaggerResources);
    List<ClassResourceInfo> cris = sfb.getClassResourceInfo();
    if (!isRunAsFilter()) {
        for (ClassResourceInfo cri : cris) {
            if (ApiListingResource.class.isAssignableFrom(cri.getResourceClass())) {
                InjectionUtils.injectContextProxies(cri, apiListingResource);
            }
        }
    }
    customizer.setClassResourceInfos(cris);
    customizer.setDynamicBasePath(dynamicBasePath);
    BeanConfig beanConfig = appInfo == null ? new BeanConfig() : new ApplicationBeanConfig(appInfo.getProvider());
    initBeanConfig(beanConfig, swaggerProps);
    Swagger swagger = beanConfig.getSwagger();
    if (swagger != null && securityDefinitions != null) {
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    customizer.setBeanConfig(beanConfig);
    providers.add(new ReaderConfigFilter());
    if (beanConfig.isUsePathBasedConfig()) {
        providers.add(new ServletConfigProvider());
    }
    factory.setUserProviders(providers);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) ArrayList(java.util.ArrayList) Properties(java.util.Properties) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Swagger(io.swagger.models.Swagger) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeanConfig(io.swagger.jaxrs.config.BeanConfig) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) LinkedList(java.util.LinkedList) Application(javax.ws.rs.core.Application)

Aggregations

JAXRSServiceFactoryBean (org.apache.cxf.jaxrs.JAXRSServiceFactoryBean)14 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)10 JAXRSServiceImpl (org.apache.cxf.jaxrs.JAXRSServiceImpl)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 Application (javax.ws.rs.core.Application)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3 Method (java.lang.reflect.Method)2 HashSet (java.util.HashSet)2 Message (org.apache.camel.Message)2 CookieHandler (org.apache.camel.http.common.cookie.CookieHandler)2 Bus (org.apache.cxf.Bus)2 Client (org.apache.cxf.jaxrs.client.Client)2 JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 BeanConfig (io.swagger.jaxrs.config.BeanConfig)1 ApiListingResource (io.swagger.jaxrs.listing.ApiListingResource)1 Swagger (io.swagger.models.Swagger)1 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)1