Skip to content

Commit

Permalink
JAX-RS @Provider autodiscovery (#1880)
Browse files Browse the repository at this point in the history
* Provider as a bean defining annotation.
* Automatic discovery of JAX-RS providers.
* Removed deprecated provider.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer authored May 27, 2020
1 parent 1061175 commit e4be3dc
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ void initInContext() {
private HelidonContainerImpl init() {
LOGGER.fine(() -> "Initializing CDI container " + id);

addHelidonBeanDefiningAnnotations("javax.ws.rs.Path", "javax.websocket.server.ServerEndpoint");
addHelidonBeanDefiningAnnotations("javax.ws.rs.Path",
"javax.ws.rs.ext.Provider",
"javax.websocket.server.ServerEndpoint");

ResourceLoader resourceLoader = new WeldResourceLoader() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import io.helidon.common.HelidonFeatures;
import io.helidon.common.HelidonFlavor;
Expand All @@ -66,6 +67,7 @@ public class JaxRsCdiExtension implements Extension {

private final Set<Class<? extends Application>> applications = new LinkedHashSet<>();
private final Set<Class<?>> resources = new HashSet<>();
private final Set<Class<?>> providers = new HashSet<>();
private final AtomicBoolean setInStone = new AtomicBoolean(false);

private void collectApplications(@Observes ProcessAnnotatedType<? extends Application> applicationType) {
Expand All @@ -82,6 +84,18 @@ private void collectResourceClasses(@Observes @WithAnnotations(Path.class) Proce
resources.add(resourceClass);
}

private void collectProviderClasses(@Observes @WithAnnotations(Provider.class) ProcessAnnotatedType<?> providerType) {
Class<?> providerClass = providerType.getAnnotatedType().getJavaClass();
if (providerClass.isInterface()) {
// we are only interested in classes
LOGGER.finest(() -> "Discovered @Provider interface " + providerClass
.getName() + ", ignored as we only support classes");
return;
}
LOGGER.finest(() -> "Discovered @Provider class " + providerClass.getName());
providers.add(providerClass);
}

// once application scoped starts, we do not allow modification of applications
void fixApps(@Observes @Priority(PLATFORM_BEFORE) @Initialized(ApplicationScoped.class) Object event) {
this.setInStone.set(true);
Expand All @@ -102,10 +116,16 @@ public List<JaxRsApplication> applicationsToRun() throws IllegalStateException {
throw new IllegalStateException("Applications are not yet fixed. This method is only available in "
+ "@Initialized(ApplicationScoped.class) event, before server is started");
}

// set of resource and provider classes that were discovered
Set<Class<?>> allClasses = new HashSet<>();
allClasses.addAll(resources);
allClasses.addAll(providers);

if (applications.isEmpty() && applicationMetas.isEmpty()) {
// create a synthetic application from all resource classes
if (!resources.isEmpty()) {
addSyntheticApp(resources);
addSyntheticApp(allClasses);
}
}

Expand All @@ -114,7 +134,7 @@ public List<JaxRsApplication> applicationsToRun() throws IllegalStateException {
.stream()
.map(appClass -> JaxRsApplication.builder()
.applicationClass(appClass)
.config(ResourceConfig.forApplicationClass(appClass, resources))
.config(ResourceConfig.forApplicationClass(appClass, allClasses))
.build())
.collect(Collectors.toList()));

Expand Down Expand Up @@ -238,7 +258,7 @@ public void addSyntheticApplication(List<Class<?>> resourceClasses) throws Illeg
// set-up synthetic application from resource classes
private void addSyntheticApp(Collection<Class<?>> resourceClasses) {
// the classes set must be created before the lambda, as the incoming collection may be mutable
Set<Class<?>> classes = new HashSet<>(resourceClasses);
Set<Class<?>> classes = Set.copyOf(resourceClasses);
this.applicationMetas.add(JaxRsApplication.builder()
.synthetic(true)
.applicationClass(Application.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,6 @@
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import io.helidon.common.HelidonFeatures;
import io.helidon.common.context.Contexts;
Expand All @@ -48,7 +47,6 @@
* Only works as part of integration with Security component.
* This class is public to allow unit testing from providers (without invoking an HTTP request)
*/
@Provider
@ConstrainedTo(RuntimeType.CLIENT)
@Priority(Priorities.AUTHENTICATION)
public class ClientSecurityFilter implements ClientRequestFilter {
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit e4be3dc

Please sign in to comment.