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