use of javax.ws.rs.ext.Provider in project meecrowave by apache.
the class ConfigurableBus method initProviders.
public void initProviders(final Configuration builder, final ClassLoader loader) {
final List<Object> providers = ofNullable(builder.getJaxrsDefaultProviders()).map(s -> Stream.of(s.split(" *, *")).map(String::trim).filter(p -> !p.isEmpty()).map(name -> {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException(name + " can't be created");
}
}).collect(Collectors.<Object>toList())).orElseGet(() -> {
// ensure both providers share the same memory reuse logic
final JsonProvider provider = JsonProvider.provider();
final JsonReaderFactory readerFactory = provider.createReaderFactory(new HashMap<String, Object>() {
{
put(JsonParserFactoryImpl.SUPPORTS_COMMENTS, builder.isJsonpSupportsComment());
Optional.of(builder.getJsonpMaxStringLen()).filter(v -> v > 0).ifPresent(s -> put(JsonParserFactoryImpl.MAX_STRING_LENGTH, s));
Optional.of(builder.getJsonpMaxReadBufferLen()).filter(v -> v > 0).ifPresent(s -> put(JsonParserFactoryImpl.BUFFER_LENGTH, s));
ofNullable(builder.getJsonpBufferStrategy()).ifPresent(s -> put(AbstractJsonFactory.BUFFER_STRATEGY, s));
}
});
final JsonWriterFactory writerFactory = provider.createWriterFactory(new HashMap<String, Object>() {
{
put(JsonGenerator.PRETTY_PRINTING, builder.isJsonpPrettify());
Optional.of(builder.getJsonpMaxWriteBufferLen()).filter(v -> v > 0).ifPresent(v -> put(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH, v));
ofNullable(builder.getJsonpBufferStrategy()).ifPresent(s -> put(AbstractJsonFactory.BUFFER_STRATEGY, s));
}
});
return Stream.<Object>of(new ConfiguredJsonbJaxrsProvider(builder.getJsonbEncoding(), builder.isJsonbNulls(), builder.isJsonbIJson(), builder.isJsonbPrettify(), builder.getJsonbBinaryStrategy(), builder.getJsonbNamingStrategy(), builder.getJsonbOrderStrategy(), new DelegateJsonProvider(provider, readerFactory, writerFactory))).collect(toList());
});
if (builder.isJaxrsAutoActivateBeanValidation()) {
try {
// we don't need the jaxrsbeanvalidationfeature since bean validation cdi extension handles it normally
loader.loadClass("javax.validation.Validation");
final Object instance = loader.loadClass("org.apache.cxf.jaxrs.validation.ValidationExceptionMapper").getConstructor().newInstance();
// validate bval can be used, check NoClassDefFoundError javax.validation.ValidationException
instance.getClass().getGenericInterfaces();
providers.add(instance);
} catch (final Exception | NoClassDefFoundError e) {
// no-op
}
}
// client
if (getProperty("org.apache.cxf.jaxrs.bus.providers") == null) {
setProperty("skip.default.json.provider.registration", "true");
setProperty("org.apache.cxf.jaxrs.bus.providers", providers);
}
}
use of javax.ws.rs.ext.Provider in project meecrowave by apache.
the class ConfigurableBus method initProviders.
public void initProviders(final Meecrowave.Builder builder, final ClassLoader loader) {
final List<Object> providers = ofNullable(builder.getJaxrsDefaultProviders()).map(s -> Stream.of(s.split(" *, *")).map(String::trim).filter(p -> !p.isEmpty()).map(name -> {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException(name + " can't be created");
}
}).collect(Collectors.<Object>toList())).orElseGet(() -> {
// ensure both providers share the same memory reuse logic
final JsonProvider provider = JsonProvider.provider();
final JsonReaderFactory readerFactory = provider.createReaderFactory(new HashMap<String, Object>() {
{
put(JsonParserFactoryImpl.SUPPORTS_COMMENTS, builder.isJsonpSupportsComment());
of(builder.getJsonpMaxStringLen()).filter(v -> v > 0).ifPresent(s -> put(JsonParserFactoryImpl.MAX_STRING_LENGTH, s));
of(builder.getJsonpMaxReadBufferLen()).filter(v -> v > 0).ifPresent(s -> put(JsonParserFactoryImpl.BUFFER_LENGTH, s));
ofNullable(builder.getJsonpBufferStrategy()).ifPresent(s -> put(AbstractJsonFactory.BUFFER_STRATEGY, s));
}
});
final JsonWriterFactory writerFactory = provider.createWriterFactory(new HashMap<String, Object>() {
{
put(JsonGenerator.PRETTY_PRINTING, builder.isJsonpPrettify());
of(builder.getJsonpMaxWriteBufferLen()).filter(v -> v > 0).ifPresent(v -> put(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH, v));
ofNullable(builder.getJsonpBufferStrategy()).ifPresent(s -> put(AbstractJsonFactory.BUFFER_STRATEGY, s));
}
});
return Stream.<Object>of(new ConfiguredJsonbJaxrsProvider(builder.getJsonbEncoding(), builder.isJsonbNulls(), builder.isJsonbIJson(), builder.isJsonbPrettify(), builder.getJsonbBinaryStrategy(), builder.getJsonbNamingStrategy(), builder.getJsonbOrderStrategy(), new DelegateJsonProvider(provider, readerFactory, writerFactory)), new ConfiguredJsrProvider(readerFactory, writerFactory)).collect(toList());
});
if (builder.isJaxrsAutoActivateBeanValidation()) {
try {
// we don't need the jaxrsbeanvalidationfeature since bean validation cdi extension handles it normally
loader.loadClass("javax.validation.Validation");
final Object instance = loader.loadClass("org.apache.cxf.jaxrs.validation.ValidationExceptionMapper").getConstructor().newInstance();
// validate bval can be used, check NoClassDefFoundError javax.validation.ValidationException
instance.getClass().getGenericInterfaces();
providers.add(instance);
} catch (final Exception | NoClassDefFoundError e) {
// no-op
}
}
// client
if (getProperty("org.apache.cxf.jaxrs.bus.providers") == null) {
setProperty("skip.default.json.provider.registration", "true");
setProperty("org.apache.cxf.jaxrs.bus.providers", providers);
}
}
use of javax.ws.rs.ext.Provider in project spring-cloud-netflix by spring-cloud.
the class EurekaServerAutoConfiguration method jerseyApplication.
/**
* Construct a Jersey {@link javax.ws.rs.core.Application} with all the resources
* required by the Eureka server.
*/
@Bean
public javax.ws.rs.core.Application jerseyApplication(Environment environment, ResourceLoader resourceLoader) {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment);
// Filter to include only classes that have a particular annotation.
//
provider.addIncludeFilter(new AnnotationTypeFilter(Path.class));
provider.addIncludeFilter(new AnnotationTypeFilter(Provider.class));
// Find classes in Eureka packages (or subpackages)
//
Set<Class<?>> classes = new HashSet<>();
for (String basePackage : EUREKA_PACKAGES) {
Set<BeanDefinition> beans = provider.findCandidateComponents(basePackage);
for (BeanDefinition bd : beans) {
Class<?> cls = ClassUtils.resolveClassName(bd.getBeanClassName(), resourceLoader.getClassLoader());
classes.add(cls);
}
}
// Construct the Jersey ResourceConfig
//
Map<String, Object> propsAndFeatures = new HashMap<>();
propsAndFeatures.put(// Skip static content used by the webapp
ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX, EurekaConstants.DEFAULT_PREFIX + "/(fonts|images|css|js)/.*");
DefaultResourceConfig rc = new DefaultResourceConfig(classes);
rc.setPropertiesAndFeatures(propsAndFeatures);
return rc;
}
use of javax.ws.rs.ext.Provider in project cxf by apache.
the class JaxrsServletContainerInitializer method onStartup.
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext ctx) throws ServletException {
Application app = null;
String servletName = null;
String servletMapping = null;
final Class<?> appClass = findCandidate(classes);
if (appClass != null) {
// Custom servlets using non-standard mechanisms to create Application will not be detected
if (isApplicationServletAvailable(ctx, appClass)) {
return;
}
try {
app = (Application) appClass.newInstance();
} catch (Throwable t) {
throw new ServletException(t);
}
// Servlet name is the application class name
servletName = appClass.getName();
ApplicationPath appPath = ResourceUtils.locateApplicationPath(appClass);
// a servlet registration with an application implementation class name
if (appPath != null) {
servletMapping = appPath.value() + "/*";
} else {
servletMapping = getServletMapping(ctx, servletName);
}
}
// resource and provider classes
if (app == null || (app.getClasses().isEmpty() && app.getSingletons().isEmpty())) {
// Custom servlets using non-standard mechanisms to create Application will not be detected
if (isCxfServletAvailable(ctx)) {
return;
}
final Map<Class<? extends Annotation>, Collection<Class<?>>> providersAndResources = groupByAnnotations(classes);
if (!providersAndResources.get(Path.class).isEmpty() || !providersAndResources.get(Provider.class).isEmpty()) {
if (app == null) {
// Servlet name is a JAX-RS Application class name
servletName = JAXRS_APPLICATION_SERVLET_NAME;
// Servlet mapping is obtained from a servlet registration
// with a JAX-RS Application class name
servletMapping = getServletMapping(ctx, servletName);
}
final Map<String, Object> appProperties = app != null ? app.getProperties() : Collections.emptyMap();
app = new Application() {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.addAll(providersAndResources.get(Path.class));
set.addAll(providersAndResources.get(Provider.class));
return set;
}
@Override
public Map<String, Object> getProperties() {
return appProperties;
}
};
}
}
if (app == null) {
return;
}
CXFNonSpringJaxrsServlet cxfServlet = new CXFNonSpringJaxrsServlet(app);
final Dynamic servlet = ctx.addServlet(servletName, cxfServlet);
servlet.addMapping(servletMapping);
}
Aggregations