use of javax.ws.rs.ApplicationPath 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);
}
});
}
use of javax.ws.rs.ApplicationPath in project swagger-core by swagger-api.
the class Reader method resolveApplicationPath.
protected String resolveApplicationPath() {
if (application != null) {
Class<?> applicationToScan = this.application.getClass();
ApplicationPath applicationPath;
// search up in the hierarchy until we find one with the annotation, this is needed because for example Weld proxies will not have the annotation and the right class will be the superClass
while ((applicationPath = applicationToScan.getAnnotation(ApplicationPath.class)) == null && !applicationToScan.getSuperclass().equals(Application.class)) {
applicationToScan = applicationToScan.getSuperclass();
}
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
}
}
// look for inner application, e.g. ResourceConfig
try {
Application innerApp = application;
Method m = application.getClass().getMethod("getApplication");
while (m != null) {
Application retrievedApp = (Application) m.invoke(innerApp);
if (retrievedApp == null) {
break;
}
if (retrievedApp.getClass().equals(innerApp.getClass())) {
break;
}
innerApp = retrievedApp;
applicationPath = innerApp.getClass().getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
}
}
m = innerApp.getClass().getMethod("getApplication");
}
} catch (NoSuchMethodException e) {
// no inner application found
} catch (Exception e) {
// no inner application found
}
}
return "";
}
use of javax.ws.rs.ApplicationPath in project swagger-core by swagger-api.
the class Reader method read.
/**
* Scans a set of classes for both ReaderListeners and OpenAPI annotations. All found listeners will
* be instantiated before any of the classes are scanned for OpenAPI annotations - so they can be invoked
* accordingly.
*
* @param classes a set of classes to scan
* @return the generated OpenAPI definition
*/
public OpenAPI read(Set<Class<?>> classes) {
Set<Class<?>> sortedClasses = new TreeSet<>((class1, class2) -> {
if (class1.equals(class2)) {
return 0;
} else if (class1.isAssignableFrom(class2)) {
return -1;
} else if (class2.isAssignableFrom(class1)) {
return 1;
}
return class1.getName().compareTo(class2.getName());
});
sortedClasses.addAll(classes);
Map<Class<?>, ReaderListener> listeners = new HashMap<>();
String appPath = "";
for (Class<?> cls : sortedClasses) {
if (ReaderListener.class.isAssignableFrom(cls) && !listeners.containsKey(cls)) {
try {
listeners.put(cls, (ReaderListener) cls.newInstance());
} catch (Exception e) {
LOGGER.error("Failed to create ReaderListener", e);
}
}
if (config != null && Boolean.TRUE.equals(config.isAlwaysResolveAppPath())) {
if (Application.class.isAssignableFrom(cls)) {
ApplicationPath appPathAnnotation = ReflectionUtils.getAnnotation(cls, ApplicationPath.class);
if (appPathAnnotation != null) {
appPath = appPathAnnotation.value();
}
}
}
}
for (ReaderListener listener : listeners.values()) {
try {
listener.beforeScan(this, openAPI);
} catch (Exception e) {
LOGGER.error("Unexpected error invoking beforeScan listener [" + listener.getClass().getName() + "]", e);
}
}
String appPathRuntime = resolveApplicationPath();
if (StringUtils.isNotBlank(appPathRuntime)) {
appPath = appPathRuntime;
}
for (Class<?> cls : sortedClasses) {
read(cls, appPath, null, false, null, null, new LinkedHashSet<String>(), new ArrayList<Parameter>(), new HashSet<Class<?>>());
}
for (ReaderListener listener : listeners.values()) {
try {
listener.afterScan(this, openAPI);
} catch (Exception e) {
LOGGER.error("Unexpected error invoking afterScan listener [" + listener.getClass().getName() + "]", e);
}
}
return openAPI;
}
Aggregations