use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method buildAuthRequest.
/**
* Helper method to build the SASL auth request.
*
* @param ctx the channel context.
* @return the created auth request.
* @throws SaslException if something went wrong during challenge evaluation.
*/
private ByteBuf buildAuthRequest(final ChannelHandlerContext ctx) throws SaslException {
byte[] payload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(Bytes.EMPTY_BYTE_ARRAY) : null;
ByteBuf body = payload != null ? ctx.alloc().buffer().writeBytes(payload) : Unpooled.EMPTY_BUFFER;
ByteBuf key = Unpooled.copiedBuffer(saslClient.getMechanismName(), UTF_8);
ByteBuf request = request(ctx.alloc(), MemcacheProtocol.Opcode.SASL_AUTH, noDatatype(), noPartition(), BaseKeyValueRequest.nextOpaque(), noCas(), noExtras(), key, body);
key.release();
body.release();
return request;
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method buildStepRequest.
/**
* Helper method to build the SASL step request based on the evaluated challenge.
*
* @param ctx the channel context
* @param evaluatedBytes the evaluated challenge.
* @return the full SASL step request.
*/
private ByteBuf buildStepRequest(final ChannelHandlerContext ctx, final byte[] evaluatedBytes) {
final String mech = saslClient.getMechanismName();
ByteBuf body;
if (mech.equalsIgnoreCase(SaslMechanism.PLAIN.mech())) {
String[] evaluated = new String(evaluatedBytes, UTF_8).split(" ");
body = Unpooled.copiedBuffer(username + "\0" + evaluated[1], UTF_8);
} else {
body = Unpooled.wrappedBuffer(evaluatedBytes);
}
ByteBuf key = Unpooled.copiedBuffer(mech, UTF_8);
ByteBuf request = request(ctx.alloc(), MemcacheProtocol.Opcode.SASL_STEP, noDatatype(), noPartition(), BaseKeyValueRequest.nextOpaque(), noCas(), noExtras(), key, body);
key.release();
body.release();
return request;
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslListMechanismsHandler method channelRead.
/**
* As soon as we get a response, turn it into a list of SASL mechanisms the server supports.
* <p>
* If the server responds with an empty list this is an issue and as a result we need to fail the connection
* immediately.
*
* @param ctx the {@link ChannelHandlerContext} for which the channel read operation is made.
* @param msg the incoming msg that needs to be parsed.
*/
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
if (msg instanceof ByteBuf) {
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
if (successful((ByteBuf) msg)) {
String[] rawMechansisms = body((ByteBuf) msg).orElse(Unpooled.EMPTY_BUFFER).toString(UTF_8).split(" ");
boolean isEmpty = rawMechansisms.length == 1 && rawMechansisms[0].isEmpty();
if (rawMechansisms.length > 0 && !isEmpty) {
final Set<SaslMechanism> serverMechanisms = decodeMechanisms(rawMechansisms);
ioContext.environment().eventBus().publish(new SaslMechanismsListedEvent(ioContext, serverMechanisms, latency.orElse(Duration.ZERO)));
ctx.channel().attr(ChannelAttributes.SASL_MECHS_KEY).set(serverMechanisms);
interceptedConnectPromise.trySuccess();
ctx.pipeline().remove(this);
} else {
failConnection("Received empty mechanism list from server", status((ByteBuf) msg), latency);
}
} else {
failConnection("Received non-success status from server", status((ByteBuf) msg), latency);
}
} else {
interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response " + "type on channel read, this is a bug - please report. " + msg));
}
ReferenceCountUtil.release(msg);
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class AnalyticsRequest method encode.
@Override
public FullHttpRequest encode() {
ByteBuf content = Unpooled.wrappedBuffer(query);
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, URI, content);
request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
request.headers().set(HttpHeaderNames.USER_AGENT, context().environment().userAgent().formattedLong());
if (priority != NO_PRIORITY) {
request.headers().set("Analytics-Priority", priority);
}
authenticator.authHttpRequest(serviceType(), request);
return request;
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class AppendRequest method encode.
@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
ByteBuf key = null;
ByteBuf content = null;
ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType);
try {
key = encodedKeyWithCollection(alloc, ctx);
byte datatype = 0;
CompressionConfig config = ctx.compressionConfig();
if (config != null && config.enabled() && this.content.length >= config.minSize()) {
ByteBuf maybeCompressed = MemcacheProtocol.tryCompression(this.content, config.minRatio());
if (maybeCompressed != null) {
datatype |= MemcacheProtocol.Datatype.SNAPPY.datatype();
content = maybeCompressed;
} else {
content = Unpooled.wrappedBuffer(this.content);
}
} else {
content = Unpooled.wrappedBuffer(this.content);
}
return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.APPEND, datatype, partition(), opaque, cas, flexibleExtras, noExtras(), key, content);
} finally {
ReferenceCountUtil.release(key);
ReferenceCountUtil.release(content);
ReferenceCountUtil.release(flexibleExtras);
}
}
Aggregations