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

Commit 6e87d76

Browse files
pavelbucekGerrit Code Review
authored andcommitted
Merge "JERSEY-2656: Fix NullPointer during injection a FormParam"
2 parents 2b27635 + 98e536f commit 6e87d76

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
import javax.ws.rs.core.Form;
5454
import javax.ws.rs.core.HttpHeaders;
5555
import javax.ws.rs.core.MediaType;
56+
import javax.ws.rs.core.MultivaluedMap;
5657

5758
import javax.inject.Inject;
5859
import javax.inject.Singleton;
5960

6061
import org.glassfish.jersey.internal.inject.ExtractorException;
62+
import org.glassfish.jersey.internal.util.collection.NullableMultivaluedHashMap;
6163
import org.glassfish.jersey.message.internal.MediaTypes;
6264
import org.glassfish.jersey.message.internal.ReaderWriter;
6365
import org.glassfish.jersey.server.ContainerRequest;
@@ -128,7 +130,8 @@ public Object provide() {
128130

129131
private Form switchUrlEncoding(final ContainerRequest request, final Form otherForm) {
130132
final Set<Map.Entry<String, List<String>>> entries = otherForm.asMap().entrySet();
131-
Form newForm = new Form();
133+
134+
MultivaluedMap<String, String> formMap = new NullableMultivaluedHashMap<>();
132135
for (Map.Entry<String, List<String>> entry : entries) {
133136
final String charsetName = ReaderWriter.getCharset(MediaType.valueOf(
134137
request.getHeaderString(HttpHeaders.CONTENT_TYPE))).name();
@@ -139,16 +142,20 @@ private Form switchUrlEncoding(final ContainerRequest request, final Form otherF
139142
charsetName);
140143

141144
for (String value : entry.getValue()) {
142-
newForm.asMap().add(key, decode ? URLDecoder.decode(value, charsetName) : URLEncoder.encode(value,
143-
charsetName));
145+
if (value != null) {
146+
formMap.add(key,
147+
decode ? URLDecoder.decode(value, charsetName) : URLEncoder.encode(value, charsetName));
148+
} else {
149+
formMap.add(key, null);
150+
}
144151
}
145152

146153
} catch (UnsupportedEncodingException uee) {
147154
throw new ProcessingException(LocalizationMessages.ERROR_UNSUPPORTED_ENCODING(charsetName,
148155
extractor.getName()), uee);
149156
}
150157
}
151-
return newForm;
158+
return new Form(formMap);
152159
}
153160

154161
private void cacheForm(final ContainerRequest request, final Form form) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.tests.e2e.server;
41+
42+
import java.util.List;
43+
44+
import javax.ws.rs.Consumes;
45+
import javax.ws.rs.Encoded;
46+
import javax.ws.rs.FormParam;
47+
import javax.ws.rs.POST;
48+
import javax.ws.rs.Path;
49+
import javax.ws.rs.client.Entity;
50+
import javax.ws.rs.core.Application;
51+
import javax.ws.rs.core.MediaType;
52+
import javax.ws.rs.core.Response;
53+
54+
import org.glassfish.jersey.server.ResourceConfig;
55+
import org.glassfish.jersey.test.JerseyTest;
56+
57+
import org.junit.Test;
58+
import static org.junit.Assert.assertEquals;
59+
60+
/**
61+
* Proper encoding of Form params
62+
*
63+
* @author Petr Bouda (petr.bouda at oracle.com)
64+
*/
65+
public class EncodedFormParamTest extends JerseyTest {
66+
67+
@Path("encoded")
68+
public static class UrlEncodedResource {
69+
70+
@Encoded
71+
@FormParam("name")
72+
private String name;
73+
74+
@FormParam("name")
75+
private String otherName;
76+
77+
@POST
78+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
79+
public String get(@FormParam("name") List<String> name) {
80+
return name.toString() + " " + this.name;
81+
}
82+
83+
}
84+
85+
@Override
86+
protected Application configure() {
87+
return new ResourceConfig(UrlEncodedResource.class);
88+
}
89+
90+
@Test
91+
public void testEncodedParam() {
92+
Response result = target().path("encoded").request()
93+
.post(Entity.entity("name&name=George", MediaType.APPLICATION_FORM_URLENCODED_TYPE));
94+
assertEquals("[null, George] null", result.readEntity(String.class));
95+
}
96+
}
97+

0 commit comments

Comments
 (0)