List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java
License:Apache License
private FullBinaryMemcacheRequest controlRequest(ChannelHandlerContext ctx, ControlParameter parameter, String value) {//from ww w .ja v a 2 s. c om byte[] key = parameter.value().getBytes(CharsetUtil.UTF_8); short keyLength = (short) key.length; byte[] val = value.getBytes(CharsetUtil.UTF_8); ByteBuf body = ctx.alloc().buffer(val.length); body.writeBytes(val); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER, body); request.setOpcode(DCPHandler.OP_CONTROL); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + body.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link ObserveRequest} into its lower level representation. * * @return a ready {@link BinaryMemcacheRequest}. *///from w w w. j a v a2 s . c om private static BinaryMemcacheRequest handleObserveRequest(final ChannelHandlerContext ctx, final ObserveRequest msg) { String key = msg.key(); short keyLength = (short) msg.keyBytes().length; ByteBuf content = ctx.alloc().buffer(); content.writeShort(msg.partition()); content.writeShort(keyLength); content.writeBytes(key.getBytes(CHARSET)); BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER, content); request.setOpcode(OP_OBSERVE); request.setTotalBodyLength(content.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final QueryRequest msg) throws Exception { FullHttpRequest request;//from w w w .j av a2s .co m if (msg instanceof GenericQueryRequest) { GenericQueryRequest queryRequest = (GenericQueryRequest) msg; request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/query"); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); if (queryRequest.isJsonFormat()) { request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); } ByteBuf query = ctx.alloc().buffer(((GenericQueryRequest) msg).query().length()); query.writeBytes(((GenericQueryRequest) msg).query().getBytes(CHARSET)); request.headers().add(HttpHeaders.Names.CONTENT_LENGTH, query.readableBytes()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); request.content().writeBytes(query); query.release(); } else if (msg instanceof KeepAliveRequest) { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/admin/ping"); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); return request; } else { throw new IllegalArgumentException("Unknown incoming QueryRequest type " + msg.getClass()); } addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ViewRequest msg) throws Exception { if (msg instanceof KeepAliveRequest) { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD, "/", Unpooled.EMPTY_BUFFER);/*from ww w. j a v a 2s. com*/ request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0); return request; } StringBuilder path = new StringBuilder(); HttpMethod method = HttpMethod.GET; ByteBuf content = null; if (msg instanceof ViewQueryRequest) { ViewQueryRequest queryMsg = (ViewQueryRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.design() : queryMsg.design()); if (queryMsg.spatial()) { path.append("/_spatial/"); } else { path.append("/_view/"); } path.append(queryMsg.view()); int queryLength = queryMsg.query() == null ? 0 : queryMsg.query().length(); int keysLength = queryMsg.keys() == null ? 0 : queryMsg.keys().length(); boolean hasQuery = queryLength > 0; boolean hasKeys = keysLength > 0; if (hasQuery || hasKeys) { if (queryLength + keysLength < MAX_GET_LENGTH) { //the query is short enough for GET //it has query, query+keys or keys only if (hasQuery) { path.append("?").append(queryMsg.query()); if (hasKeys) { path.append("&keys=").append(encodeKeysGet(queryMsg.keys())); } } else { //it surely has keys if not query path.append("?keys=").append(encodeKeysGet(queryMsg.keys())); } } else { //the query is too long for GET, use the keys as JSON body if (hasQuery) { path.append("?").append(queryMsg.query()); } String keysContent = encodeKeysPost(queryMsg.keys()); //switch to POST method = HttpMethod.POST; //body is "keys" but in JSON content = ctx.alloc().buffer(keysContent.length()); content.writeBytes(keysContent.getBytes(CHARSET)); } } } else if (msg instanceof GetDesignDocumentRequest) { GetDesignDocumentRequest queryMsg = (GetDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); } else if (msg instanceof UpsertDesignDocumentRequest) { method = HttpMethod.PUT; UpsertDesignDocumentRequest queryMsg = (UpsertDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); content = Unpooled.copiedBuffer(queryMsg.body(), CHARSET); } else if (msg instanceof RemoveDesignDocumentRequest) { method = HttpMethod.DELETE; RemoveDesignDocumentRequest queryMsg = (RemoveDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); } else { throw new IllegalArgumentException("Unknown incoming ViewRequest type " + msg.getClass()); } if (content == null) { content = Unpooled.buffer(0); } FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(), content); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes()); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiLookupRequest.java
License:Apache License
private static ByteBuf encode(List<LookupCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator? for (LookupCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator? commandBuf.writeByte(command.opCode()); commandBuf.writeByte(0); //no flags supported for lookup commandBuf.writeShort(pathLength); //no value length commandBuf.writeBytes(pathBytes); compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); }/*from w w w.ja v a 2s . c om*/ return compositeBuf; }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiMutationRequest.java
License:Apache License
private static ByteBuf encode(List<MutationCommand> commands) { //FIXME a way of using the pooled allocator? CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); for (MutationCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes()); commandBuf.writeByte(command.opCode()); if (command.createIntermediaryPath()) { commandBuf.writeByte(KeyValueHandler.SUBDOC_BITMASK_MKDIR_P); //0 | SUBDOC_BITMASK_MKDIR_P } else {//w w w . j a v a 2 s.co m commandBuf.writeByte(0); } commandBuf.writeShort(pathLength); commandBuf.writeInt(command.fragment().readableBytes()); commandBuf.writeBytes(pathBytes); //copy the fragment but don't move indexes (in case it is retained and reused) commandBuf.writeBytes(command.fragment(), command.fragment().readerIndex(), command.fragment().readableBytes()); //eagerly release the fragment once it's been copied command.fragment().release(); //add the command to the composite buffer compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
From source file:com.cssweb.network.CustomEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, CustomMessage response, ByteBuf out) throws Exception { logger.info("encode"); // //from w w w. j ava 2 s . c o m out.writeBytes(response.getCustomMessageHeader().getMsgHeader()); // out.writeBytes(response.getMsgContent()); }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static void writeString(String str, ByteBuf cb) { byte[] bytes = str.getBytes(CharsetUtil.UTF_8); cb.writeShort(bytes.length);/*from w w w. ja va2 s. c om*/ cb.writeBytes(bytes); }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static void writeLongString(String str, ByteBuf cb) { byte[] bytes = str.getBytes(CharsetUtil.UTF_8); cb.writeInt(bytes.length);//from w w w . j a v a 2s. c o m cb.writeBytes(bytes); }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static void writeBytes(byte[] bytes, ByteBuf cb) { cb.writeShort(bytes.length); cb.writeBytes(bytes); }