use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project cxf by apache.
the class JAXRSUtilsTest method testFindTargetResourceClass.
@Test
public void testFindTargetResourceClass() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.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.
OperationResourceInfo ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123/", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json,application/xml;q=0.9"));
assertNotNull(ori);
assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json"));
assertNotNull(ori);
assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("getBook", ori.getMethodToInvoke().getName());
// test
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("getBooks", ori.getMethodToInvoke().getName());
// test find POST
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("addBook", ori.getMethodToInvoke().getName());
// test find PUT
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertEquals("updateBook", ori.getMethodToInvoke().getName());
// test find DELETE
ori = findTargetResourceClass(resources, createMessage2(), "/bookstore/1/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
assertNotNull(ori);
assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project fabric8 by jboss-fuse.
the class ManagedApi method getRESTResourceTypes.
private Set<Class<?>> getRESTResourceTypes() {
JAXRSServiceFactoryBean serviceFactory = (JAXRSServiceFactoryBean) endpoint.get(JAXRSServiceFactoryBean.class.getName());
List<ClassResourceInfo> list = serviceFactory.getClassResourceInfo();
return ResourceUtils.getAllRequestResponseTypes(list, false).getAllTypes().keySet();
}
use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project fabric8 by fabric8io.
the class ManagedApi method getRESTResourceTypes.
private Set<Class<?>> getRESTResourceTypes() {
JAXRSServiceFactoryBean serviceFactory = (JAXRSServiceFactoryBean) endpoint.get(JAXRSServiceFactoryBean.class.getName());
List<ClassResourceInfo> list = serviceFactory.getClassResourceInfo();
return ResourceUtils.getAllRequestResponseTypes(list, false).getAllTypes().keySet();
}
use of org.apache.cxf.jaxrs.JAXRSServiceFactoryBean in project component-runtime by Talend.
the class WebSocketBroadcastSetup method contextInitialized.
@Override
public void contextInitialized(final ServletContextEvent sce) {
final ServerContainer container = ServerContainer.class.cast(sce.getServletContext().getAttribute(ServerContainer.class.getName()));
final JAXRSServiceFactoryBean factory = JAXRSServiceFactoryBean.class.cast(bus.getExtension(ServerRegistry.class).getServers().iterator().next().getEndpoint().get(JAXRSServiceFactoryBean.class.getName()));
final String appBase = StreamSupport.stream(Spliterators.spliteratorUnknownSize(applications.iterator(), Spliterator.IMMUTABLE), false).filter(a -> a.getClass().isAnnotationPresent(ApplicationPath.class)).map(a -> a.getClass().getAnnotation(ApplicationPath.class)).map(ApplicationPath::value).findFirst().map(s -> !s.startsWith("/") ? "/" + s : s).orElse("/api/v1");
final String version = appBase.replaceFirst("/api", "");
final DestinationRegistry registry;
try {
final HTTPTransportFactory transportFactory = HTTPTransportFactory.class.cast(bus.getExtension(DestinationFactoryManager.class).getDestinationFactory("http://cxf.apache.org/transports/http" + "/configuration"));
registry = transportFactory.getRegistry();
} catch (final BusException e) {
throw new IllegalStateException(e);
}
final ServletContext servletContext = sce.getServletContext();
final WebSocketRegistry webSocketRegistry = new WebSocketRegistry(registry);
final ServletController controller = new ServletController(webSocketRegistry, new ServletConfig() {
@Override
public String getServletName() {
return "Talend Component Kit Websocket Transport";
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
@Override
public String getInitParameter(final String s) {
return null;
}
@Override
public Enumeration<String> getInitParameterNames() {
return emptyEnumeration();
}
}, new ServiceListGeneratorServlet(registry, bus));
webSocketRegistry.controller = controller;
Stream.concat(factory.getClassResourceInfo().stream().flatMap(cri -> cri.getMethodDispatcher().getOperationResourceInfos().stream()).map(ori -> {
final String uri = ori.getClassResourceInfo().getURITemplate().getValue() + ori.getURITemplate().getValue();
return ServerEndpointConfig.Builder.create(Endpoint.class, "/websocket" + version + "/" + String.valueOf(ori.getHttpMethod()).toLowerCase(ENGLISH) + uri).configurator(new ServerEndpointConfig.Configurator() {
@Override
public <T> T getEndpointInstance(final Class<T> clazz) throws InstantiationException {
final Map<String, List<String>> headers = new HashMap<>();
if (!ori.getProduceTypes().isEmpty()) {
headers.put(HttpHeaders.CONTENT_TYPE, singletonList(ori.getProduceTypes().iterator().next().toString()));
}
if (!ori.getConsumeTypes().isEmpty()) {
headers.put(HttpHeaders.ACCEPT, singletonList(ori.getConsumeTypes().iterator().next().toString()));
}
return (T) new JAXRSEndpoint(appBase, controller, servletContext, ori.getHttpMethod(), uri, headers);
}
}).build();
}), Stream.of(ServerEndpointConfig.Builder.create(Endpoint.class, "/websocket" + version + "/bus").configurator(new ServerEndpointConfig.Configurator() {
@Override
public <T> T getEndpointInstance(final Class<T> clazz) throws InstantiationException {
return (T) new JAXRSEndpoint(appBase, controller, servletContext, "GET", "/", emptyMap());
}
}).build())).sorted(Comparator.comparing(ServerEndpointConfig::getPath)).peek(e -> log.info("Deploying WebSocket(path={})", e.getPath())).forEach(config -> {
try {
container.addEndpoint(config);
} catch (final DeploymentException e) {
throw new IllegalStateException(e);
}
});
}
Aggregations