org.springframework.web.socket.WebSocketExtension Java Examples

The following examples show how to use org.springframework.web.socket.WebSocketExtension. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 7 votes vote down vote up
@Test
public void subProtocolCapableHandlerNoMatch() throws Exception {

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"});

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketProtocol("v10.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response,
			null, Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
 
Example #2
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test
public void supportedExtensions() {
	WebSocketExtension extension1 = new WebSocketExtension("ext1");
	WebSocketExtension extension2 = new WebSocketExtension("ext2");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Collections.singletonList(extension1));

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, null,
			Collections.singletonList(extension1), null, handler, attributes);
}
 
Example #3
Source File: StandardWebSocketSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeNativeSession(Session session) {
	super.initializeNativeSession(session);

	this.id = session.getId();
	this.uri = session.getRequestURI();

	this.acceptedProtocol = session.getNegotiatedSubprotocol();

	List<Extension> source = getNativeSession().getNegotiatedExtensions();
	this.extensions = new ArrayList<WebSocketExtension>(source.size());
	for (Extension ext : source) {
		this.extensions.add(new StandardToWebSocketExtensionAdapter(ext));
	}

	if (this.user == null) {
		this.user = session.getUserPrincipal();
	}
}
 
Example #4
Source File: DefaultHandshakeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void supportedExtensions() {
	WebSocketExtension extension1 = new WebSocketExtension("ext1");
	WebSocketExtension extension2 = new WebSocketExtension("ext2");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Collections.singletonList(extension1));

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, null,
			Collections.singletonList(extension1), null, handler, attributes);
}
 
Example #5
Source File: JettyWebSocketSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeNativeSession(Session session) {
	super.initializeNativeSession(session);

	this.id = ObjectUtils.getIdentityHexString(getNativeSession());
	this.uri = session.getUpgradeRequest().getRequestURI();

	this.headers = new HttpHeaders();
	this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
	this.headers = HttpHeaders.readOnlyHttpHeaders(headers);

	this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();

	List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
	this.extensions = new ArrayList<WebSocketExtension>(source.size());
	for (ExtensionConfig ec : source) {
		this.extensions.add(new WebSocketExtension(ec.getName(), ec.getParameters()));
	}

	if (this.user == null) {
		this.user = session.getUpgradeRequest().getUserPrincipal();
	}
}
 
Example #6
Source File: AbstractStandardUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
		String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user,
		WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {

	HttpHeaders headers = request.getHeaders();
	InetSocketAddress localAddr = request.getLocalAddress();
	InetSocketAddress remoteAddr = request.getRemoteAddress();

	StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
	StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);

	List<Extension> extensions = new ArrayList<Extension>();
	for (WebSocketExtension extension : selectedExtensions) {
		extensions.add(new WebSocketToStandardExtensionAdapter(extension));
	}

	upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
 
Example #7
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void supportedSubProtocols() throws Exception {

	this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketProtocol("STOMP");

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response,
			"STOMP", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
 
Example #8
Source File: JettyRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private List<WebSocketExtension> buildWebSocketExtensions() {
	Set<String> names = this.factory.getExtensionFactory().getExtensionNames();
	List<WebSocketExtension> result = new ArrayList<>(names.size());
	for (String name : names) {
		result.add(new WebSocketExtension(name));
	}
	return result;
}
 
Example #9
Source File: AbstractStandardUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected List<WebSocketExtension> getInstalledExtensions(WebSocketContainer container) {
	List<WebSocketExtension> result = new ArrayList<WebSocketExtension>();
	for (Extension ext : container.getInstalledExtensions()) {
		result.add(new StandardToWebSocketExtensionAdapter(ext));
	}
	return result;
}
 
Example #10
Source File: JettyWebSocketSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void initializeNativeSession(Session session) {
	super.initializeNativeSession(session);

	this.uri = session.getUpgradeRequest().getRequestURI();

	HttpHeaders headers = new HttpHeaders();
	headers.putAll(session.getUpgradeRequest().getHeaders());
	this.headers = HttpHeaders.readOnlyHttpHeaders(headers);

	this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();

	List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions();
	if (!CollectionUtils.isEmpty(jettyExtensions)) {
		List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size());
		for (ExtensionConfig jettyExtension : jettyExtensions) {
			extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters()));
		}
		this.extensions = Collections.unmodifiableList(extensions);
	}
	else {
		this.extensions = Collections.emptyList();
	}

	if (this.user == null) {
		this.user = session.getUpgradeRequest().getUserPrincipal();
	}
}
 
Example #11
Source File: AbstractHandshakeHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Filter the list of requested WebSocket extensions.
 * <p>As of 4.1, the default implementation of this method filters the list to
 * leave only extensions that are both requested and supported.
 * @param request the current request
 * @param requestedExtensions the list of extensions requested by the client
 * @param supportedExtensions the list of extensions supported by the server
 * @return the selected extensions or an empty list
 */
protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request,
		List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) {

	List<WebSocketExtension> result = new ArrayList<>(requestedExtensions.size());
	for (WebSocketExtension extension : requestedExtensions) {
		if (supportedExtensions.contains(extension)) {
			result.add(extension);
		}
	}
	return result;
}
 
Example #12
Source File: JettyRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private List<WebSocketExtension> getWebSocketExtensions() {
	List<WebSocketExtension> result = new ArrayList<WebSocketExtension>();
	for (String name : this.factory.getExtensionFactory().getExtensionNames()) {
		result.add(new WebSocketExtension(name));
	}
	return result;
}
 
Example #13
Source File: StandardWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler,
		HttpHeaders headers, final URI uri, List<String> protocols,
		List<WebSocketExtension> extensions, Map<String, Object> attributes) {

	int port = getPort(uri);
	InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
	InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);

	final StandardWebSocketSession session = new StandardWebSocketSession(headers,
			attributes, localAddress, remoteAddress);

	final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create()
			.configurator(new StandardWebSocketClientConfigurator(headers))
			.preferredSubprotocols(protocols)
			.extensions(adaptExtensions(extensions)).build();

	endpointConfig.getUserProperties().putAll(getUserProperties());

	final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session);

	Callable<WebSocketSession> connectTask = () -> {
		this.webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
		return session;
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
		task.run();
		return task;
	}
}
 
Example #14
Source File: AbstractWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
		@Nullable WebSocketHttpHeaders headers, URI uri) {

	Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
	assertUri(uri);

	if (logger.isDebugEnabled()) {
		logger.debug("Connecting to " + uri);
	}

	HttpHeaders headersToUse = new HttpHeaders();
	if (headers != null) {
		headers.forEach((header, values) -> {
			if (values != null && !specialHeaders.contains(header.toLowerCase())) {
				headersToUse.put(header, values);
			}
		});
	}

	List<String> subProtocols =
			(headers != null ? headers.getSecWebSocketProtocol() : Collections.emptyList());
	List<WebSocketExtension> extensions =
			(headers != null ? headers.getSecWebSocketExtensions() : Collections.emptyList());

	return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions,
			Collections.emptyMap());
}
 
Example #15
Source File: JettyWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler,
		HttpHeaders headers, final URI uri, List<String> protocols,
		List<WebSocketExtension> extensions,  Map<String, Object> attributes) {

	final ClientUpgradeRequest request = new ClientUpgradeRequest();
	request.setSubProtocols(protocols);

	for (WebSocketExtension e : extensions) {
		request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
	}

	headers.forEach(request::setHeader);

	Principal user = getUser();
	final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
	final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);

	Callable<WebSocketSession> connectTask = () -> {
		Future<Session> future = this.client.connect(listener, uri, request);
		future.get();
		return wsSession;
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
		task.run();
		return task;
	}
}
 
Example #16
Source File: AbstractTyrusRequestUpgradeStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected List<WebSocketExtension> getInstalledExtensions(WebSocketContainer container) {
	try {
		return super.getInstalledExtensions(container);
	}
	catch (UnsupportedOperationException ex) {
		return new ArrayList<>(0);
	}
}
 
Example #17
Source File: JettyRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public List<WebSocketExtension> getSupportedExtensions(ServerHttpRequest request) {
	if (this.supportedExtensions == null) {
		this.supportedExtensions = buildWebSocketExtensions();
	}
	return this.supportedExtensions;
}
 
Example #18
Source File: JettyRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public WebSocketHandlerContainer(JettyWebSocketHandlerAdapter handler, String protocol, List<WebSocketExtension> extensions) {
	this.handler = handler;
	this.selectedProtocol = protocol;
	if (CollectionUtils.isEmpty(extensions)) {
		this.extensionConfigs = null;
	}
	else {
		this.extensionConfigs = new ArrayList<ExtensionConfig>();
		for (WebSocketExtension e : extensions) {
			this.extensionConfigs.add(new WebSocketToJettyExtensionConfigAdapter(e));
		}
	}
}
 
Example #19
Source File: StandardWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static List<Extension> adaptExtensions(List<WebSocketExtension> extensions) {
	List<Extension> result = new ArrayList<>();
	for (WebSocketExtension extension : extensions) {
		result.add(new WebSocketToStandardExtensionAdapter(extension));
	}
	return result;
}
 
Example #20
Source File: JettyRequestUpgradeStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
private List<WebSocketExtension> buildWebSocketExtensions() {
	Set<String> names = this.factory.getExtensionFactory().getExtensionNames();
	List<WebSocketExtension> result = new ArrayList<>(names.size());
	for (String name : names) {
		result.add(new WebSocketExtension(name));
	}
	return result;
}
 
Example #21
Source File: JettyRequestUpgradeStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
		String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user,
		WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {

	Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
	HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();

	Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
	HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();

	Assert.isTrue(this.factory.isUpgradeRequest(servletRequest, servletResponse), "Not a WebSocket handshake");

	JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
	JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(wsHandler, session);

	WebSocketHandlerContainer container =
			new WebSocketHandlerContainer(handlerAdapter, selectedProtocol, selectedExtensions);

	try {
		containerHolder.set(container);
		this.factory.acceptWebSocket(servletRequest, servletResponse);
	}
	catch (IOException ex) {
		throw new HandshakeFailureException(
				"Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
	}
	finally {
		containerHolder.remove();
	}
}
 
Example #22
Source File: JettyRequestUpgradeStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
public WebSocketHandlerContainer(
		JettyWebSocketHandlerAdapter handler, String protocol, List<WebSocketExtension> extensions) {

	this.handler = handler;
	this.selectedProtocol = protocol;
	if (CollectionUtils.isEmpty(extensions)) {
		this.extensionConfigs = new ArrayList<>(0);
	}
	else {
		this.extensionConfigs = new ArrayList<>(extensions.size());
		for (WebSocketExtension extension : extensions) {
			this.extensionConfigs.add(new WebSocketToJettyExtensionConfigAdapter(extension));
		}
	}
}
 
Example #23
Source File: WebSocketHttpHeadersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void parseWebSocketExtensions() {
	List<String> extensions = new ArrayList<>();
	extensions.add("x-foo-extension, x-bar-extension");
	extensions.add("x-test-extension");
	this.headers.put(WebSocketHttpHeaders.SEC_WEBSOCKET_EXTENSIONS, extensions);

	List<WebSocketExtension> parsedExtensions = this.headers.getSecWebSocketExtensions();
	assertThat(parsedExtensions, Matchers.hasSize(3));
}
 
Example #24
Source File: WebSocketToStandardExtensionAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public WebSocketToStandardExtensionAdapter(final WebSocketExtension extension) {
	this.name = extension.getName();
	for (final String paramName : extension.getParameters().keySet()) {
		this.parameters.add(new Parameter() {
			@Override
			public String getName() {
				return paramName;
			}
			@Override
			public String getValue() {
				return extension.getParameters().get(paramName);
			}
		});
	}
}
 
Example #25
Source File: StandardWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler,
		HttpHeaders headers, final URI uri, List<String> protocols,
		List<WebSocketExtension> extensions, Map<String, Object> attributes) {

	int port = getPort(uri);
	InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
	InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);

	final StandardWebSocketSession session = new StandardWebSocketSession(headers,
			attributes, localAddress, remoteAddress);

	final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create()
			.configurator(new StandardWebSocketClientConfigurator(headers))
			.preferredSubprotocols(protocols)
			.extensions(adaptExtensions(extensions)).build();

	endpointConfig.getUserProperties().putAll(getUserProperties());

	final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session);

	Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {
		@Override
		public WebSocketSession call() throws Exception {
			webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
			return session;
		}
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<WebSocketSession>(connectTask);
		task.run();
		return task;
	}
}
 
Example #26
Source File: StandardWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static List<Extension> adaptExtensions(List<WebSocketExtension> extensions) {
	List<Extension> result = new ArrayList<Extension>();
	for (WebSocketExtension extension : extensions) {
		result.add(new WebSocketToStandardExtensionAdapter(extension));
	}
	return result;
}
 
Example #27
Source File: AbstractWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
		WebSocketHttpHeaders headers, URI uri) {

	Assert.notNull(webSocketHandler, "webSocketHandler must not be null");
	assertUri(uri);

	if (logger.isDebugEnabled()) {
		logger.debug("Connecting to " + uri);
	}

	HttpHeaders headersToUse = new HttpHeaders();
	if (headers != null) {
		for (String header : headers.keySet()) {
			if (!specialHeaders.contains(header.toLowerCase())) {
				headersToUse.put(header, headers.get(header));
			}
		}
	}

	List<String> subProtocols = ((headers != null) && (headers.getSecWebSocketProtocol() != null)) ?
			headers.getSecWebSocketProtocol() : Collections.<String>emptyList();

	List<WebSocketExtension> extensions = ((headers != null) && (headers.getSecWebSocketExtensions() != null)) ?
			headers.getSecWebSocketExtensions() : Collections.<WebSocketExtension>emptyList();

	return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions,
			Collections.<String, Object>emptyMap());
}
 
Example #28
Source File: JettyWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler,
		HttpHeaders headers, final URI uri, List<String> protocols,
		List<WebSocketExtension> extensions,  Map<String, Object> attributes) {

	final ClientUpgradeRequest request = new ClientUpgradeRequest();
	request.setSubProtocols(protocols);

	for (WebSocketExtension e : extensions) {
		request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
	}

	for (String header : headers.keySet()) {
		request.setHeader(header, headers.get(header));
	}

	Principal user = getUser();
	final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
	final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);

	Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {
		@Override
		public WebSocketSession call() throws Exception {
			Future<Session> future = client.connect(listener, uri, request);
			future.get();
			return wsSession;
		}
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<WebSocketSession>(connectTask);
		task.run();
		return task;
	}
}
 
Example #29
Source File: AbstractTyrusRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected List<WebSocketExtension> getInstalledExtensions(WebSocketContainer container) {
	try {
		return super.getInstalledExtensions(container);
	}
	catch (UnsupportedOperationException ex) {
		return new ArrayList<WebSocketExtension>(0);
	}
}
 
Example #30
Source File: AbstractStandardUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public List<WebSocketExtension> getSupportedExtensions(ServerHttpRequest request) {
	if (this.extensions == null) {
		HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
		this.extensions = getInstalledExtensions(getContainer(servletRequest));
	}
	return this.extensions;
}