Add first cut of SockJS server support

This commit is contained in:
Rossen Stoyanchev
2013-04-01 15:39:04 -04:00
parent 30ab5953f9
commit 88447e503b
57 changed files with 3220 additions and 50 deletions

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http;
public interface Cookie {
String getName();
String getValue();
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Cookies {
private final List<Cookie> cookies;
public Cookies() {
this.cookies = new ArrayList<Cookie>();
}
private Cookies(Cookies cookies) {
this.cookies = Collections.unmodifiableList(cookies.getCookies());
}
public static Cookies readOnlyCookies(Cookies cookies) {
return new Cookies(cookies);
}
public List<Cookie> getCookies() {
return this.cookies;
}
public Cookie getCookie(String name) {
for (Cookie c : this.cookies) {
if (c.getName().equals(name)) {
return c;
}
}
return null;
}
public Cookie addCookie(String name, String value) {
DefaultCookie cookie = new DefaultCookie(name, value);
this.cookies.add(cookie);
return cookie;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http;
import org.springframework.util.Assert;
public class DefaultCookie implements Cookie {
private final String name;
private final String value;
DefaultCookie(String name, String value) {
Assert.hasText(name, "cookie name must not be empty");
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}

View File

@@ -31,4 +31,9 @@ public interface HttpMessage {
*/
HttpHeaders getHeaders();
/**
* TODO ..
*/
Cookies getCookies();
}

View File

@@ -19,6 +19,7 @@ package org.springframework.http.client;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
@@ -44,6 +45,11 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
return getBodyInternal(this.headers);
}
public Cookies getCookies() {
// TODO
throw new UnsupportedOperationException();
}
public final ClientHttpResponse execute() throws IOException {
checkExecuted();
ClientHttpResponse result = executeInternal(this.headers);

View File

@@ -18,6 +18,7 @@ package org.springframework.http.client;
import java.io.IOException;
import org.springframework.http.Cookies;
import org.springframework.http.HttpStatus;
/**
@@ -32,4 +33,9 @@ public abstract class AbstractClientHttpResponse implements ClientHttpResponse {
return HttpStatus.valueOf(getRawStatusCode());
}
public Cookies getCookies() {
// TODO
throw new UnsupportedOperationException();
}
}

View File

@@ -17,9 +17,9 @@
package org.springframework.http.client;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
@@ -58,4 +58,9 @@ final class BufferingClientHttpRequestWrapper extends AbstractBufferingClientHtt
return new BufferingClientHttpResponseWrapper(response);
}
@Override
public Cookies getCookies() {
return this.request.getCookies();
}
}

View File

@@ -20,9 +20,9 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
/**
@@ -67,6 +67,10 @@ final class BufferingClientHttpResponseWrapper implements ClientHttpResponse {
return new ByteArrayInputStream(this.body);
}
public Cookies getCookies() {
return this.response.getCookies();
}
public void close() {
this.response.close();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.http.client.support;
import java.net.URI;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
@@ -73,4 +74,11 @@ public class HttpRequestWrapper implements HttpRequest {
return this.request.getHeaders();
}
/**
* Returns the cookies of the wrapped request.
*/
public Cookies getCookies() {
return this.request.getCookies();
}
}

View File

@@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Random;
import org.springframework.core.io.Resource;
import org.springframework.http.Cookies;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
@@ -383,6 +384,11 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
return this.os;
}
public Cookies getCookies() {
// TODO
throw new UnsupportedOperationException();
}
private void writeHeaders() throws IOException {
if (!this.headersWritten) {
for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.server;
/**
* TODO..
*/
public interface AsyncServerHttpRequest extends ServerHttpRequest {
void setTimeout(long timeout);
void startAsync();
boolean isAsyncStarted();
void completeAsync();
boolean isAsyncCompleted();
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
public class AsyncServletServerHttpRequest extends ServletServerHttpRequest
implements AsyncServerHttpRequest, AsyncListener {
private Long timeout;
private AsyncContext asyncContext;
private AtomicBoolean asyncCompleted = new AtomicBoolean(false);
private final List<Runnable> timeoutHandlers = new ArrayList<Runnable>();
private final List<Runnable> completionHandlers = new ArrayList<Runnable>();
private final HttpServletResponse servletResponse;
/**
* Create a new instance for the given request/response pair.
*/
public AsyncServletServerHttpRequest(HttpServletRequest request, HttpServletResponse response) {
super(request);
this.servletResponse = response;
}
/**
* Timeout period begins after the container thread has exited.
*/
public void setTimeout(long timeout) {
Assert.state(!isAsyncStarted(), "Cannot change the timeout with concurrent handling in progress");
this.timeout = timeout;
}
public void addTimeoutHandler(Runnable timeoutHandler) {
this.timeoutHandlers.add(timeoutHandler);
}
public void addCompletionHandler(Runnable runnable) {
this.completionHandlers.add(runnable);
}
public boolean isAsyncStarted() {
return ((this.asyncContext != null) && getServletRequest().isAsyncStarted());
}
/**
* Whether async request processing has completed.
* <p>It is important to avoid use of request and response objects after async
* processing has completed. Servlet containers often re-use them.
*/
public boolean isAsyncCompleted() {
return this.asyncCompleted.get();
}
public void startAsync() {
Assert.state(getServletRequest().isAsyncSupported(),
"Async support must be enabled on a servlet and for all filters involved " +
"in async request processing. This is done in Java code using the Servlet API " +
"or by adding \"<async-supported>true</async-supported>\" to servlet and " +
"filter declarations in web.xml.");
Assert.state(!isAsyncCompleted(), "Async processing has already completed");
if (isAsyncStarted()) {
return;
}
this.asyncContext = getServletRequest().startAsync(getServletRequest(), this.servletResponse);
this.asyncContext.addListener(this);
if (this.timeout != null) {
this.asyncContext.setTimeout(this.timeout);
}
}
public void dispatch() {
Assert.notNull(this.asyncContext, "Cannot dispatch without an AsyncContext");
this.asyncContext.dispatch();
}
public void completeAsync() {
Assert.notNull(this.asyncContext, "Cannot dispatch without an AsyncContext");
if (isAsyncStarted() && !isAsyncCompleted()) {
this.asyncContext.complete();
}
}
// ---------------------------------------------------------------------
// Implementation of AsyncListener methods
// ---------------------------------------------------------------------
public void onStartAsync(AsyncEvent event) throws IOException {
}
public void onError(AsyncEvent event) throws IOException {
}
public void onTimeout(AsyncEvent event) throws IOException {
for (Runnable handler : this.timeoutHandlers) {
handler.run();
}
}
public void onComplete(AsyncEvent event) throws IOException {
for (Runnable handler : this.completionHandlers) {
handler.run();
}
this.asyncContext = null;
this.asyncCompleted.set(true);
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.http.server;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpRequest;
import org.springframework.util.MultiValueMap;
/**
* Represents a server-side HTTP request.
@@ -27,4 +28,9 @@ import org.springframework.http.HttpRequest;
*/
public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
/**
* Returns the map of query parameters. Empty if no query has been set.
*/
MultiValueMap<String, String> getQueryParams();
}

View File

@@ -33,12 +33,16 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}.
@@ -58,6 +62,10 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
private HttpHeaders headers;
private Cookies cookies;
private MultiValueMap<String, String> queryParams;
/**
* Construct a new instance of the ServletServerHttpRequest based on the given {@link HttpServletRequest}.
@@ -123,6 +131,28 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
return this.headers;
}
public Cookies getCookies() {
if (this.cookies == null) {
this.cookies = new Cookies();
for (Cookie cookie : this.servletRequest.getCookies()) {
this.cookies.addCookie(cookie.getName(), cookie.getValue());
}
}
return this.cookies;
}
public MultiValueMap<String, String> getQueryParams() {
if (this.queryParams == null) {
this.queryParams = new LinkedMultiValueMap<String, String>(this.servletRequest.getParameterMap().size());
for (String name : this.servletRequest.getParameterMap().keySet()) {
for (String value : this.servletRequest.getParameterValues(name)) {
this.queryParams.add(name, value);
}
}
}
return this.queryParams;
}
public InputStream getBody() throws IOException {
if (isFormPost(this.servletRequest)) {
return getBodyFromServletRequestParameters(this.servletRequest);

View File

@@ -22,6 +22,8 @@ import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.Cookie;
import org.springframework.http.Cookies;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
@@ -40,6 +42,8 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
private boolean headersWritten = false;
private final Cookies cookies = new Cookies();
/**
* Construct a new instance of the ServletServerHttpResponse based on the given {@link HttpServletResponse}.
@@ -66,12 +70,18 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
}
public Cookies getCookies() {
return (this.headersWritten ? Cookies.readOnlyCookies(this.cookies) : this.cookies);
}
public OutputStream getBody() throws IOException {
writeCookies();
writeHeaders();
return this.servletResponse.getOutputStream();
}
public void close() {
writeCookies();
writeHeaders();
}
@@ -95,4 +105,13 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
}
}
private void writeCookies() {
if (!this.headersWritten) {
for (Cookie source : this.cookies.getCookies()) {
javax.servlet.http.Cookie target = new javax.servlet.http.Cookie(source.getName(), source.getValue());
target.setPath("/");
this.servletResponse.addCookie(target);
}
}
}
}