提交 71dbd7bc 编写于 作者: R Rossen Stoyanchev

Remove qetQueryParams from ServerHttpRequest

上级 4c0490a0
......@@ -21,7 +21,6 @@ import java.security.Principal;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpRequest;
import org.springframework.util.MultiValueMap;
/**
* Represents a server-side HTTP request.
......@@ -32,11 +31,6 @@ import org.springframework.util.MultiValueMap;
*/
public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
/**
* Returns the map of query parameters. Empty if no query has been set.
*/
MultiValueMap<String, String> getQueryParams();
/**
* Return a {@link java.security.Principal} instance containing the name of the
* authenticated user. If the user has not been authenticated, the method returns
......
......@@ -34,8 +34,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
......@@ -43,9 +41,6 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}.
......@@ -61,14 +56,10 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
private static final String METHOD_POST = "POST";
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
private final HttpServletRequest servletRequest;
private HttpHeaders headers;
private MultiValueMap<String, String> queryParams;
private ServerHttpAsyncRequestControl asyncRequestControl;
......@@ -154,26 +145,6 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
return new InetSocketAddress(this.servletRequest.getRemoteHost(), this.servletRequest.getRemotePort());
}
@Override
public MultiValueMap<String, String> getQueryParams() {
if (this.queryParams == null) {
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>();
String queryString = this.servletRequest.getQueryString();
if (queryString != null) {
Matcher m = QUERY_PARAM_PATTERN.matcher(queryString);
while (m.find()) {
String name = m.group(1);
String[] values = this.servletRequest.getParameterValues(name);
if (values != null) {
result.put(name, Arrays.asList(values));
}
}
}
this.queryParams = CollectionUtils.unmodifiableMultiValueMap(result);
}
return this.queryParams;
}
@Override
public InputStream getBody() throws IOException {
if (isFormPost(this.servletRequest)) {
......
......@@ -18,7 +18,6 @@ package org.springframework.http.server;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
......@@ -106,21 +105,4 @@ public class ServletServerHttpRequestTests {
assertArrayEquals("Invalid content returned", content, result);
}
@Test
public void getQueryParams() throws Exception {
mockRequest.setQueryString("foo=bar");
mockRequest.addParameter("foo", "bar");
mockRequest.addParameter("a", "b");
assertEquals(Arrays.asList("bar"), request.getQueryParams().get("foo"));
assertNull(request.getQueryParams().get("a"));
}
@Test
public void getQueryParamsTwoValues() throws Exception {
mockRequest.setQueryString("baz=qux&baz=42");
mockRequest.addParameter("baz", "qux");
mockRequest.addParameter("baz", "42");
assertEquals(Arrays.asList("qux", "42"), request.getQueryParams().get("baz"));
}
}
\ No newline at end of file
......@@ -17,10 +17,13 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.sockjs.SockJsException;
......@@ -28,6 +31,8 @@ import org.springframework.web.socket.sockjs.support.frame.SockJsFrame;
import org.springframework.web.socket.sockjs.support.frame.SockJsFrame.FrameFormat;
import org.springframework.web.socket.sockjs.transport.TransportHandler;
import org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
/**
* Base class for HTTP transport handlers that push messages to connected clients.
......@@ -81,4 +86,17 @@ public abstract class AbstractHttpSendingTransportHandler extends TransportHandl
protected abstract FrameFormat getFrameFormat(ServerHttpRequest request);
protected final String getCallbackParam(ServerHttpRequest request) {
String query = request.getURI().getQuery();
MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
String value = params.getFirst("c");
try {
return StringUtils.isEmpty(value) ? null : UriUtils.decode(value, "UTF-8");
}
catch (UnsupportedEncodingException e) {
// should never happen
throw new SockJsException("Unable to decode callback query parameter", null, e);
}
}
}
......@@ -98,18 +98,18 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
AbstractHttpSockJsSession sockJsSession) {
String callback = request.getQueryParams().getFirst("c");
if (! StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
try {
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
}
catch (IOException t) {
sockJsSession.tryCloseWithSockJsTransportError(t, CloseStatus.SERVER_ERROR);
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), t);
}
return;
String callback = getCallbackParam(request);
if (! StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
try {
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
}
catch (IOException t) {
sockJsSession.tryCloseWithSockJsTransportError(t, CloseStatus.SERVER_ERROR);
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), t);
}
return;
}
super.handleRequestInternal(request, response, sockJsSession);
}
......@@ -137,7 +137,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
protected void writePrelude() throws IOException {
// we already validated the parameter above..
String callback = getRequest().getQueryParams().getFirst("c");
String callback = getCallbackParam(getRequest());
String html = String.format(PARTIAL_HTML_CONTENT, callback);
getResponse().getBody().write(html.getBytes("UTF-8"));
......
......@@ -65,7 +65,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
AbstractHttpSockJsSession sockJsSession) throws SockJsException {
try {
String callback = request.getQueryParams().getFirst("c");
String callback = getCallbackParam(request);
if (! StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
......@@ -84,7 +84,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
protected FrameFormat getFrameFormat(ServerHttpRequest request) {
// we already validated the parameter above..
String callback = request.getQueryParams().getFirst("c");
String callback = getCallbackParam(request);
return new SockJsFrame.DefaultFrameFormat(callback + "(\"%s\");\r\n") {
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册