use of io.netty.handler.codec.AsciiString in project alien4cloud by alien4cloud.
the class WebSocketClientHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (this.authenticationUrl != null) {
HttpRequest loginRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, this.authenticationUrl);
loginRequest.headers().set(new AsciiString("host"), this.host);
HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(loginRequest, false);
bodyRequestEncoder.addBodyAttribute("j_username", user);
bodyRequestEncoder.addBodyAttribute("j_password", password);
bodyRequestEncoder.addBodyAttribute("submit", "Login");
loginRequest = bodyRequestEncoder.finalizeRequest();
if (log.isDebugEnabled()) {
log.debug("Authentication request for user {} to {}", this.user, this.authenticationUrl);
}
ctx.writeAndFlush(loginRequest);
} else {
handShaker.handshake(ctx.channel());
}
}
use of io.netty.handler.codec.AsciiString in project alien4cloud by alien4cloud.
the class WebSocketClientHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
// The first message must be authentication response
if (this.authenticationUrl != null && (this.cookies == null || this.cookies.isEmpty())) {
HttpResponse response = (HttpResponse) msg;
CharSequence cookieData = response.headers().get(new AsciiString("set-cookie"));
if (cookieData != null) {
this.cookies = ServerCookieDecoder.decode(cookieData.toString());
if (this.cookies == null || this.cookies.isEmpty()) {
throw new WebSocketAuthenticationFailureException("Could not authenticate");
}
if (log.isDebugEnabled()) {
for (Cookie cookie : this.cookies) {
log.debug("Server says must set cookie with name {} and value {}", cookie.name(), cookie.value());
}
}
} else {
throw new ITException("Could not authenticate");
}
if (log.isDebugEnabled()) {
log.debug("Authentication succeeded for user {}", this.user);
}
handShaker.handshake(ctx.channel());
return;
}
// The second one must be the response for web socket handshake
if (!handShaker.isHandshakeComplete()) {
handShaker.finishHandshake(ctx.channel(), (FullHttpResponse) msg);
if (log.isDebugEnabled()) {
log.debug("Web socket client connected for user {}", this.user);
}
handshakeFuture.setSuccess();
return;
}
// Take the byte buff and send it up to Stomp decoder
if (msg instanceof WebSocketFrame) {
if (log.isDebugEnabled()) {
if (msg instanceof TextWebSocketFrame) {
log.debug("Received text frame {}", ((TextWebSocketFrame) msg).text());
}
}
ReferenceCountUtil.retain(msg);
ctx.fireChannelRead(((WebSocketFrame) msg).content());
}
}
use of io.netty.handler.codec.AsciiString in project alien4cloud by alien4cloud.
the class WebSocketClientHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof HttpRequest) {
if (this.cookies != null && !this.cookies.isEmpty()) {
HttpRequest request = (HttpRequest) msg;
request.headers().set(new AsciiString("cookie"), ClientCookieEncoder.encode(cookies));
if (log.isDebugEnabled()) {
log.debug("Write HttpRequest {} enriched with security cookie", request);
}
}
super.write(ctx, msg, promise);
return;
}
String wrappedFrame = ((ByteBuf) msg).toString(Charset.forName("UTF-8"));
if (log.isDebugEnabled()) {
log.debug("Write text frame {}", wrappedFrame);
}
WebSocketFrame webSocketFrame = new TextWebSocketFrame(wrappedFrame);
super.write(ctx, webSocketFrame, promise);
}
Aggregations