private ClientUpgradeInfo processUpgradeResponse(UpgradeResponse upgradeResponse,
final Writer writer,
final Connection.CloseListener closeListener) throws HandshakeException {
clientHandShake.validateServerResponse(upgradeResponse);
final TyrusWebSocket socket = new TyrusWebSocket(protocolHandler, endpointWrapper);
final List<Extension> handshakeResponseExtensions = TyrusExtension.fromHeaders(upgradeResponse.getHeaders().get(HandshakeRequest.SEC_WEBSOCKET_EXTENSIONS));
final List<Extension> extensions = new ArrayList<Extension>();
final ExtendedExtension.ExtensionContext extensionContext = new ExtendedExtension.ExtensionContext() {
private final Map<String, Object> properties = new HashMap<String, Object>();
@Override
public Map<String, Object> getProperties() {
return properties;
}
};
for (Extension responseExtension : handshakeResponseExtensions) {
for (Extension installedExtension : ((ClientEndpointConfig) endpointWrapper.getEndpointConfig()).getExtensions()) {
if (responseExtension.getName() != null && responseExtension.getName().equals(installedExtension.getName())) {
if (installedExtension instanceof ExtendedExtension) {
((ExtendedExtension) installedExtension).onHandshakeResponse(extensionContext, responseExtension.getParameters());
}
extensions.add(installedExtension);
}
}
}
final Session sessionForRemoteEndpoint = endpointWrapper.createSessionForRemoteEndpoint(
socket,
upgradeResponse.getFirstHeaderValue(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL),
extensions);
((ClientEndpointConfig) endpointWrapper.getEndpointConfig()).getConfigurator().afterResponse(upgradeResponse);
protocolHandler.setWriter(writer);
protocolHandler.setWebSocket(socket);
protocolHandler.setExtensions(extensions);
protocolHandler.setExtensionContext(extensionContext);
// subprotocol and extensions are already set -- TODO: introduce new method (onClientConnect)?
socket.onConnect(this.clientHandShake.getRequest(), null, null, null);
listener.onSessionCreated(sessionForRemoteEndpoint);
// incoming buffer size - max frame size possible to receive.
Integer tyrusIncomingBufferSize = Utils.getProperty(properties, ClientProperties.INCOMING_BUFFER_SIZE, Integer.class);
Integer wlsIncomingBufferSize = Utils.getProperty(endpointWrapper.getEndpointConfig().getUserProperties(), ClientContainer.WLS_INCOMING_BUFFER_SIZE, Integer.class);
final Integer incomingBufferSize;
if (tyrusIncomingBufferSize == null && wlsIncomingBufferSize == null) {
incomingBufferSize = DEFAULT_INCOMING_BUFFER_SIZE;
} else if (wlsIncomingBufferSize != null) {
incomingBufferSize = wlsIncomingBufferSize;
} else {
incomingBufferSize = tyrusIncomingBufferSize;
}
return new ClientUpgradeInfo() {
@Override
public ClientUpgradeStatus getUpgradeStatus() {
return ClientUpgradeStatus.SUCCESS;
}
@Override
public UpgradeRequest getUpgradeRequest() {
return null;
}
@Override
public Connection createConnection() {
return new Connection() {
private final ReadHandler readHandler = new TyrusReadHandler(protocolHandler, socket,
incomingBufferSize,
sessionForRemoteEndpoint.getNegotiatedExtensions(),
extensionContext);
@Override
public ReadHandler getReadHandler() {
return readHandler;
}
@Override
public Writer getWriter() {
return writer;
}
@Override
public CloseListener getCloseListener() {
return closeListener;
}
@Override
public void close(CloseReason reason) {
try {
writer.close();
} catch (IOException e) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, e.getMessage(), e);
}
socket.close(reason.getCloseCode().getCode(), reason.getReasonPhrase());
for (Extension extension : sessionForRemoteEndpoint.getNegotiatedExtensions()) {
if (extension instanceof ExtendedExtension) {
((ExtendedExtension) extension).destroy(extensionContext);
}