netty5/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java

153 lines
4.6 KiB
Java
Raw Normal View History

2011-09-26 14:51:15 +02:00
/*
2012-06-04 22:31:44 +02:00
* Copyright 2012 The Netty Project
2011-09-26 14:51:15 +02:00
*
* The Netty Project licenses this file to you 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:
2011-09-26 14:51:15 +02:00
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-09-26 14:51:15 +02:00
*
* 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
2011-09-26 14:51:15 +02:00
* License for the specific language governing permissions and limitations
* under the License.
*/
2011-12-09 04:38:59 +01:00
package io.netty.handler.codec.http.websocketx;
2011-09-26 14:51:15 +02:00
2011-12-09 04:38:59 +01:00
import io.netty.channel.Channel;
2012-01-19 05:12:45 +01:00
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
2011-09-26 14:51:15 +02:00
import java.net.URI;
2011-09-26 14:51:15 +02:00
/**
* Base class for web socket client handshake implementations
*/
public abstract class WebSocketClientHandshaker {
2012-01-19 05:12:45 +01:00
private final URI webSocketUrl;
2012-01-19 05:12:45 +01:00
private final WebSocketVersion version;
2012-01-19 05:12:45 +01:00
private boolean handshakeComplete;
2012-01-19 05:12:45 +01:00
private final String expectedSubprotocol;
2012-01-19 05:12:45 +01:00
private String actualSubprotocol;
protected final HttpHeaders customHeaders;
private final int maxFramePayloadLength;
2012-01-19 05:12:45 +01:00
/**
* Base constructor
*
* @param webSocketUrl
* URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL.
* @param version
* Version of web socket specification to use to connect to the server
* @param subprotocol
* Sub protocol request sent to the server.
* @param customHeaders
* Map of custom headers to add to the client request
* @param maxFramePayloadLength
* Maximum length of a frame's payload
2012-01-19 05:12:45 +01:00
*/
protected WebSocketClientHandshaker(URI webSocketUrl, WebSocketVersion version, String subprotocol,
HttpHeaders customHeaders, int maxFramePayloadLength) {
2012-01-19 05:12:45 +01:00
this.webSocketUrl = webSocketUrl;
this.version = version;
2012-01-19 05:12:45 +01:00
expectedSubprotocol = subprotocol;
this.customHeaders = customHeaders;
this.maxFramePayloadLength = maxFramePayloadLength;
}
/**
* Returns the URI to the web socket. e.g. "ws://myhost.com/path"
*/
2012-01-19 05:12:45 +01:00
public URI getWebSocketUrl() {
return webSocketUrl;
}
/**
* Version of the web socket specification that is being used
*/
public WebSocketVersion getVersion() {
return version;
}
/**
* Returns the max length for any frame's payload
*/
public int getMaxFramePayloadLength() {
return maxFramePayloadLength;
}
/**
* Flag to indicate if the opening handshake is complete
*/
2012-01-19 05:12:45 +01:00
public boolean isHandshakeComplete() {
return handshakeComplete;
}
2012-01-19 05:12:45 +01:00
protected void setHandshakeComplete() {
handshakeComplete = true;
}
/**
* Returns the CSV of requested subprotocol(s) sent to the server as specified in the constructor
*/
2012-01-19 05:12:45 +01:00
public String getExpectedSubprotocol() {
return expectedSubprotocol;
}
/**
* Returns the subprotocol response sent by the server. Only available after end of handshake.
* Null if no subprotocol was requested or confirmed by the server.
*/
2012-01-19 05:12:45 +01:00
public String getActualSubprotocol() {
return actualSubprotocol;
}
2012-01-19 05:12:45 +01:00
protected void setActualSubprotocol(String actualSubprotocol) {
this.actualSubprotocol = actualSubprotocol;
}
/**
2012-01-19 05:12:45 +01:00
* Begins the opening handshake
2012-02-21 03:06:26 +01:00
*
* @param channel
* Channel
*/
public ChannelFuture handshake(Channel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
return handshake(channel, channel.newPromise());
}
/**
* Begins the opening handshake
*
* @param channel
* Channel
* @param promise
* the {@link ChannelPromise} to be notified when the opening handshake is sent
*/
public abstract ChannelFuture handshake(Channel channel, ChannelPromise promise);
/**
2012-01-19 05:12:45 +01:00
* Validates and finishes the opening handshake initiated by {@link #handshake}}.
2012-02-21 03:06:26 +01:00
*
* @param channel
* Channel
* @param response
* HTTP response containing the closing handshake details
*/
public abstract void finishHandshake(Channel channel, FullHttpResponse response);
2011-09-26 14:51:15 +02:00
}