Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 46b3638

Browse files
Marek PotociarPetr Bouda
authored andcommitted
Removed dependency injection from request-bound value suppliers.
Value suppliers that derive their supplied value from request had to be injected before. This requirement has been removed for these value suppliers and the need to access injectable request-scoped ContainerRequest instance has been moved to a single place in code in the AbstractRequestDerivedValueSupplier. Change-Id: I535ee41e8f69a6c3241a7c15a9d943223b549925 Signed-off-by: Marek Potociar <[email protected]>
1 parent b3ab4e4 commit 46b3638

File tree

17 files changed

+439
-344
lines changed

17 files changed

+439
-344
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/inject/Injections.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
import javax.ws.rs.WebApplicationException;
4343

44+
import javax.inject.Provider;
45+
4446
import org.glassfish.hk2.api.DynamicConfiguration;
4547
import org.glassfish.hk2.api.DynamicConfigurationService;
4648
import org.glassfish.hk2.api.Factory;
@@ -187,6 +189,18 @@ public static <T> T getOrCreate(final ServiceLocator serviceLocator, final Class
187189
}
188190
}
189191

192+
/**
193+
* Get a provider for a contract.
194+
*
195+
* @param <T> instance type.
196+
* @param serviceLocator HK2 service locator.
197+
* @param clazz class of the instance to be provider.
198+
* @return provider of contract class.
199+
*/
200+
public static <T> Provider<T> getProvider(final ServiceLocator serviceLocator, final Class<T> clazz) {
201+
return () -> serviceLocator.getService(clazz);
202+
}
203+
190204
/**
191205
* Add a binding represented by the binding builder to the HK2 dynamic configuration.
192206
*
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
import java.util.function.Supplier;
4343

44-
import javax.inject.Inject;
4544
import javax.inject.Provider;
4645

4746
import org.glassfish.jersey.server.ContainerRequest;
@@ -53,16 +52,19 @@
5352
* @author Paul Sandoz
5453
* @author Marek Potociar (marek.potociar at oracle.com)
5554
*/
56-
public abstract class AbstractContainerRequestValueSupplier<T> implements Supplier<T> {
57-
@Inject
58-
private Provider<ContainerRequest> request;
55+
public abstract class AbstractRequestDerivedValueSupplier<T> implements Supplier<T> {
56+
private final Provider<ContainerRequest> requestProvider;
57+
58+
protected AbstractRequestDerivedValueSupplier(Provider<ContainerRequest> requestProvider) {
59+
this.requestProvider = requestProvider;
60+
}
5961

6062
/**
6163
* Get the container request.
6264
*
6365
* @return the container request.
6466
*/
65-
protected final ContainerRequest getContainerRequest() {
66-
return request.get();
67+
protected final ContainerRequest getRequest() {
68+
return requestProvider.get();
6769
}
6870
}

core-server/src/main/java/org/glassfish/jersey/server/internal/inject/AbstractValueSupplierProvider.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
import java.util.Set;
4545
import java.util.function.Supplier;
4646

47+
import javax.inject.Provider;
48+
49+
import org.glassfish.jersey.internal.inject.Injections;
50+
import org.glassfish.jersey.server.ContainerRequest;
4751
import org.glassfish.jersey.server.model.Parameter;
4852
import org.glassfish.jersey.server.spi.internal.ValueSupplierProvider;
4953

@@ -63,6 +67,7 @@ public abstract class AbstractValueSupplierProvider implements ValueSupplierProv
6367
private final MultivaluedParameterExtractorProvider mpep;
6468
private final ServiceLocator locator;
6569
private final Set<Parameter.Source> compatibleSources;
70+
private final Provider<ContainerRequest> requestProvider;
6671

6772
/**
6873
* Initialize the provider.
@@ -76,31 +81,20 @@ protected AbstractValueSupplierProvider(MultivaluedParameterExtractorProvider mp
7681
Parameter.Source... compatibleSources) {
7782
this.mpep = mpep;
7883
this.locator = locator;
79-
this.compatibleSources = new HashSet<Parameter.Source>(Arrays.asList(compatibleSources));
80-
}
84+
this.compatibleSources = new HashSet<>(Arrays.asList(compatibleSources));
8185

82-
/**
83-
* Get a parameter extractor that ignores default value set on the parameter.
84-
*
85-
* @param parameter parameter supported by the returned extractor.
86-
* @return extractor supporting the parameter. The returned instance ignores
87-
* any default values set on the parameter.
88-
* @see #get(org.glassfish.jersey.server.model.Parameter)
89-
*/
90-
protected final MultivaluedParameterExtractor<?> getWithoutDefaultValue(Parameter parameter) {
91-
return mpep.getWithoutDefaultValue(parameter);
86+
this.requestProvider = Injections.getProvider(locator, ContainerRequest.class);
9287
}
9388

9489
/**
95-
* Get a parameter extractor. Unlike the extractor returned by the
96-
* {@link #getWithoutDefaultValue(org.glassfish.jersey.server.model.Parameter)}
97-
* method, the extractor returned from this method will use the default value
90+
* Get a parameter extractor.
91+
* The extractor returned from this method will use the default value
9892
* set on the parameter, in case the parameter is not found in the supplied
9993
* {@link javax.ws.rs.core.MultivaluedMap multivalued parameter map}.
10094
*
10195
* @param parameter parameter supported by the returned extractor.
10296
* @return extractor supporting the parameter. The returned instance ignores
103-
* any default values set on the parameter.
97+
* any default values set on the parameter.
10498
*/
10599
protected final MultivaluedParameterExtractor<?> get(Parameter parameter) {
106100
return mpep.get(parameter);
@@ -110,27 +104,29 @@ protected final MultivaluedParameterExtractor<?> get(Parameter parameter) {
110104
* Create a value supplier for the parameter. May return {@code null} in case
111105
* the parameter is not supported by the value supplier provider.
112106
*
113-
* @param parameter parameter requesting the value supplier instance.
107+
* @param parameter parameter requesting the value supplier instance.
108+
* @param requestProvider container request provider that provides request context specific access to the
109+
* {@link ContainerRequest} instance.
114110
* @return parameter value supplier. Returns {@code null} if parameter is not supported.
115111
*/
116-
protected abstract Supplier<?> createValueSupplier(Parameter parameter);
112+
protected abstract AbstractRequestDerivedValueSupplier<?> createValueSupplier(
113+
Parameter parameter, Provider<ContainerRequest> requestProvider);
117114

118115
/**
119116
* Get an injected value supplier for the parameter. May return {@code null}
120117
* in case the parameter is not supported by the value supplier provider.
121118
*
122119
* @param parameter parameter requesting the value supplier instance.
123120
* @return injected parameter value supplier. Returns {@code null} if parameter
124-
* is not supported.
121+
* is not supported.
125122
*/
126123
@Override
127124
public final Supplier<?> getValueSupplier(Parameter parameter) {
128125
if (!compatibleSources.contains(parameter.getSource())) {
129126
// not compatible
130127
return null;
131128
}
132-
133-
final Supplier<?> valueSupplier = createValueSupplier(parameter);
129+
final Supplier<?> valueSupplier = createValueSupplier(parameter, requestProvider);
134130
if (valueSupplier != null) {
135131
locator.inject(valueSupplier);
136132
}

core-server/src/main/java/org/glassfish/jersey/server/internal/inject/BeanParamValueSupplierProvider.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@
4343
import javax.ws.rs.BeanParam;
4444

4545
import javax.inject.Inject;
46+
import javax.inject.Provider;
4647
import javax.inject.Singleton;
4748

4849
import org.glassfish.jersey.process.internal.RequestScoped;
50+
import org.glassfish.jersey.server.ContainerRequest;
4951
import org.glassfish.jersey.server.model.Parameter;
5052

5153
import org.glassfish.hk2.api.ActiveDescriptor;
@@ -81,7 +83,7 @@ public InjectionResolver() {
8183
}
8284
}
8385

84-
private static final class BeanParamValueSupplier extends AbstractContainerRequestValueSupplier<Object> {
86+
private static final class BeanParamValueSupplier extends AbstractRequestDerivedValueSupplier<Object> {
8587
private final Parameter parameter;
8688
private final ServiceLocator locator;
8789

@@ -100,7 +102,9 @@ public ActiveDescriptor<?> compute(Class<?> key) {
100102
}
101103
});
102104

103-
private BeanParamValueSupplier(ServiceLocator locator, Parameter parameter) {
105+
private BeanParamValueSupplier(ServiceLocator locator, Parameter parameter, Provider<ContainerRequest> requestProvider) {
106+
super(requestProvider);
107+
104108
this.locator = locator;
105109
this.parameter = parameter;
106110
}
@@ -131,7 +135,10 @@ public BeanParamValueSupplierProvider(MultivaluedParameterExtractorProvider mpep
131135
}
132136

133137
@Override
134-
public AbstractContainerRequestValueSupplier<?> createValueSupplier(Parameter parameter) {
135-
return new BeanParamValueSupplier(locator, parameter);
138+
public AbstractRequestDerivedValueSupplier<?> createValueSupplier(
139+
Parameter parameter,
140+
Provider<ContainerRequest> requestProvider) {
141+
142+
return new BeanParamValueSupplier(locator, parameter, requestProvider);
136143
}
137144
}

core-server/src/main/java/org/glassfish/jersey/server/internal/inject/CookieParamValueSupplierProvider.java

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
import javax.ws.rs.core.MultivaluedMap;
4747

4848
import javax.inject.Inject;
49+
import javax.inject.Provider;
4950
import javax.inject.Singleton;
5051

5152
import org.glassfish.jersey.internal.inject.ExtractorException;
5253
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
54+
import org.glassfish.jersey.server.ContainerRequest;
5355
import org.glassfish.jersey.server.ParamException;
5456
import org.glassfish.jersey.server.model.Parameter;
5557

@@ -64,6 +66,39 @@
6466
@Singleton
6567
final class CookieParamValueSupplierProvider extends AbstractValueSupplierProvider {
6668

69+
/**
70+
* {@link CookieParam} annotation value factory provider injection constructor.
71+
*
72+
* @param mpep multivalued parameter extractor provider.
73+
* @param injector injector instance.
74+
*/
75+
@Inject
76+
public CookieParamValueSupplierProvider(MultivaluedParameterExtractorProvider mpep, ServiceLocator injector) {
77+
super(mpep, injector, Parameter.Source.COOKIE);
78+
}
79+
80+
@Override
81+
public AbstractRequestDerivedValueSupplier<?> createValueSupplier(
82+
Parameter parameter,
83+
Provider<ContainerRequest> requestProvider) {
84+
85+
String parameterName = parameter.getSourceName();
86+
if (parameterName == null || parameterName.length() == 0) {
87+
// Invalid cookie parameter name
88+
return null;
89+
}
90+
91+
if (parameter.getRawType() == Cookie.class) {
92+
return new CookieTypeParamValueSupplier(parameterName, requestProvider);
93+
} else {
94+
MultivaluedParameterExtractor e = get(parameter);
95+
if (e == null) {
96+
return null;
97+
}
98+
return new CookieParamValueSupplier(e, requestProvider);
99+
}
100+
}
101+
67102
/**
68103
* Injection resolver for {@link CookieParam} annotation.
69104
*/
@@ -78,11 +113,13 @@ public InjectionResolver() {
78113
}
79114
}
80115

81-
private static final class CookieParamValueSupplier extends AbstractContainerRequestValueSupplier<Object> {
116+
private static final class CookieParamValueSupplier extends AbstractRequestDerivedValueSupplier<Object> {
82117

83118
private final MultivaluedParameterExtractor<?> extractor;
84119

85-
CookieParamValueSupplier(MultivaluedParameterExtractor<?> extractor) {
120+
CookieParamValueSupplier(MultivaluedParameterExtractor<?> extractor, Provider<ContainerRequest> requestProvider) {
121+
super(requestProvider);
122+
86123
this.extractor = extractor;
87124
}
88125

@@ -91,7 +128,7 @@ public Object get() {
91128
// TODO: cache?
92129
MultivaluedMap<String, String> cookies = new MultivaluedStringMap();
93130

94-
for (Map.Entry<String, Cookie> e : getContainerRequest().getCookies().entrySet()) {
131+
for (Map.Entry<String, Cookie> e : getRequest().getCookies().entrySet()) {
95132
cookies.putSingle(e.getKey(), e.getValue().getValue());
96133
}
97134

@@ -104,47 +141,19 @@ public Object get() {
104141
}
105142
}
106143

107-
private static final class CookieTypeParamValueSupplier extends AbstractContainerRequestValueSupplier<Cookie> {
144+
private static final class CookieTypeParamValueSupplier extends AbstractRequestDerivedValueSupplier<Cookie> {
108145

109146
private final String name;
110147

111-
CookieTypeParamValueSupplier(String name) {
148+
CookieTypeParamValueSupplier(String name, Provider<ContainerRequest> requestProvider) {
149+
super(requestProvider);
150+
112151
this.name = name;
113152
}
114153

115154
@Override
116155
public Cookie get() {
117-
return getContainerRequest().getCookies().get(name);
118-
}
119-
}
120-
121-
/**
122-
* {@link CookieParam} annotation value factory provider injection constructor.
123-
*
124-
* @param mpep multivalued parameter extractor provider.
125-
* @param injector injector instance.
126-
*/
127-
@Inject
128-
public CookieParamValueSupplierProvider(MultivaluedParameterExtractorProvider mpep, ServiceLocator injector) {
129-
super(mpep, injector, Parameter.Source.COOKIE);
130-
}
131-
132-
@Override
133-
public AbstractContainerRequestValueSupplier<?> createValueSupplier(Parameter parameter) {
134-
String parameterName = parameter.getSourceName();
135-
if (parameterName == null || parameterName.length() == 0) {
136-
// Invalid cookie parameter name
137-
return null;
138-
}
139-
140-
if (parameter.getRawType() == Cookie.class) {
141-
return new CookieTypeParamValueSupplier(parameterName);
142-
} else {
143-
MultivaluedParameterExtractor e = get(parameter);
144-
if (e == null) {
145-
return null;
146-
}
147-
return new CookieParamValueSupplier(e);
156+
return getRequest().getCookies().get(name);
148157
}
149158
}
150159
}

core-server/src/main/java/org/glassfish/jersey/server/internal/inject/EntityParamValueSupplierProvider.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.ws.rs.core.Response;
4646

4747
import javax.inject.Inject;
48+
import javax.inject.Provider;
4849
import javax.inject.Singleton;
4950

5051
import org.glassfish.jersey.server.ContainerRequest;
@@ -73,17 +74,27 @@ class EntityParamValueSupplierProvider extends AbstractValueSupplierProvider {
7374
super(mpep, injector, Parameter.Source.ENTITY);
7475
}
7576

76-
private static class EntityValueSupplier extends AbstractContainerRequestValueSupplier<Object> {
77+
@Override
78+
protected AbstractRequestDerivedValueSupplier<?> createValueSupplier(
79+
Parameter parameter,
80+
Provider<ContainerRequest> requestProvider) {
81+
82+
return new EntityValueSupplier(parameter, requestProvider);
83+
}
84+
85+
private static class EntityValueSupplier extends AbstractRequestDerivedValueSupplier<Object> {
7786

7887
private final Parameter parameter;
7988

80-
public EntityValueSupplier(Parameter parameter) {
89+
public EntityValueSupplier(Parameter parameter, Provider<ContainerRequest> requestProvider) {
90+
super(requestProvider);
91+
8192
this.parameter = parameter;
8293
}
8394

8495
@Override
8596
public Object get() {
86-
final ContainerRequest requestContext = getContainerRequest();
97+
final ContainerRequest requestContext = getRequest();
8798

8899
final Class<?> rawType = parameter.getRawType();
89100

@@ -102,9 +113,4 @@ public Object get() {
102113

103114
}
104115
}
105-
106-
@Override
107-
protected AbstractContainerRequestValueSupplier<?> createValueSupplier(Parameter parameter) {
108-
return new EntityValueSupplier(parameter);
109-
}
110116
}

0 commit comments

Comments
 (0)