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

Commit 669aecc

Browse files
Petr Boudapavelbucek
authored andcommitted
JERSEY-2642: FIX - Create the new FormEntityValueFactory for URL-ENCODED entity
Change-Id: I47e5afd1dd2cc8b5e9c8ec71e381a53f83648474
1 parent 3b57c12 commit 669aecc

File tree

9 files changed

+268
-89
lines changed

9 files changed

+268
-89
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
import javax.inject.Inject;
4646
import javax.inject.Singleton;
4747

48+
import org.glassfish.jersey.process.internal.RequestScoped;
49+
import org.glassfish.jersey.server.model.Parameter;
50+
4851
import org.glassfish.hk2.api.ActiveDescriptor;
4952
import org.glassfish.hk2.api.ServiceLocator;
5053
import org.glassfish.hk2.utilities.AbstractActiveDescriptor;
@@ -53,9 +56,6 @@
5356
import org.glassfish.hk2.utilities.cache.Cache;
5457
import org.glassfish.hk2.utilities.cache.Computable;
5558

56-
import org.glassfish.jersey.process.internal.RequestScoped;
57-
import org.glassfish.jersey.server.model.Parameter;
58-
5959
/**
6060
* Value factory provider for {@link BeanParam bean parameters}.
6161
*
@@ -121,6 +121,7 @@ public Object provide() {
121121

122122
/**
123123
* Creates new instance initialized from parameters injected by HK2.
124+
*
124125
* @param mpep Multivalued parameter extractor provider.
125126
* @param injector HK2 Service locator.
126127
*/

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

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,13 @@
4141

4242
import javax.ws.rs.BadRequestException;
4343
import javax.ws.rs.container.ContainerRequestContext;
44-
import javax.ws.rs.core.Form;
45-
import javax.ws.rs.core.MediaType;
46-
import javax.ws.rs.core.MultivaluedMap;
4744
import javax.ws.rs.core.Request;
4845
import javax.ws.rs.core.Response;
4946

5047
import javax.inject.Inject;
5148
import javax.inject.Singleton;
5249

53-
import org.glassfish.jersey.message.internal.MediaTypes;
5450
import org.glassfish.jersey.server.ContainerRequest;
55-
import org.glassfish.jersey.server.internal.InternalServerProperties;
5651
import org.glassfish.jersey.server.internal.LocalizationMessages;
5752
import org.glassfish.jersey.server.model.Parameter;
5853

@@ -109,66 +104,8 @@ public Object provide() {
109104
}
110105
}
111106

112-
private static class FormEntityValueFactory extends AbstractContainerRequestValueFactory<Object> {
113-
114-
private final MultivaluedParameterExtractor<?> extractor;
115-
116-
public FormEntityValueFactory(MultivaluedParameterExtractor<?> extractor) {
117-
this.extractor = extractor;
118-
}
119-
120-
@Override
121-
public Object provide() {
122-
final ContainerRequest request = getContainerRequest();
123-
124-
Form form = getCachedForm(request);
125-
if (form == null) {
126-
form = getForm(request);
127-
cacheForm(request, form);
128-
}
129-
130-
return form.asMap();
131-
}
132-
133-
private static Form getCachedForm(final ContainerRequest request) {
134-
return (Form) request.getProperty(InternalServerProperties.FORM_DECODED_PROPERTY);
135-
}
136-
137-
private void cacheForm(final ContainerRequest request, final Form form) {
138-
request.setProperty(InternalServerProperties.FORM_DECODED_PROPERTY, form);
139-
}
140-
141-
private Form getForm(final ContainerRequest request) {
142-
return getFormParameters(ensureValidRequest(request));
143-
}
144-
145-
private static ContainerRequest ensureValidRequest(final ContainerRequest request) throws IllegalStateException {
146-
if (request.getMethod().equals("GET")) {
147-
throw new IllegalStateException(LocalizationMessages.FORM_PARAM_METHOD_ERROR());
148-
}
149-
150-
if (!MediaTypes.typeEqual(MediaType.APPLICATION_FORM_URLENCODED_TYPE, request.getMediaType())) {
151-
throw new IllegalStateException(LocalizationMessages.FORM_PARAM_CONTENT_TYPE_ERROR());
152-
}
153-
return request;
154-
}
155-
156-
private Form getFormParameters(ContainerRequest request) {
157-
if (MediaTypes.typeEqual(MediaType.APPLICATION_FORM_URLENCODED_TYPE, request.getMediaType())) {
158-
request.bufferEntity();
159-
Form form = request.readEntity(Form.class);
160-
return (form == null ? new Form() : form);
161-
} else {
162-
return new Form();
163-
}
164-
}
165-
}
166-
167107
@Override
168108
protected Factory<?> createValueFactory(Parameter parameter) {
169-
if (MultivaluedMap.class.isAssignableFrom(parameter.getRawType())) {
170-
return new FormEntityValueFactory(get(parameter));
171-
}
172109
return new EntityValueFactory(parameter);
173110
}
174111
}

core-server/src/main/java/org/glassfish/jersey/server/internal/routing/SubResourceLocatorRouter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
import org.glassfish.jersey.server.model.Resource;
6060
import org.glassfish.jersey.server.model.ResourceMethod;
6161
import org.glassfish.jersey.server.monitoring.RequestEvent;
62+
import org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource;
6263
import org.glassfish.jersey.server.spi.internal.ParameterValueHelper;
6364

64-
import org.glassfish.hk2.api.Factory;
6565
import org.glassfish.hk2.api.ServiceLocator;
6666

6767
/**
@@ -78,7 +78,7 @@
7878
final class SubResourceLocatorRouter implements Router {
7979

8080
private final ResourceMethod locatorModel;
81-
private final List<Factory<?>> valueProviders;
81+
private final List<ParamValueFactoryWithSource<?>> valueProviders;
8282
private final RuntimeLocatorModelBuilder runtimeLocatorBuilder;
8383
private final JerseyResourceContext resourceContext;
8484

core-server/src/main/java/org/glassfish/jersey/server/model/Invocable.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,10 @@ public boolean isInflector() {
313313
* values for parameters of this Invocable returned by {@link #getParameters()}. Value providers are ordered in the same
314314
* order as parameters.
315315
*
316-
*
317316
* @param locator HK2 service locator.
318317
* @return Set of value providers for this Invocable.
319318
*/
320-
public List<Factory<?>> getValueProviders(ServiceLocator locator) {
319+
public List<? extends Factory<?>> getValueProviders(ServiceLocator locator) {
321320
return ParameterValueHelper.createValueProviders(locator, this);
322321
}
323322

core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private void checkMethod(ResourceMethod method) {
162162
}
163163

164164
private void checkValueProviders(ResourceMethod method) {
165-
final List<Factory<?>> valueProviders = method.getInvocable().getValueProviders(locator);
165+
final List<? extends Factory<?>> valueProviders = method.getInvocable().getValueProviders(locator);
166166
if (valueProviders.contains(null)) {
167167
int index = valueProviders.indexOf(null);
168168
Errors.fatal(method, LocalizationMessages.ERROR_PARAMETER_MISSING_VALUE_PROVIDER(index, method.getInvocable()

core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
import org.glassfish.jersey.server.ContainerRequest;
5353
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
5454
import org.glassfish.jersey.server.model.Invocable;
55+
import org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource;
5556
import org.glassfish.jersey.server.spi.internal.ParameterValueHelper;
5657
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
5758

58-
import org.glassfish.hk2.api.Factory;
5959
import org.glassfish.hk2.api.ServiceLocator;
6060

6161
/**
@@ -74,7 +74,8 @@ class JavaResourceMethodDispatcherProvider implements ResourceMethodDispatcher.P
7474
public ResourceMethodDispatcher create(final Invocable resourceMethod,
7575
final InvocationHandler invocationHandler,
7676
final ConfiguredValidator validator) {
77-
final List<Factory<?>> valueProviders = resourceMethod.getValueProviders(serviceLocator);
77+
final List<ParamValueFactoryWithSource<?>> valueProviders =
78+
ParameterValueHelper.createValueProviders(serviceLocator, resourceMethod);
7879
final Class<?> returnType = resourceMethod.getHandlingMethod().getReturnType();
7980

8081
ResourceMethodDispatcher resourceMethodDispatcher;
@@ -111,12 +112,12 @@ final ServiceLocator getServiceLocator() {
111112

112113
private abstract static class AbstractMethodParamInvoker extends AbstractJavaResourceMethodDispatcher {
113114

114-
private final List<Factory<?>> valueProviders;
115+
private final List<ParamValueFactoryWithSource<?>> valueProviders;
115116

116117
public AbstractMethodParamInvoker(
117118
final Invocable resourceMethod,
118119
final InvocationHandler handler,
119-
final List<Factory<?>> valueProviders,
120+
final List<ParamValueFactoryWithSource<?>> valueProviders,
120121
final ConfiguredValidator validator) {
121122
super(resourceMethod, handler, validator);
122123
this.valueProviders = valueProviders;
@@ -132,7 +133,7 @@ private static final class VoidOutInvoker extends AbstractMethodParamInvoker {
132133
public VoidOutInvoker(
133134
final Invocable resourceMethod,
134135
final InvocationHandler handler,
135-
final List<Factory<?>> valueProviders,
136+
final List<ParamValueFactoryWithSource<?>> valueProviders,
136137
final ConfiguredValidator validator) {
137138
super(resourceMethod, handler, valueProviders, validator);
138139
}
@@ -149,7 +150,7 @@ private static final class ResponseOutInvoker extends AbstractMethodParamInvoker
149150
public ResponseOutInvoker(
150151
final Invocable resourceMethod,
151152
final InvocationHandler handler,
152-
final List<Factory<?>> valueProviders,
153+
final List<ParamValueFactoryWithSource<?>> valueProviders,
153154
final ConfiguredValidator validator) {
154155
super(resourceMethod, handler, valueProviders, validator);
155156
}
@@ -165,7 +166,7 @@ private static final class ObjectOutInvoker extends AbstractMethodParamInvoker {
165166
public ObjectOutInvoker(
166167
final Invocable resourceMethod,
167168
final InvocationHandler handler,
168-
final List<Factory<?>> valueProviders,
169+
final List<ParamValueFactoryWithSource<?>> valueProviders,
169170
final ConfiguredValidator validator) {
170171
super(resourceMethod, handler, valueProviders, validator);
171172
}
@@ -193,7 +194,7 @@ private static final class TypeOutInvoker extends AbstractMethodParamInvoker {
193194
public TypeOutInvoker(
194195
final Invocable resourceMethod,
195196
final InvocationHandler handler,
196-
final List<Factory<?>> valueProviders,
197+
final List<ParamValueFactoryWithSource<?>> valueProviders,
197198
final ConfiguredValidator validator) {
198199
super(resourceMethod, handler, valueProviders, validator);
199200
this.t = resourceMethod.getHandlingMethod().getGenericReturnType();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.server.spi.internal;
41+
42+
import org.glassfish.jersey.server.model.Parameter;
43+
44+
import org.glassfish.hk2.api.Factory;
45+
46+
/**
47+
* Extends {@link Factory} interface with
48+
* {@link org.glassfish.jersey.server.model.Parameter.Source} information.
49+
*
50+
* @param <T> This must be the type of entity for which this is a factory.
51+
* @author Petr Bouda (petr.bouda at oracle.com)
52+
*/
53+
public final class ParamValueFactoryWithSource<T> implements Factory<T> {
54+
55+
private final Factory<T> factory;
56+
private final Parameter.Source parameterSource;
57+
58+
/**
59+
* Wrap provided param factory.
60+
*
61+
* @param factory param factory to be wrapped.
62+
* @param parameterSource param source.
63+
*/
64+
public ParamValueFactoryWithSource(Factory<T> factory, Parameter.Source parameterSource) {
65+
this.factory = factory;
66+
this.parameterSource = parameterSource;
67+
}
68+
69+
@Override
70+
public T provide() {
71+
return factory.provide();
72+
}
73+
74+
@Override
75+
public void dispose(T t) {
76+
factory.dispose(t);
77+
}
78+
79+
/**
80+
* Returns {@link org.glassfish.jersey.server.model.Parameter.Source}
81+
* which closely determines a function of the given factory.
82+
*
83+
* @return Source which a given parameter belongs to.
84+
**/
85+
public Parameter.Source getSource() {
86+
return parameterSource;
87+
}
88+
89+
}

0 commit comments

Comments
 (0)