Skip to content

Commit e4be3dc

Browse files
authored
JAX-RS @Provider autodiscovery (helidon-io#1880)
* Provider as a bean defining annotation. * Automatic discovery of JAX-RS providers. * Removed deprecated provider. Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
1 parent 1061175 commit e4be3dc

10 files changed

Lines changed: 125 additions & 323 deletions

File tree

microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ void initInContext() {
141141
private HelidonContainerImpl init() {
142142
LOGGER.fine(() -> "Initializing CDI container " + id);
143143

144-
addHelidonBeanDefiningAnnotations("javax.ws.rs.Path", "javax.websocket.server.ServerEndpoint");
144+
addHelidonBeanDefiningAnnotations("javax.ws.rs.Path",
145+
"javax.ws.rs.ext.Provider",
146+
"javax.websocket.server.ServerEndpoint");
145147

146148
ResourceLoader resourceLoader = new WeldResourceLoader() {
147149
@Override

microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsCdiExtension.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import javax.ws.rs.core.Application;
4242
import javax.ws.rs.core.Response;
4343
import javax.ws.rs.ext.ExceptionMapper;
44+
import javax.ws.rs.ext.Provider;
4445

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

6768
private final Set<Class<? extends Application>> applications = new LinkedHashSet<>();
6869
private final Set<Class<?>> resources = new HashSet<>();
70+
private final Set<Class<?>> providers = new HashSet<>();
6971
private final AtomicBoolean setInStone = new AtomicBoolean(false);
7072

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

87+
private void collectProviderClasses(@Observes @WithAnnotations(Provider.class) ProcessAnnotatedType<?> providerType) {
88+
Class<?> providerClass = providerType.getAnnotatedType().getJavaClass();
89+
if (providerClass.isInterface()) {
90+
// we are only interested in classes
91+
LOGGER.finest(() -> "Discovered @Provider interface " + providerClass
92+
.getName() + ", ignored as we only support classes");
93+
return;
94+
}
95+
LOGGER.finest(() -> "Discovered @Provider class " + providerClass.getName());
96+
providers.add(providerClass);
97+
}
98+
8599
// once application scoped starts, we do not allow modification of applications
86100
void fixApps(@Observes @Priority(PLATFORM_BEFORE) @Initialized(ApplicationScoped.class) Object event) {
87101
this.setInStone.set(true);
@@ -102,10 +116,16 @@ public List<JaxRsApplication> applicationsToRun() throws IllegalStateException {
102116
throw new IllegalStateException("Applications are not yet fixed. This method is only available in "
103117
+ "@Initialized(ApplicationScoped.class) event, before server is started");
104118
}
119+
120+
// set of resource and provider classes that were discovered
121+
Set<Class<?>> allClasses = new HashSet<>();
122+
allClasses.addAll(resources);
123+
allClasses.addAll(providers);
124+
105125
if (applications.isEmpty() && applicationMetas.isEmpty()) {
106126
// create a synthetic application from all resource classes
107127
if (!resources.isEmpty()) {
108-
addSyntheticApp(resources);
128+
addSyntheticApp(allClasses);
109129
}
110130
}
111131

@@ -114,7 +134,7 @@ public List<JaxRsApplication> applicationsToRun() throws IllegalStateException {
114134
.stream()
115135
.map(appClass -> JaxRsApplication.builder()
116136
.applicationClass(appClass)
117-
.config(ResourceConfig.forApplicationClass(appClass, resources))
137+
.config(ResourceConfig.forApplicationClass(appClass, allClasses))
118138
.build())
119139
.collect(Collectors.toList()));
120140

@@ -238,7 +258,7 @@ public void addSyntheticApplication(List<Class<?>> resourceClasses) throws Illeg
238258
// set-up synthetic application from resource classes
239259
private void addSyntheticApp(Collection<Class<?>> resourceClasses) {
240260
// the classes set must be created before the lambda, as the incoming collection may be mutable
241-
Set<Class<?>> classes = new HashSet<>(resourceClasses);
261+
Set<Class<?>> classes = Set.copyOf(resourceClasses);
242262
this.applicationMetas.add(JaxRsApplication.builder()
243263
.synthetic(true)
244264
.applicationClass(Application.class)

security/integration/jersey-client/src/main/java/io/helidon/security/integration/jersey/client/ClientSecurityFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929
import javax.ws.rs.client.ClientRequestContext;
3030
import javax.ws.rs.client.ClientRequestFilter;
3131
import javax.ws.rs.core.MultivaluedMap;
32-
import javax.ws.rs.ext.Provider;
3332

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

security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/ClientSecurityFeature.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/ClientSecurityFilter.java

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)