Search in sources :

Example 6 with JAXRSServiceFactoryBean

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

the class OpenApiFeature method initialize.

@Override
public void initialize(Server server, Bus bus) {
    final JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    final ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    final Set<String> packages = new HashSet<>();
    if (resourcePackages != null) {
        packages.addAll(resourcePackages);
    }
    Properties swaggerProps = null;
    GenericOpenApiContextBuilder<?> openApiConfiguration;
    final Application application = getApplicationOrDefault(server, factory, sfb, bus);
    if (StringUtils.isEmpty(getConfigLocation())) {
        swaggerProps = getSwaggerProperties(propertiesLocation, bus);
        if (isScan()) {
            packages.addAll(scanResourcePackages(sfb));
        }
        final OpenAPI oas = new OpenAPI().info(getInfo(swaggerProps));
        registerComponents(securityDefinitions).ifPresent(oas::setComponents);
        final SwaggerConfiguration config = new SwaggerConfiguration().openAPI(oas).prettyPrint(getOrFallback(isPrettyPrint(), swaggerProps, PRETTY_PRINT_PROPERTY)).readAllResources(isReadAllResources()).ignoredRoutes(getIgnoredRoutes()).filterClass(getOrFallback(getFilterClass(), swaggerProps, FILTER_CLASS_PROPERTY)).resourceClasses(getResourceClasses()).resourcePackages(getOrFallback(packages, swaggerProps, RESOURCE_PACKAGE_PROPERTY));
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).openApiConfiguration(config);
    } else {
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).configLocation(getConfigLocation());
    }
    try {
        final OpenApiContext context = openApiConfiguration.buildContext(true);
        final Properties userProperties = getUserProperties(context.getOpenApiConfiguration().getUserDefinedOptions());
        registerOpenApiResources(sfb, packages, context.getOpenApiConfiguration());
        registerSwaggerUiResources(sfb, combine(swaggerProps, userProperties), factory, bus);
        if (customizer != null) {
            customizer.setApplicationInfo(factory.getApplicationProvider());
        }
    } catch (OpenApiConfigurationException ex) {
        throw new RuntimeException("Unable to initialize OpenAPI context", ex);
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) Properties(java.util.Properties) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Application(javax.ws.rs.core.Application) OpenAPI(io.swagger.v3.oas.models.OpenAPI) HashSet(java.util.HashSet) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Example 7 with JAXRSServiceFactoryBean

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

the class AbstractSwaggerFeature method calculateDefaultResourcePackage.

private void calculateDefaultResourcePackage(Server server) {
    if (!StringUtils.isEmpty(getResourcePackage())) {
        return;
    }
    JAXRSServiceFactoryBean serviceFactoryBean = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    List<ClassResourceInfo> resourceInfos = serviceFactoryBean.getClassResourceInfo();
    if (resourceInfos.size() == 1) {
        setResourcePackage(resourceInfos.get(0).getServiceClass().getPackage().getName());
    } else {
        List<Class<?>> serviceClasses = new ArrayList<Class<?>>(resourceInfos.size());
        for (ClassResourceInfo cri : resourceInfos) {
            serviceClasses.add(cri.getServiceClass());
        }
        String sharedPackage = PackageUtils.getSharedPackageName(serviceClasses);
        if (!StringUtils.isEmpty(sharedPackage)) {
            setResourcePackage(sharedPackage);
        }
    }
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ArrayList(java.util.ArrayList)

Example 8 with JAXRSServiceFactoryBean

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

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses.

@Test
public void testSelectBetweenMultipleResourceClasses() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class, org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    ClassResourceInfo bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/bookstore", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.BookStore.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/bookstore/", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.BookStore.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/bookstore/bar", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.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 9 with JAXRSServiceFactoryBean

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

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses2.

@Test
public void testSelectBetweenMultipleResourceClasses2() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    ClassResourceInfo bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/1", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/1/", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/1/foo", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class);
    bStore = firstResource(JAXRSUtils.selectResourceClass(resources, "/1/foo/bar", null));
    assertEquals(bStore.getResourceClass(), org.apache.cxf.jaxrs.resources.TestResourceTemplate2.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 10 with JAXRSServiceFactoryBean

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

the class JAXRSUtilsTest method testFindTargetResourceClassWithTemplates.

@Test
public void testFindTargetResourceClassWithTemplates() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreTemplates.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    // If acceptContentTypes does not specify a specific Mime type, the
    // method is declared with a most specific ProduceMime type is selected.
    MetadataMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage2(), "/1/2/", "GET", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBooks", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("2 {id} values should've been picked up", 2, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("First {id} is 1", "1", values.getFirst("id"));
    assertEquals("Second id is 2", "2", values.get("id").get(1));
    values = new MetadataMap<String, String>();
    ori = findTargetResourceClass(resources, createMessage2(), "/2", "POST", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBookStoreInfo", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("Only single {id} should've been picked up", 1, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "2", values.getFirst("id"));
    values = new MetadataMap<String, String>();
    ori = findTargetResourceClass(resources, createMessage2(), "/3/4", "PUT", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    assertEquals("Only the first {id} should've been picked up", 3, values.size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("id").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("bookId").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "3", values.getFirst("id"));
    assertEquals("Only the first {id} should've been picked up", "4", values.getFirst("bookId"));
}
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)

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