use of org.asynchttpclient.ws.WebSocketUpgradeHandler in project async-http-client by AsyncHttpClient.
the class WebSocketHandler method handleException.
@Override
public void handleException(NettyResponseFuture<?> future, Throwable e) {
logger.warn("onError", e);
try {
WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
NettyWebSocket webSocket = h.onCompleted();
if (webSocket != null) {
webSocket.onError(e.getCause());
webSocket.close();
}
} catch (Throwable t) {
logger.error("onError", t);
}
}
use of org.asynchttpclient.ws.WebSocketUpgradeHandler in project async-http-client by AsyncHttpClient.
the class NettyRequestSender method validateWebSocketRequest.
private void validateWebSocketRequest(Request request, AsyncHandler<?> asyncHandler) {
Uri uri = request.getUri();
boolean isWs = uri.isWebSocket();
if (asyncHandler instanceof WebSocketUpgradeHandler) {
if (!isWs)
throw new IllegalArgumentException("WebSocketUpgradeHandler but scheme isn't ws or wss: " + uri.getScheme());
else if (!request.getMethod().equals(GET) && !request.getMethod().equals(CONNECT))
throw new IllegalArgumentException("WebSocketUpgradeHandler but method isn't GET or CONNECT: " + request.getMethod());
} else if (isWs) {
throw new IllegalArgumentException("No WebSocketUpgradeHandler but scheme is " + uri.getScheme());
}
}
use of org.asynchttpclient.ws.WebSocketUpgradeHandler in project async-http-client by AsyncHttpClient.
the class WebSocketHandler method handleRead.
@Override
public void handleRead(Channel channel, NettyResponseFuture<?> future, Object e) throws Exception {
if (e instanceof HttpResponse) {
HttpResponse response = (HttpResponse) e;
if (logger.isDebugEnabled()) {
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
}
WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
switch(handler.onStatusReceived(status)) {
case CONTINUE:
upgrade(channel, future, handler, response, responseHeaders);
break;
default:
abort(channel, future, handler, status);
}
}
} else if (e instanceof WebSocketFrame) {
final WebSocketFrame frame = (WebSocketFrame) e;
WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
NettyWebSocket webSocket = handler.onCompleted();
// retain because we might buffer the frame
if (webSocket.isReady()) {
webSocket.handleFrame(frame);
} else {
// WebSocket hasn't been open yet, but upgrading the pipeline triggered a read and a frame was sent along the HTTP upgrade response
// as we want to keep sequential order (but can't notify user of open before upgrading so he doesn't to try send immediately), we have to buffer
webSocket.bufferFrame(frame);
}
} else if (!(e instanceof LastHttpContent)) {
// ignore, end of handshake response
logger.error("Invalid message {}", e);
}
}
use of org.asynchttpclient.ws.WebSocketUpgradeHandler in project async-http-client by AsyncHttpClient.
the class WebSocketHandler method handleChannelInactive.
@Override
public void handleChannelInactive(NettyResponseFuture<?> future) {
logger.trace("onClose");
try {
WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
NettyWebSocket webSocket = h.onCompleted();
logger.trace("Connection was closed abnormally (that is, with no close frame being received).");
if (webSocket != null)
webSocket.close(1006, "Connection was closed abnormally (that is, with no close frame being received).");
} catch (Throwable t) {
logger.error("onError", t);
}
}
Aggregations