Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for paths in websocket URL #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc, and individual contributors.
*
*
* 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.
Expand All @@ -17,6 +17,7 @@
package org.projectodd.stilts.stomp.client;

import java.net.InetSocketAddress;
import java.net.URI;

import javax.net.ssl.SSLContext;

Expand All @@ -26,26 +27,31 @@
import org.projectodd.stilts.stomp.protocol.StompFrame.Version;

class ClientContextImpl implements ClientContext {

ClientContextImpl(StompClient client) {
this.client = client;
}

@Override
public InetSocketAddress getServerAddress() {
return this.client.getServerAddress();
}

@Override
public URI getWebSocketAddress() {
return client.getWebSocketAddress();
}

@Override
public State getConnectionState() {
return this.client.getConnectionState();
}

@Override
public Version getVersion() {
return this.client.getVersion();
}

@Override
public boolean isSecure() {
return this.client.isSecure();
Expand All @@ -59,8 +65,8 @@ public void setConnectionState(State connectionState) {
@Override
public void setVersion(Version version) {
this.client.setVersion( version );
}
}

@Override
public void messageReceived(StompMessage message) {
this.client.messageReceived( message );
Expand All @@ -75,7 +81,7 @@ public void errorReceived(StompMessage message) {
public void receiptReceived(String receiptId) {
this.client.receiptReceived( receiptId );
}

@Override
public SSLContext getSSLContext() {
return this.client.getSSLContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc, and individual contributors.
*
*
* 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.
Expand Down Expand Up @@ -103,16 +103,21 @@ public StompClient(URI uri, SSLContext sslContext) throws URISyntaxException {
this.useSSL = true;
}
}

if ( port < 0 ) {
if ( useSSL ) {
port = Constants.DEFAULT_SECURE_PORT;
} else {
port = Constants.DEFAULT_PORT;
}

}
this.serverAddress = new InetSocketAddress( host, port );
if( useWebSockets ) {
this.webSocketAddress = new URI(this.useSSL ? "wss" : "ws" + "://" + host + ":" + port + uri.getPath());
} else {
this.webSocketAddress = null;
}
}

public boolean isSecure() {
Expand Down Expand Up @@ -459,7 +464,12 @@ protected ClientSocketChannelFactory createChannelFactory() {
return new NioClientSocketChannelFactory( bossExecutor, workerExecutor, 2 );
}

public URI getWebSocketAddress() {
return webSocketAddress;
}

private static final Callable<Void> NOOP = new Callable<Void>() {
@Override
public Void call() throws Exception {
return null;
}
Expand All @@ -485,6 +495,7 @@ public Void call() throws Exception {
private boolean destroyExecutor = false;
private Channel channel;
private InetSocketAddress serverAddress;
private final URI webSocketAddress;
private Version version = Version.VERSION_1_0;
private boolean useWebSockets = false;
private boolean useSSL = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc, and individual contributors.
*
*
* 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.
Expand Down Expand Up @@ -74,7 +74,7 @@ public ChannelPipeline getPipeline() throws Exception {
if (this.handshake != null) {
pipeline.addLast( "http-encoder", new HttpRequestEncoder() );
pipeline.addLast( "http-decoder", new WebSocketHttpResponseDecoder( this.handshake ) );
pipeline.addLast( "websocket-connection-negotiator", new WebSocketConnectionNegotiator( this.clientContext.getServerAddress(), this.handshake, this.clientContext.isSecure() ) );
pipeline.addLast( "websocket-connection-negotiator", new WebSocketConnectionNegotiator( this.clientContext.getWebSocketAddress(), this.handshake ) );
pipeline.addLast( "stomp-frame-decoder", new WebSocketStompFrameDecoder() );
pipeline.addLast( "stomp-frame-encoder", new WebSocketStompFrameEncoder() );
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc, and individual contributors.
*
*
* 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.
Expand All @@ -17,6 +17,7 @@
package org.projectodd.stilts.stomp.client.protocol;

import java.net.InetSocketAddress;
import java.net.URI;

import javax.net.ssl.SSLContext;

Expand All @@ -25,17 +26,19 @@
import org.projectodd.stilts.stomp.protocol.StompFrame.Version;

public interface ClientContext {

InetSocketAddress getServerAddress();
URI getWebSocketAddress();

State getConnectionState();
Version getVersion();

boolean isSecure();
SSLContext getSSLContext();

void setConnectionState(State state);
void setVersion(Version version);

void receiptReceived(String receiptId);
void messageReceived(StompMessage message);
void errorReceived(StompMessage message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.projectodd.stilts.stomp.client.protocol.websockets;

import java.net.InetSocketAddress;
import java.net.URI;
import java.security.NoSuchAlgorithmException;

Expand All @@ -20,27 +19,26 @@
import org.projectodd.stilts.stomp.protocol.websocket.WebSocketDisconnectionNegotiator;

/** WebSockets protocol connection negotiator.
*
*
* <p>This handler reacts to Netty's CONNECTED event and handles the handshake
* of the WebSockets HTTP upgrade handshake. Upon successful completion, it forwards
* a CONNECTED event upstream to the underlying protocol making use of the websocket
* transport. For instance, STOMP.</p>
*
*
* @author Bob McWhirter
*/
public class WebSocketConnectionNegotiator extends SimpleChannelUpstreamHandler {


public WebSocketConnectionNegotiator(InetSocketAddress serverAddress, Handshake handshake, boolean useSSL) throws NoSuchAlgorithmException {
this.serverAddress = serverAddress;

public WebSocketConnectionNegotiator(URI webSocketAddress, Handshake handshake) throws NoSuchAlgorithmException {
this.webSocketAddress = webSocketAddress;
this.handshake = handshake;
this.useSSL = useSSL;
}

@Override
public void channelConnected(ChannelHandlerContext context, ChannelStateEvent e) throws Exception {
URI uri = new URI( ( this.useSSL ? "wss" : "ws" ) + "://" + this.serverAddress.getHostName() + ":" + this.serverAddress.getPort() + "/" );
HttpRequest request = this.handshake.generateRequest( uri );

HttpRequest request = this.handshake.generateRequest( this.webSocketAddress );
this.connectedEvent = e;
Channel channel = context.getChannel();
Channels.write( channel, request );
Expand All @@ -50,7 +48,7 @@ public void channelConnected(ChannelHandlerContext context, ChannelStateEvent e)
public void messageReceived(ChannelHandlerContext context, MessageEvent e) throws Exception {
if (e.getMessage() instanceof HttpResponse) {
HttpResponse response = (HttpResponse) e.getMessage();

if ( this.handshake.isComplete( response) ) {
ChannelPipeline pipeline = context.getPipeline();
if (pipeline.get( WebSocketHttpResponseDecoder.class ) != null) {
Expand All @@ -63,15 +61,15 @@ public void messageReceived(ChannelHandlerContext context, MessageEvent e) throw
} else {
pipeline.addAfter( "websockets-decoder", "websockets-encoder", this.handshake.newEncoder() );
}

ChannelHandler[] additionalHandlers = this.handshake.newAdditionalHandlers();
String currentTail = "websockets-decoder";
for ( ChannelHandler each : additionalHandlers ) {
String handlerName = "additional-" + each.getClass().getSimpleName();
pipeline.addAfter( currentTail, handlerName, each);
pipeline.addAfter( currentTail, handlerName, each);
currentTail = handlerName;
}

context.sendUpstream( this.connectedEvent );
pipeline.replace( this, "websocket-disconnection-negotiator", new WebSocketDisconnectionNegotiator() );
}
Expand All @@ -81,8 +79,7 @@ public void messageReceived(ChannelHandlerContext context, MessageEvent e) throw
}

private static final Logger log = Logger.getLogger( "stomp.proto.client.websocket" );
private InetSocketAddress serverAddress;
private boolean useSSL;
private final URI webSocketAddress;
private Handshake handshake;
private ChannelStateEvent connectedEvent;

Expand Down
Loading