Skip to content

Commit 71dbd7b

Browse files
committed
Remove qetQueryParams from ServerHttpRequest
1 parent 4c0490a commit 71dbd7b

6 files changed

Lines changed: 32 additions & 67 deletions

File tree

spring-web/src/main/java/org/springframework/http/server/ServerHttpRequest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.springframework.http.HttpInputMessage;
2323
import org.springframework.http.HttpRequest;
24-
import org.springframework.util.MultiValueMap;
2524

2625
/**
2726
* Represents a server-side HTTP request.
@@ -32,11 +31,6 @@
3231
*/
3332
public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
3433

35-
/**
36-
* Returns the map of query parameters. Empty if no query has been set.
37-
*/
38-
MultiValueMap<String, String> getQueryParams();
39-
4034
/**
4135
* Return a {@link java.security.Principal} instance containing the name of the
4236
* authenticated user. If the user has not been authenticated, the method returns

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,13 @@
3434
import java.util.Iterator;
3535
import java.util.List;
3636
import java.util.Map;
37-
import java.util.regex.Matcher;
38-
import java.util.regex.Pattern;
3937

4038
import javax.servlet.http.HttpServletRequest;
4139

4240
import org.springframework.http.HttpHeaders;
4341
import org.springframework.http.HttpMethod;
4442
import org.springframework.http.MediaType;
4543
import org.springframework.util.Assert;
46-
import org.springframework.util.CollectionUtils;
47-
import org.springframework.util.LinkedMultiValueMap;
48-
import org.springframework.util.MultiValueMap;
4944

5045
/**
5146
* {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}.
@@ -61,14 +56,10 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
6156

6257
private static final String METHOD_POST = "POST";
6358

64-
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
65-
6659
private final HttpServletRequest servletRequest;
6760

6861
private HttpHeaders headers;
6962

70-
private MultiValueMap<String, String> queryParams;
71-
7263
private ServerHttpAsyncRequestControl asyncRequestControl;
7364

7465

@@ -154,26 +145,6 @@ public InetSocketAddress getRemoteAddress() {
154145
return new InetSocketAddress(this.servletRequest.getRemoteHost(), this.servletRequest.getRemotePort());
155146
}
156147

157-
@Override
158-
public MultiValueMap<String, String> getQueryParams() {
159-
if (this.queryParams == null) {
160-
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>();
161-
String queryString = this.servletRequest.getQueryString();
162-
if (queryString != null) {
163-
Matcher m = QUERY_PARAM_PATTERN.matcher(queryString);
164-
while (m.find()) {
165-
String name = m.group(1);
166-
String[] values = this.servletRequest.getParameterValues(name);
167-
if (values != null) {
168-
result.put(name, Arrays.asList(values));
169-
}
170-
}
171-
}
172-
this.queryParams = CollectionUtils.unmodifiableMultiValueMap(result);
173-
}
174-
return this.queryParams;
175-
}
176-
177148
@Override
178149
public InputStream getBody() throws IOException {
179150
if (isFormPost(this.servletRequest)) {

spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.net.URI;
2020
import java.nio.charset.Charset;
21-
import java.util.Arrays;
2221
import java.util.List;
2322

2423
import org.junit.Before;
@@ -106,21 +105,4 @@ public void getFormBody() throws Exception {
106105
assertArrayEquals("Invalid content returned", content, result);
107106
}
108107

109-
@Test
110-
public void getQueryParams() throws Exception {
111-
mockRequest.setQueryString("foo=bar");
112-
mockRequest.addParameter("foo", "bar");
113-
mockRequest.addParameter("a", "b");
114-
assertEquals(Arrays.asList("bar"), request.getQueryParams().get("foo"));
115-
assertNull(request.getQueryParams().get("a"));
116-
}
117-
118-
@Test
119-
public void getQueryParamsTwoValues() throws Exception {
120-
mockRequest.setQueryString("baz=qux&baz=42");
121-
mockRequest.addParameter("baz", "qux");
122-
mockRequest.addParameter("baz", "42");
123-
assertEquals(Arrays.asList("qux", "42"), request.getQueryParams().get("baz"));
124-
}
125-
126108
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
package org.springframework.web.socket.sockjs.transport.handler;
1818

1919
import java.io.IOException;
20+
import java.io.UnsupportedEncodingException;
2021

2122
import org.springframework.http.MediaType;
2223
import org.springframework.http.server.ServerHttpRequest;
2324
import org.springframework.http.server.ServerHttpResponse;
25+
import org.springframework.util.MultiValueMap;
26+
import org.springframework.util.StringUtils;
2427
import org.springframework.web.socket.WebSocketHandler;
2528
import org.springframework.web.socket.WebSocketSession;
2629
import org.springframework.web.socket.sockjs.SockJsException;
2730
import org.springframework.web.socket.sockjs.support.frame.SockJsFrame;
2831
import org.springframework.web.socket.sockjs.support.frame.SockJsFrame.FrameFormat;
2932
import org.springframework.web.socket.sockjs.transport.TransportHandler;
3033
import org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession;
34+
import org.springframework.web.util.UriComponentsBuilder;
35+
import org.springframework.web.util.UriUtils;
3136

3237
/**
3338
* Base class for HTTP transport handlers that push messages to connected clients.
@@ -81,4 +86,17 @@ else if (!sockJsSession.isActive()) {
8186

8287
protected abstract FrameFormat getFrameFormat(ServerHttpRequest request);
8388

89+
protected final String getCallbackParam(ServerHttpRequest request) {
90+
String query = request.getURI().getQuery();
91+
MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
92+
String value = params.getFirst("c");
93+
try {
94+
return StringUtils.isEmpty(value) ? null : UriUtils.decode(value, "UTF-8");
95+
}
96+
catch (UnsupportedEncodingException e) {
97+
// should never happen
98+
throw new SockJsException("Unable to decode callback query parameter", null, e);
99+
}
100+
}
101+
84102
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ public StreamingSockJsSession createSession(String sessionId, WebSocketHandler w
9898
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
9999
AbstractHttpSockJsSession sockJsSession) {
100100

101-
String callback = request.getQueryParams().getFirst("c");
102-
if (! StringUtils.hasText(callback)) {
103-
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
104-
try {
105-
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
106-
}
107-
catch (IOException t) {
108-
sockJsSession.tryCloseWithSockJsTransportError(t, CloseStatus.SERVER_ERROR);
109-
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), t);
110-
}
111-
return;
101+
String callback = getCallbackParam(request);
102+
if (! StringUtils.hasText(callback)) {
103+
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
104+
try {
105+
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
112106
}
107+
catch (IOException t) {
108+
sockJsSession.tryCloseWithSockJsTransportError(t, CloseStatus.SERVER_ERROR);
109+
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), t);
110+
}
111+
return;
112+
}
113113

114114
super.handleRequestInternal(request, response, sockJsSession);
115115
}
@@ -137,7 +137,7 @@ private HtmlFileStreamingSockJsSession(String sessionId, SockJsServiceConfig con
137137
protected void writePrelude() throws IOException {
138138

139139
// we already validated the parameter above..
140-
String callback = getRequest().getQueryParams().getFirst("c");
140+
String callback = getCallbackParam(getRequest());
141141

142142
String html = String.format(PARTIAL_HTML_CONTENT, callback);
143143
getResponse().getBody().write(html.getBytes("UTF-8"));

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpPollingTransportHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse
6565
AbstractHttpSockJsSession sockJsSession) throws SockJsException {
6666

6767
try {
68-
String callback = request.getQueryParams().getFirst("c");
68+
String callback = getCallbackParam(request);
6969
if (! StringUtils.hasText(callback)) {
7070
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
7171
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
@@ -84,7 +84,7 @@ public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse
8484
protected FrameFormat getFrameFormat(ServerHttpRequest request) {
8585

8686
// we already validated the parameter above..
87-
String callback = request.getQueryParams().getFirst("c");
87+
String callback = getCallbackParam(request);
8888

8989
return new SockJsFrame.DefaultFrameFormat(callback + "(\"%s\");\r\n") {
9090
@Override

0 commit comments

Comments
 (0)