List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void write(XHROptionsMessage msg, ChannelHandlerContext ctx) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); HttpHeaders.addHeader(res, "Set-Cookie", "io=" + msg.getSessionId()); HttpHeaders.addHeader(res, CONNECTION, KEEP_ALIVE); HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_HEADERS, CONTENT_TYPE); addOriginHeaders(ctx.channel(), res); ByteBuf out = encoder.allocateBuffer(ctx.alloc()); sendMessage(msg, ctx.channel(), out, res); }
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void write(XHRPostMessage msg, ChannelHandlerContext ctx) { ByteBuf out = encoder.allocateBuffer(ctx.alloc()); out.writeBytes(OK);/* w w w .j a va 2 s . co m*/ sendMessage(msg, ctx.channel(), out, "text/html"); }
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx) throws IOException { while (true) { Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport()); Packet packet = queue.poll();//from ww w . jav a 2 s . c o m if (packet == null) { break; } final ByteBuf out = encoder.allocateBuffer(ctx.alloc()); encoder.encodePacket(packet, out, ctx.alloc(), true); WebSocketFrame res = new TextWebSocketFrame(out); if (log.isTraceEnabled()) { log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId()); } if (out.isReadable()) { ctx.channel().writeAndFlush(res); } else { out.release(); } for (ByteBuf buf : packet.getAttachments()) { ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc()); outBuf.writeByte(4); outBuf.writeBytes(buf); if (log.isTraceEnabled()) { log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId()); } ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf)); } } }
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void handleHTTP(OutPacketMessage msg, ChannelHandlerContext ctx) throws IOException { Channel channel = ctx.channel(); Attribute<Boolean> attr = channel.attr(WRITE_ONCE); Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport()); if (!channel.isActive() || queue.isEmpty() || !attr.compareAndSet(null, true)) { return;/* w w w . j a va 2 s. c om*/ } ByteBuf out = encoder.allocateBuffer(ctx.alloc()); Boolean b64 = ctx.channel().attr(EncoderHandler.B64).get(); if (b64 != null && b64) { Integer jsonpIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get(); encoder.encodeJsonP(jsonpIndex, queue, out, ctx.alloc(), 50); String type = "application/javascript"; if (jsonpIndex == null) { type = "text/plain"; } sendMessage(msg, channel, out, type); } else { encoder.encodePackets(queue, out, ctx.alloc(), 50); sendMessage(msg, channel, out, "application/octet-stream"); } }
From source file:com.corundumstudio.socketio.SocketIOEncoder.java
License:Apache License
private void write(HttpMessage xhrMessage, Packet packet, ChannelHandlerContext ctx, ByteBuf out) throws IOException { XHRClientEntry clientEntry = getXHRClientEntry(xhrMessage.getSessionId()); if (packet != null) { clientEntry.addPacket(packet);// ww w . j a va 2 s. c o m } Channel channel = ctx.channel(); if (!channel.isActive() || clientEntry.getPackets().isEmpty() || !clientEntry.writeOnce(channel)) { out.release(); return; } encoder.encodePackets(clientEntry.getPackets(), out, ctx.alloc()); sendMessage(xhrMessage, channel, out); }
From source file:com.corundumstudio.socketio.SocketIOEncoder.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (!(msg instanceof BaseMessage)) { super.write(ctx, msg, promise); return;//from w w w. j a va 2 s . com } ByteBuf out = encoder.allocateBuffer(ctx.alloc()); if (msg instanceof AuthorizeMessage) { handle((AuthorizeMessage) msg, ctx.channel(), out); } if (msg instanceof XHRNewChannelMessage) { write((XHRNewChannelMessage) msg, null, ctx, out); } if (msg instanceof XHRPacketMessage) { XHRPacketMessage m = (XHRPacketMessage) msg; write(m, m.getPacket(), ctx, out); } if (msg instanceof XHROutMessage) { sendMessage((XHROutMessage) msg, ctx.channel(), out); } if (msg instanceof XHRErrorMessage) { XHRErrorMessage xhrErrorMessage = (XHRErrorMessage) msg; encoder.encodePacket(xhrErrorMessage.getPacket(), out); sendMessage(xhrErrorMessage, ctx.channel(), out); } if (msg instanceof WebSocketPacketMessage) { handle((WebSocketPacketMessage) msg, ctx.channel(), out); } if (msg instanceof WebsocketErrorMessage) { handle((WebsocketErrorMessage) msg, ctx.channel(), out); } }
From source file:com.couchbase.client.core.endpoint.AbstractGenericHandler.java
License:Apache License
/** * Add basic authentication headers to a {@link HttpRequest}. * * The given information is Base64 encoded and the authorization header is set appropriately. Since this needs * to be done for every request, it is refactored out. * * @param ctx the handler context.//from w w w. j av a2 s. co m * @param request the request where the header should be added. * @param user the username for auth. * @param password the password for auth. */ public static void addHttpBasicAuth(final ChannelHandlerContext ctx, final HttpRequest request, final String user, final String password) { final String pw = password == null ? "" : password; ByteBuf raw = ctx.alloc().buffer(user.length() + pw.length() + 1); raw.writeBytes((user + ":" + pw).getBytes(CHARSET)); ByteBuf encoded = Base64.encode(raw, false); request.headers().add(HttpHeaders.Names.AUTHORIZATION, "Basic " + encoded.toString(CHARSET)); encoded.release(); raw.release(); }
From source file:com.couchbase.client.core.endpoint.binary.BinaryAuthHandler.java
License:Open Source License
/** * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step. * * @param ctx the handler context.//from w w w . jav a 2 s . c o m * @param msg the incoming message to investigate. * @throws Exception */ private void handleListMechsResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg) throws Exception { String remote = ctx.channel().remoteAddress().toString(); String[] supportedMechanisms = msg.content().toString(CharsetUtil.UTF_8).split(" "); if (supportedMechanisms.length == 0) { throw new AuthenticationException("Received empty SASL mechanisms list from server: " + remote); } saslClient = Sasl.createSaslClient(supportedMechanisms, null, "couchbase", remote, null, this); selectedMechanism = saslClient.getMechanismName(); int mechanismLength = selectedMechanism.length(); byte[] bytePayload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(new byte[] {}) : null; ByteBuf payload = bytePayload != null ? ctx.alloc().buffer().writeBytes(bytePayload) : Unpooled.EMPTY_BUFFER; FullBinaryMemcacheRequest initialRequest = new DefaultFullBinaryMemcacheRequest(selectedMechanism, Unpooled.EMPTY_BUFFER, payload); initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength) .setTotalBodyLength(mechanismLength + payload.readableBytes()); ctx.writeAndFlush(initialRequest); }
From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java
License:Open Source License
/** * Creates the actual protocol level request for an incoming upsert request. * * @param request the incoming upsert request. * @param ctx the channel handler context for buffer allocations. * @return the built protocol request./* w ww. ja v a 2 s. c o m*/ */ private BinaryMemcacheRequest handleUpsertRequest(final UpsertRequest request, final ChannelHandlerContext ctx) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(request.flags()); extras.writeInt(request.expiration()); FullBinaryMemcacheRequest msg = new DefaultFullBinaryMemcacheRequest(request.key(), extras, request.content()); msg.setOpcode(BinaryMemcacheOpcodes.SET); msg.setKeyLength((short) request.key().length()); msg.setTotalBodyLength( (short) request.key().length() + request.content().readableBytes() + extras.readableBytes()); msg.setReserved(request.partition()); msg.setExtrasLength((byte) extras.readableBytes()); return msg; }
From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java
License:Open Source License
/** * Creates the actual protocol level request for an incoming replacer request. * * @param request the incoming replace request. * @param ctx the channel handler context for buffer allocations. * @return the built protocol request./*from ww w .j a v a2 s. co m*/ */ private BinaryMemcacheRequest handleReplaceRequest(final ReplaceRequest request, final ChannelHandlerContext ctx) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(request.flags()); extras.writeInt(request.expiration()); FullBinaryMemcacheRequest msg = new DefaultFullBinaryMemcacheRequest(request.key(), extras, request.content()); msg.setOpcode(BinaryMemcacheOpcodes.REPLACE); msg.setCAS(request.cas()); msg.setKeyLength((short) request.key().length()); msg.setTotalBodyLength( (short) request.key().length() + request.content().readableBytes() + extras.readableBytes()); msg.setReserved(request.partition()); msg.setExtrasLength((byte) extras.readableBytes()); return msg; }