use of javax.ws.rs.ext.Providers in project jersey by eclipse-ee4j.
the class JaxrsProvidersTest method testProviders.
@Test
public void testProviders() throws Exception {
InjectionManager injectionManager = Injections.createInjectionManager();
injectionManager.register(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER));
injectionManager.register(new Binder());
BootstrapBag bootstrapBag = new BootstrapBag();
List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(new RequestScope.RequestScopeConfigurator(), new TestConfigConfigurator(), new ContextResolverFactory.ContextResolversConfigurator(), new MessageBodyFactory.MessageBodyWorkersConfigurator(), new ExceptionMapperFactory.ExceptionMappersConfigurator(), new JaxrsProviders.ProvidersConfigurator());
injectionManager.register(new TestBinder());
TestBinder.initProviders(injectionManager);
bootstrapConfigurators.forEach(configurator -> configurator.init(injectionManager, bootstrapBag));
injectionManager.completeRegistration();
bootstrapConfigurators.forEach(configurator -> configurator.postInit(injectionManager, bootstrapBag));
RequestScope scope = bootstrapBag.getRequestScope();
scope.runInScope((Callable<Object>) () -> {
Providers instance = injectionManager.getInstance(Providers.class);
assertNotNull(instance);
assertSame(JaxrsProviders.class, instance.getClass());
assertNotNull(instance.getExceptionMapper(Throwable.class));
assertNotNull(instance.getMessageBodyReader(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
assertNotNull(instance.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
assertNotNull(instance.getContextResolver(String.class, MediaType.TEXT_PLAIN_TYPE));
return null;
});
}
use of javax.ws.rs.ext.Providers in project quarkus by quarkusio.
the class ResteasyCommonProcessor method setupProviders.
@BuildStep
JaxrsProvidersToRegisterBuildItem setupProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem indexBuildItem, BeanArchiveIndexBuildItem beanArchiveIndexBuildItem, BuildProducer<UnremovableBeanBuildItem> unremovableBeans, List<ResteasyJaxrsProviderBuildItem> contributedProviderBuildItems, List<RestClientBuildItem> restClients, ResteasyConfigBuildItem resteasyConfig, Capabilities capabilities) throws Exception {
Set<String> contributedProviders = new HashSet<>();
for (ResteasyJaxrsProviderBuildItem contributedProviderBuildItem : contributedProviderBuildItems) {
contributedProviders.add(contributedProviderBuildItem.getName());
}
Set<String> annotatedProviders = new HashSet<>();
for (AnnotationInstance i : indexBuildItem.getIndex().getAnnotations(ResteasyDotNames.PROVIDER)) {
if (i.target().kind() == AnnotationTarget.Kind.CLASS) {
annotatedProviders.add(i.target().asClass().name().toString());
}
checkProperConfigAccessInProvider(i);
}
contributedProviders.addAll(annotatedProviders);
Set<String> availableProviders = new HashSet<>(ServiceUtil.classNamesNamedIn(getClass().getClassLoader(), "META-INF/services/" + Providers.class.getName()));
// this one is added manually in RESTEasy's ResteasyDeploymentImpl
availableProviders.add(ServerFormUrlEncodedProvider.class.getName());
MediaTypeMap<String> categorizedReaders = new MediaTypeMap<>();
MediaTypeMap<String> categorizedWriters = new MediaTypeMap<>();
MediaTypeMap<String> categorizedContextResolvers = new MediaTypeMap<>();
Set<String> otherProviders = new HashSet<>();
categorizeProviders(availableProviders, categorizedReaders, categorizedWriters, categorizedContextResolvers, otherProviders);
// add the other providers detected
Set<String> providersToRegister = new HashSet<>(otherProviders);
if (!capabilities.isPresent(Capability.VERTX) && !capabilities.isCapabilityWithPrefixPresent(Capability.RESTEASY_JSON)) {
boolean needJsonSupport = restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.CONSUMES) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.PRODUCES) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_SSE_ELEMENT_TYPE) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_PART_TYPE);
if (needJsonSupport) {
LOGGER.warn("Quarkus detected the need of REST JSON support but you have not provided the necessary JSON " + "extension for this. You can visit https://quarkus.io/guides/rest-json for more " + "information on how to set one.");
}
}
if (!capabilities.isPresent(Capability.RESTEASY_MUTINY)) {
String needsMutinyClasses = mutinySupportNeeded(indexBuildItem);
if (needsMutinyClasses != null) {
LOGGER.warn("Quarkus detected the need for Mutiny reactive programming support, however the quarkus-resteasy-mutiny extension " + "was not present. Reactive REST endpoints in your application that return Uni or Multi " + "will not function as you expect until you add this extension. Endpoints that need Mutiny are: " + needsMutinyClasses);
}
}
// we add a couple of default providers
providersToRegister.add(StringTextStar.class.getName());
providersToRegister.addAll(categorizedWriters.getPossible(MediaType.APPLICATION_JSON_TYPE));
IndexView index = indexBuildItem.getIndex();
IndexView beansIndex = beanArchiveIndexBuildItem.getIndex();
// find the providers declared in our services
boolean useBuiltinProviders = collectDeclaredProviders(restClients, resteasyConfig, providersToRegister, categorizedReaders, categorizedWriters, categorizedContextResolvers, index, beansIndex);
if (useBuiltinProviders) {
providersToRegister = new HashSet<>(contributedProviders);
providersToRegister.addAll(availableProviders);
} else {
providersToRegister.addAll(contributedProviders);
}
if (providersToRegister.contains("org.jboss.resteasy.plugins.providers.jsonb.JsonBindingProvider")) {
// This abstract one is also accessed directly via reflection
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "org.jboss.resteasy.plugins.providers.jsonb.AbstractJsonBindingProvider"));
}
JaxrsProvidersToRegisterBuildItem result = new JaxrsProvidersToRegisterBuildItem(providersToRegister, contributedProviders, annotatedProviders, useBuiltinProviders);
// Providers that are also beans are unremovable
unremovableBeans.produce(new UnremovableBeanBuildItem(b -> result.getProviders().contains(b.getBeanClass().toString())));
return result;
}
use of javax.ws.rs.ext.Providers in project jetrs by jetrs.
the class RestApplicationServlet method service.
private void service(final HttpServletRequestImpl httpServletRequest, final HttpServletResponse httpServletResponse) throws IOException, ServletException {
final ServerContext serverContext = getServerContext();
final ContainerResponseContextImpl containerResponseContext = new ContainerResponseContextImpl(httpServletRequest, httpServletResponse, serverContext.getWriterInterceptors());
final HttpHeadersImpl requestHeaders = new HttpHeadersImpl(httpServletRequest);
final ExecutionContext executionContext = new ExecutionContext(requestHeaders, httpServletResponse, containerResponseContext, serverContext);
// NOTE: This weird construct is done this way to at least somehow make the two objects cohesive
final ContainerRequestContextImpl containerRequestContext;
httpServletRequest.setRequestContext(containerRequestContext = new ContainerRequestContextImpl(httpServletRequest, executionContext, serverContext.getReaderInterceptors()));
final AnnotationInjector annotationInjector = createAnnotationInjector(containerRequestContext, containerResponseContext, httpServletRequest, httpServletResponse, requestHeaders, serverContext);
final Providers providers = serverContext.getProviders(annotationInjector);
ResourceMatch resourceMatch = null;
Stage stage = null;
try {
// (1) Filter Request (Pre-Match)
stage = Stage.FILTER_REQUEST_PRE_MATCH;
executionContext.filterPreMatchContainerRequest(containerRequestContext, annotationInjector);
// (2) Match
stage = Stage.MATCH;
resourceMatch = executionContext.filterAndMatch(containerRequestContext, annotationInjector);
final ResourceManifest resourceManifest = resourceMatch.getManifest();
annotationInjector.setResourceInfo(resourceManifest);
// (3) Filter Request
stage = Stage.FILTER_REQUEST;
executionContext.filterContainerRequest(containerRequestContext, annotationInjector);
// (4a) Service
stage = Stage.SERVICE;
httpServletRequest.setResourceManifest(resourceManifest);
executionContext.service(resourceMatch, containerRequestContext, annotationInjector);
// (5a) Filter Response
stage = Stage.FILTER_RESPONSE;
executionContext.filterContainerResponse(containerRequestContext, annotationInjector);
// (6a) Write Response
stage = Stage.WRITE_RESPONSE;
executionContext.writeResponse(annotationInjector, resourceMatch, containerRequestContext, providers);
} catch (final IOException | RuntimeException | ServletException e) {
if (!(e instanceof AbortFilterChainException)) {
// FIXME: Review [JAX-RS 2.1 3.3.4 2,3]
// (4b) Error
final Response response;
try {
response = executionContext.setErrorResponse(providers, e);
} catch (final RuntimeException e1) {
if (!(e1 instanceof WebApplicationException))
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
e1.addSuppressed(e);
throw e1;
}
if (response == null) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
throw e;
}
} else if (stage == Stage.FILTER_RESPONSE) {
throw new IllegalStateException("ContainerRequestContext.abortWith(Response) cannot be called from response filter chain");
} else {
executionContext.setAbortResponse((AbortFilterChainException) e);
}
try {
// (5b) Filter Response
stage = Stage.FILTER_RESPONSE;
executionContext.filterContainerResponse(containerRequestContext, annotationInjector);
} catch (final IOException | RuntimeException e1) {
e.addSuppressed(e1);
}
try {
// (6b) Write Response
stage = Stage.WRITE_RESPONSE;
executionContext.writeResponse(annotationInjector, resourceMatch, containerRequestContext, providers);
} catch (final IOException | RuntimeException e1) {
if (!(e1 instanceof WebApplicationException))
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
e.addSuppressed(e1);
throw e;
}
} finally {
// (7) Commit Response
executionContext.commitResponse(containerRequestContext);
}
}
Aggregations