Remove qetQueryParams from ServerHttpRequest

This commit is contained in:
Rossen Stoyanchev
2013-08-14 11:43:37 -04:00
parent 4c0490a070
commit 71dbd7bc1f
6 changed files with 32 additions and 67 deletions

View 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);
}
}
}

View File

@@ -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"));

View File

@@ -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