List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:com.couchbase.client.core.endpoint.dcp.DCPHandler.java
License:Apache License
private List<FailoverLogEntry> readFailoverLogs(final ByteBuf content) { List<FailoverLogEntry> failoverLog = new ArrayList<FailoverLogEntry>(content.readableBytes() / 16); while (content.readableBytes() >= 16) { FailoverLogEntry entry = new FailoverLogEntry(content.readLong(), content.readLong()); failoverLog.add(entry);//from w w w. j a v a 2 s. c o m } return failoverLog; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java
License:Apache License
/** * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step. * * @param ctx the handler context./* www . j a v a 2 s . c o m*/ * @param msg the incoming message to investigate. * @throws Exception if something goes wrong during negotiation. */ 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.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, payload); initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength) .setTotalBodyLength(mechanismLength + payload.readableBytes()); ChannelFuture future = ctx.writeAndFlush(initialRequest); future.addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { LOGGER.warn("Error during SASL Auth negotiation phase.", future); } } }); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java
License:Apache License
/** * Handles an incoming SASL AUTH response and - if needed - dispatches the SASL STEPs. * * @param ctx the handler context./* ww w .ja va 2 s. c o m*/ * @param msg the incoming message to investigate. * @throws Exception if something goes wrong during negotiation. */ private void handleAuthResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg) throws Exception { if (saslClient.isComplete()) { checkIsAuthed(msg); return; } byte[] response = new byte[msg.content().readableBytes()]; msg.content().readBytes(response); byte[] evaluatedBytes = saslClient.evaluateChallenge(response); if (evaluatedBytes != null) { ByteBuf content; // This is needed against older server versions where the protocol does not // align on cram and plain, the else block is used for all the newer cram-sha* // mechanisms. // // Note that most likely this is only executed in the CRAM-MD5 case only, but // just to play it safe keep it for both mechanisms. if (selectedMechanism.equals("CRAM-MD5") || selectedMechanism.equals("PLAIN")) { String[] evaluated = new String(evaluatedBytes).split(" "); content = Unpooled.copiedBuffer(username + "\0" + evaluated[1], CharsetUtil.UTF_8); } else { content = Unpooled.wrappedBuffer(evaluatedBytes); } FullBinaryMemcacheRequest stepRequest = new DefaultFullBinaryMemcacheRequest( selectedMechanism.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, content); stepRequest.setOpcode(SASL_STEP_OPCODE).setKeyLength((short) selectedMechanism.length()) .setTotalBodyLength(content.readableBytes() + selectedMechanism.length()); ChannelFuture future = ctx.writeAndFlush(stepRequest); future.addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { LOGGER.warn("Error during SASL Auth negotiation phase.", future); } } }); } else { throw new AuthenticationException("SASL Challenge evaluation returned null."); } }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueFeatureHandler.java
License:Apache License
/** * Creates the HELLO request to ask for certain supported features. * * @return the request to send over the wire *//*w ww . j av a 2s . c om*/ private FullBinaryMemcacheRequest helloRequest() { byte[] key = userAgent.getBytes(CharsetUtil.UTF_8); short keyLength = (short) key.length; ByteBuf wanted = Unpooled.buffer(features.size() * 2); for (ServerFeatures feature : features) { wanted.writeShort(feature.value()); } LOGGER.debug("Requesting supported features: {}", features); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER, wanted); request.setOpcode(HELLO_CMD); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + wanted.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link GetRequest} into its lower level representation. * * Depending on the flags set on the {@link GetRequest}, the appropriate opcode gets chosen. Currently, a regular * get, as well as "get and touch" and "get and lock" are supported. Latter variants have server-side side-effects * but do not differ in response behavior. * * @param ctx the {@link ChannelHandlerContext} to use for allocation and others. * @param msg the incoming message./* ww w . ja va2s. co m*/ * @return a ready {@link BinaryMemcacheRequest}. */ private static BinaryMemcacheRequest handleGetRequest(final ChannelHandlerContext ctx, final GetRequest msg) { byte opcode; ByteBuf extras; if (msg.lock()) { opcode = OP_GET_AND_LOCK; extras = ctx.alloc().buffer().writeInt(msg.expiry()); } else if (msg.touch()) { opcode = OP_GET_AND_TOUCH; extras = ctx.alloc().buffer().writeInt(msg.expiry()); } else { opcode = OP_GET; extras = Unpooled.EMPTY_BUFFER; } byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key); request.setOpcode(opcode).setKeyLength(keyLength).setExtras(extras).setExtrasLength(extrasLength) .setTotalBodyLength(keyLength + extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link BinaryStoreRequest} into its lower level representation. * * There are three types of store operations that need to be considered: insert, upsert and replace, which * directly translate to the add, set and replace binary memcached opcodes. By convention, only the replace * command supports setting a CAS value, even if the others theoretically would do as well (but do not provide * benefit in such cases).//from w ww . j a v a 2 s . co m * * Currently, the content is loaded and sent down in one batch, streaming for requests is not supported. * * @return a ready {@link BinaryMemcacheRequest}. */ private static BinaryMemcacheRequest handleStoreRequest(final ChannelHandlerContext ctx, final BinaryStoreRequest msg) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(msg.flags()); extras.writeInt(msg.expiration()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content()); if (msg instanceof InsertRequest) { request.setOpcode(OP_INSERT); } else if (msg instanceof UpsertRequest) { request.setOpcode(OP_UPSERT); } else if (msg instanceof ReplaceRequest) { request.setOpcode(OP_REPLACE); request.setCAS(((ReplaceRequest) msg).cas()); } else { throw new IllegalArgumentException("Unknown incoming BinaryStoreRequest type " + msg.getClass()); } request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength); request.setExtrasLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link CounterRequest} into its lower level representation. * * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached * commands are utilized. The value is converted to its absolute variant to conform with the protocol. * * @return a ready {@link BinaryMemcacheRequest}. *///from www . jav a 2 s. co m private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx, final CounterRequest msg) { ByteBuf extras = ctx.alloc().buffer(); extras.writeLong(Math.abs(msg.delta())); extras.writeLong(msg.initial()); extras.writeInt(msg.expiry()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras); request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + extrasLength); request.setExtrasLength(extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Encodes a {@link TouchRequest} into its lower level representation. * * @return a ready {@link BinaryMemcacheRequest}. *///from w w w .ja v a2 s . com private static BinaryMemcacheRequest handleTouchRequest(final ChannelHandlerContext ctx, final TouchRequest msg) { ByteBuf extras = ctx.alloc().buffer(); extras.writeInt(msg.expiry()); byte[] key = msg.keyBytes(); short keyLength = (short) key.length; byte extrasLength = (byte) extras.readableBytes(); BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key); request.setExtras(extras); request.setOpcode(OP_TOUCH); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + extrasLength); request.setExtrasLength(extrasLength); 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}. *//*w ww . j a va 2s .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.kv.KeyValueHandler.java
License:Apache License
private static BinaryMemcacheRequest handleObserveSeqnoRequest(final ChannelHandlerContext ctx, final ObserveSeqnoRequest msg) { ByteBuf content = ctx.alloc().buffer(); content.writeLong(msg.vbucketUUID()); BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER, content);/*w w w. java 2 s . c om*/ request.setOpcode(OP_OBSERVE_SEQ); request.setTotalBodyLength(content.readableBytes()); return request; }