View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.sockjs.client;
18  
19  import java.net.URI;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.springframework.http.HttpHeaders;
27  import org.springframework.http.HttpStatus;
28  import org.springframework.http.MediaType;
29  import org.springframework.http.ResponseEntity;
30  import org.springframework.util.concurrent.ListenableFuture;
31  import org.springframework.util.concurrent.SettableListenableFuture;
32  import org.springframework.web.client.HttpServerErrorException;
33  import org.springframework.web.socket.TextMessage;
34  import org.springframework.web.socket.WebSocketHandler;
35  import org.springframework.web.socket.WebSocketSession;
36  import org.springframework.web.socket.sockjs.frame.SockJsFrame;
37  import org.springframework.web.socket.sockjs.transport.TransportType;
38  
39  /**
40   * Abstract base class for XHR transport implementations to extend.
41   *
42   * @author Rossen Stoyanchev
43   * @since 4.1
44   */
45  public abstract class AbstractXhrTransport implements XhrTransport {
46  
47  	protected static final String PRELUDE;
48  
49  	static {
50  		byte[] bytes = new byte[2048];
51  		for (int i = 0; i < bytes.length; i++) {
52  			bytes[i] = 'h';
53  		}
54  		PRELUDE = new String(bytes, SockJsFrame.CHARSET);
55  	}
56  
57  	protected Log logger = LogFactory.getLog(getClass());
58  
59  	private boolean xhrStreamingDisabled;
60  
61  	private HttpHeaders requestHeaders = new HttpHeaders();
62  
63  	private HttpHeaders xhrSendRequestHeaders = new HttpHeaders();
64  
65  
66  	@Override
67  	public List<TransportType> getTransportTypes() {
68  		return (isXhrStreamingDisabled() ?
69  				Arrays.asList(TransportType.XHR) :
70  				Arrays.asList(TransportType.XHR_STREAMING, TransportType.XHR));
71  	}
72  
73  	/**
74  	 * An {@code XhrTransport} can support both the "xhr_streaming" and "xhr"
75  	 * SockJS server transports. From a client perspective there is no
76  	 * implementation difference.
77  	 *
78  	 * <p>Typically an {@code XhrTransport} is used as "XHR streaming" first and
79  	 * then, if that fails, as "XHR". In some cases however it may be helpful to
80  	 * suppress XHR streaming so that only XHR is attempted.
81  	 *
82  	 * <p>By default this property is set to {@code false} which means both
83  	 * "XHR streaming" and "XHR" apply.
84  	 */
85  	public void setXhrStreamingDisabled(boolean disabled) {
86  		this.xhrStreamingDisabled = disabled;
87  	}
88  
89  	/**
90  	 * Whether XHR streaming is disabled or not.
91  	 */
92  	public boolean isXhrStreamingDisabled() {
93  		return this.xhrStreamingDisabled;
94  	}
95  
96  	/**
97  	 * Configure headers to be added to every executed HTTP request.
98  	 * @param requestHeaders the headers to add to requests
99  	 */
100 	public void setRequestHeaders(HttpHeaders requestHeaders) {
101 		this.requestHeaders.clear();
102 		this.xhrSendRequestHeaders.clear();
103 		if (requestHeaders != null) {
104 			this.requestHeaders.putAll(requestHeaders);
105 			this.xhrSendRequestHeaders.putAll(requestHeaders);
106 			this.xhrSendRequestHeaders.setContentType(MediaType.APPLICATION_JSON);
107 		}
108 	}
109 
110 	public HttpHeaders getRequestHeaders() {
111 		return this.requestHeaders;
112 	}
113 
114 	@Override
115 	public String executeInfoRequest(URI infoUrl) {
116 		if (logger.isDebugEnabled()) {
117 			logger.debug("Executing SockJS Info request, url=" + infoUrl);
118 		}
119 		ResponseEntity<String> response = executeInfoRequestInternal(infoUrl);
120 		if (response.getStatusCode() != HttpStatus.OK) {
121 			if (logger.isErrorEnabled()) {
122 				logger.error("SockJS Info request (url=" + infoUrl + ") failed: " + response);
123 			}
124 			throw new HttpServerErrorException(response.getStatusCode());
125 		}
126 		if (logger.isTraceEnabled()) {
127 			logger.trace("SockJS Info request (url=" + infoUrl + ") response: " + response);
128 		}
129 		return response.getBody();
130 	}
131 
132 	protected abstract ResponseEntity<String> executeInfoRequestInternal(URI infoUrl);
133 
134 	@Override
135 	public void executeSendRequest(URI url, TextMessage message) {
136 		if (logger.isTraceEnabled()) {
137 			logger.trace("Starting XHR send, url=" + url);
138 		}
139 		ResponseEntity<String> response = executeSendRequestInternal(url, this.xhrSendRequestHeaders, message);
140 		if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
141 			if (logger.isErrorEnabled()) {
142 				logger.error("XHR send request (url=" + url + ") failed: " + response);
143 			}
144 			throw new HttpServerErrorException(response.getStatusCode());
145 		}
146 		if (logger.isTraceEnabled()) {
147 			logger.trace("XHR send request (url=" + url + ") response: " + response);
148 		}
149 	}
150 
151 	protected abstract ResponseEntity<String> executeSendRequestInternal(URI url, HttpHeaders headers, TextMessage message);
152 
153 	@Override
154 	public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
155 		SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<WebSocketSession>();
156 		XhrClientSockJsSession session = new XhrClientSockJsSession(request, handler, this, connectFuture);
157 		request.addTimeoutTask(session.getTimeoutTask());
158 
159 		URI receiveUrl = request.getTransportUrl();
160 		if (logger.isDebugEnabled()) {
161 			logger.debug("Starting XHR " +
162 					(isXhrStreamingDisabled() ? "Polling" : "Streaming") + "session url=" + receiveUrl);
163 		}
164 
165 		HttpHeaders handshakeHeaders = new HttpHeaders();
166 		handshakeHeaders.putAll(request.getHandshakeHeaders());
167 		handshakeHeaders.putAll(getRequestHeaders());
168 
169 		connectInternal(request, handler, receiveUrl, handshakeHeaders, session, connectFuture);
170 		return connectFuture;
171 	}
172 
173 	protected abstract void connectInternal(TransportRequest request, WebSocketHandler handler,
174 			URI receiveUrl, HttpHeaders handshakeHeaders, XhrClientSockJsSession session,
175 			SettableListenableFuture<WebSocketSession> connectFuture);
176 
177 
178 	@Override
179 	public String toString() {
180 		return getClass().getSimpleName();
181 	}
182 
183 }