|
|
|
@ -16,6 +16,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.socket.sockjs.client; |
|
|
|
package org.springframework.web.socket.sockjs.client; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.task.SimpleAsyncTaskExecutor; |
|
|
|
import org.springframework.core.task.SimpleAsyncTaskExecutor; |
|
|
|
import org.springframework.core.task.TaskExecutor; |
|
|
|
import org.springframework.core.task.TaskExecutor; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
@ -38,11 +43,6 @@ import org.springframework.web.socket.WebSocketHandler; |
|
|
|
import org.springframework.web.socket.WebSocketSession; |
|
|
|
import org.springframework.web.socket.WebSocketSession; |
|
|
|
import org.springframework.web.socket.sockjs.frame.SockJsFrame; |
|
|
|
import org.springframework.web.socket.sockjs.frame.SockJsFrame; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An {@code XhrTransport} implementation that uses a |
|
|
|
* An {@code XhrTransport} implementation that uses a |
|
|
|
* {@link org.springframework.web.client.RestTemplate RestTemplate}. |
|
|
|
* {@link org.springframework.web.client.RestTemplate RestTemplate}. |
|
|
|
@ -76,12 +76,9 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Configure the {@code TaskExecutor} to use to execute XHR receive requests. |
|
|
|
* Configure the {@code TaskExecutor} to use to execute XHR receive requests. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor |
|
|
|
* <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor |
|
|
|
* SimpleAsyncTaskExecutor} is configured which creates a new thread every |
|
|
|
* SimpleAsyncTaskExecutor} is configured which creates a new thread every |
|
|
|
* time the transports connects. |
|
|
|
* time the transports connects. |
|
|
|
* |
|
|
|
|
|
|
|
* @param taskExecutor the task executor, cannot be {@code null} |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setTaskExecutor(TaskExecutor taskExecutor) { |
|
|
|
public void setTaskExecutor(TaskExecutor taskExecutor) { |
|
|
|
Assert.notNull(this.taskExecutor); |
|
|
|
Assert.notNull(this.taskExecutor); |
|
|
|
@ -147,6 +144,24 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* A simple ResponseExtractor that reads the body into a String. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private final static ResponseExtractor<ResponseEntity<String>> textExtractor = |
|
|
|
|
|
|
|
new ResponseExtractor<ResponseEntity<String>>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException { |
|
|
|
|
|
|
|
if (response.getBody() == null) { |
|
|
|
|
|
|
|
return new ResponseEntity<String>(response.getHeaders(), response.getStatusCode()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET); |
|
|
|
|
|
|
|
return new ResponseEntity<String>(body, response.getHeaders(), response.getStatusCode()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A RequestCallback to add the headers and (optionally) String content. |
|
|
|
* A RequestCallback to add the headers and (optionally) String content. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -156,7 +171,6 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
|
|
|
|
|
|
|
|
private final String body; |
|
|
|
private final String body; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public XhrRequestCallback(HttpHeaders headers) { |
|
|
|
public XhrRequestCallback(HttpHeaders headers) { |
|
|
|
this(headers, null); |
|
|
|
this(headers, null); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -166,7 +180,6 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
this.body = body; |
|
|
|
this.body = body; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doWithRequest(ClientHttpRequest request) throws IOException { |
|
|
|
public void doWithRequest(ClientHttpRequest request) throws IOException { |
|
|
|
if (this.headers != null) { |
|
|
|
if (this.headers != null) { |
|
|
|
@ -178,23 +191,6 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* A simple ResponseExtractor that reads the body into a String. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private final static ResponseExtractor<ResponseEntity<String>> textExtractor = |
|
|
|
|
|
|
|
new ResponseExtractor<ResponseEntity<String>>() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException { |
|
|
|
|
|
|
|
if (response.getBody() == null) { |
|
|
|
|
|
|
|
return new ResponseEntity<String>(response.getHeaders(), response.getStatusCode()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET); |
|
|
|
|
|
|
|
return new ResponseEntity<String>(body, response.getHeaders(), response.getStatusCode()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Splits the body of an HTTP response into SockJS frames and delegates those |
|
|
|
* Splits the body of an HTTP response into SockJS frames and delegates those |
|
|
|
@ -204,12 +200,10 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
|
|
|
|
|
|
|
|
private final XhrClientSockJsSession sockJsSession; |
|
|
|
private final XhrClientSockJsSession sockJsSession; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) { |
|
|
|
public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) { |
|
|
|
this.sockJsSession = sockJsSession; |
|
|
|
this.sockJsSession = sockJsSession; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Object extractData(ClientHttpResponse response) throws IOException { |
|
|
|
public Object extractData(ClientHttpResponse response) throws IOException { |
|
|
|
if (!HttpStatus.OK.equals(response.getStatusCode())) { |
|
|
|
if (!HttpStatus.OK.equals(response.getStatusCode())) { |
|
|
|
@ -262,4 +256,3 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|