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

166 lines
4.9 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
import io.netty.channel.Channel;
2012-01-19 05:12:45 +01:00
import io.netty.channel.ChannelFuture;
2011-12-09 04:38:59 +01:00
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.internal.StringUtil;
2011-09-26 14:51:15 +02:00
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
2011-09-26 14:51:15 +02:00
/**
* Base class for server side web socket opening and closing handshakes
*/
public abstract class WebSocketServerHandshaker {
2012-01-19 05:12:45 +01:00
private final String webSocketUrl;
2012-01-19 05:12:45 +01:00
private final String[] subprotocols;
2012-01-19 05:12:45 +01:00
private final WebSocketVersion version;
private final int maxFramePayloadLength;
2012-05-31 04:09:23 +02:00
private String selectedSubprotocol;
/**
* Constructor specifying the destination web socket location
2012-02-21 03:06:26 +01:00
*
2012-01-19 05:12:45 +01:00
* @param version
* the protocol version
* @param webSocketUrl
* URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL.
2012-01-19 05:12:45 +01:00
* @param subprotocols
* CSV of supported protocols. Null if sub protocols not supported.
* @param maxFramePayloadLength
* Maximum length of a frame's payload
*/
2012-01-19 05:12:45 +01:00
protected WebSocketServerHandshaker(
WebSocketVersion version, String webSocketUrl, String subprotocols,
int maxFramePayloadLength) {
2012-01-19 05:12:45 +01:00
this.version = version;
this.webSocketUrl = webSocketUrl;
if (subprotocols != null) {
String[] subprotocolArray = StringUtil.split(subprotocols, ',');
2012-01-19 05:12:45 +01:00
for (int i = 0; i < subprotocolArray.length; i++) {
subprotocolArray[i] = subprotocolArray[i].trim();
}
2012-01-19 05:12:45 +01:00
this.subprotocols = subprotocolArray;
} else {
this.subprotocols = new String[0];
}
this.maxFramePayloadLength = maxFramePayloadLength;
}
/**
* Returns the URL of the web socket
*/
2012-01-19 05:12:45 +01:00
public String getWebSocketUrl() {
return webSocketUrl;
}
/**
* Returns the CSV of supported sub protocols
*/
2012-01-19 05:12:45 +01:00
public Set<String> getSubprotocols() {
Set<String> ret = new LinkedHashSet<String>();
Collections.addAll(ret, subprotocols);
2012-01-19 05:12:45 +01:00
return ret;
}
/**
* Returns the version of the specification being supported
*/
public WebSocketVersion getVersion() {
return version;
}
/**
* Gets the maximum length for any frame's payload.
*
* @return The maximum length for a frame's payload
*/
public int getMaxFramePayloadLength() {
return maxFramePayloadLength;
}
/**
* Performs the opening handshake
2012-02-21 03:06:26 +01:00
*
* @param channel
* Channel
* @param req
* HTTP Request
*/
2012-01-19 05:12:45 +01:00
public abstract ChannelFuture handshake(Channel channel, HttpRequest req);
/**
* Performs the closing handshake
2012-02-21 03:06:26 +01:00
*
* @param channel
* Channel
* @param frame
* Closing Frame that was received
*/
2012-01-19 05:12:45 +01:00
public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame);
/**
* Selects the first matching supported sub protocol
2012-02-21 03:06:26 +01:00
*
2012-01-19 05:12:45 +01:00
* @param requestedSubprotocols
* CSV of protocols to be supported. e.g. "chat, superchat"
* @return First matching supported sub protocol. Null if not found.
*/
2012-01-19 05:12:45 +01:00
protected String selectSubprotocol(String requestedSubprotocols) {
if (requestedSubprotocols == null || subprotocols.length == 0) {
return null;
}
String[] requestedSubprotocolArray = StringUtil.split(requestedSubprotocols, ',');
for (String p: requestedSubprotocolArray) {
2012-01-19 05:12:45 +01:00
String requestedSubprotocol = p.trim();
2012-01-19 05:12:45 +01:00
for (String supportedSubprotocol: subprotocols) {
if (requestedSubprotocol.equals(supportedSubprotocol)) {
return requestedSubprotocol;
}
}
}
// No match found
return null;
}
/**
* Returns the selected subprotocol. Null if no subprotocol has been selected.
* <p>
* This is only available AFTER <tt>handshake()</tt> has been called.
* </p>
*/
public String getSelectedSubprotocol() {
return selectedSubprotocol;
}
protected void setSelectedSubprotocol(String value) {
selectedSubprotocol = value;
}
2011-09-26 14:51:15 +02:00
}