Search in sources :

Example 1 with TypeReference

use of com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference in project couchbase-jvm-clients by couchbase.

the class SaslAuthenticationHandler method failConnect.

/**
 * Refactored method which is called from many places to fail the connection
 * process because of an issue during SASL auth.
 *
 * <p>Usually errors during auth are very problematic and as a result we cannot continue
 * with this channel connect attempt.</p>
 *
 * @param ctx the channel context.
 */
private void failConnect(final ChannelHandlerContext ctx, final String message, final ByteBuf lastPacket, final Throwable cause, final short status) {
    Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
    byte[] packetCopy = Bytes.EMPTY_BYTE_ARRAY;
    Map<String, Object> serverContext = null;
    if (lastPacket != null) {
        if (MemcacheProtocol.verifyResponse(lastPacket)) {
            // This is a proper response, try to extract server context
            Optional<ByteBuf> body = MemcacheProtocol.body(lastPacket);
            if (body.isPresent()) {
                byte[] content = ByteBufUtil.getBytes(body.get());
                try {
                    serverContext = Mapper.decodeInto(content, new TypeReference<Map<String, Object>>() {
                    });
                } catch (Exception ex) {
                // Ignore, no displayable content
                }
            }
        } else {
            // This is not a proper memcache response, store the raw packet for debugging purposes
            int ridx = lastPacket.readerIndex();
            lastPacket.readerIndex(lastPacket.writerIndex());
            packetCopy = new byte[lastPacket.readableBytes()];
            lastPacket.readBytes(packetCopy);
            lastPacket.readerIndex(ridx);
        }
    }
    KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, serverContext);
    endpointContext.environment().eventBus().publish(new SaslAuthenticationFailedEvent(latency.orElse(Duration.ZERO), errorContext, message, packetCopy));
    interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, cause));
    ctx.pipeline().remove(this);
}
Also used : Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TimeoutException(java.util.concurrent.TimeoutException) SaslException(javax.security.sasl.SaslException) SaslAuthenticationFailedEvent(com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)

Example 2 with TypeReference

use of com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference in project couchbase-jvm-clients by couchbase.

the class AsyncSearchIndexManager method analyzeDocument.

/**
 * Allows to see how a document is analyzed against a specific index.
 *
 * @param name the name of the search index.
 * @param document the document to analyze.
 * @return a {@link CompletableFuture} with analyzed document parts once complete.
 */
public CompletableFuture<List<JsonObject>> analyzeDocument(final String name, final JsonObject document, final AnalyzeDocumentOptions options) {
    notNullOrEmpty(name, "Search Index Name");
    notNull(document, "Document");
    return searchHttpClient.post(path(analyzeDocumentPath(name)), options.build()).trace(TracingIdentifiers.SPAN_REQUEST_MS_ANALYZE_DOCUMENT).json(Mapper.encodeAsBytes(document.toMap())).exec(core).exceptionally(throwable -> {
        if (throwable.getMessage().contains("Page not found")) {
            throw new FeatureNotAvailableException("Document analysis is not available on this server version!");
        } else if (throwable instanceof RuntimeException) {
            throw (RuntimeException) throwable;
        } else {
            throw new CouchbaseException("Failed to analyze search document", throwable);
        }
    }).thenApply(response -> {
        JsonNode rootNode = Mapper.decodeIntoTree(response.content());
        List<Map<String, Object>> analyzed = Mapper.convertValue(rootNode.get("analyzed"), new TypeReference<List<Map<String, Object>>>() {
        });
        return analyzed.stream().filter(Objects::nonNull).map(JsonObject::from).collect(Collectors.toList());
    });
}
Also used : AnalyzeDocumentOptions.analyzeDocumentOptions(com.couchbase.client.java.manager.search.AnalyzeDocumentOptions.analyzeDocumentOptions) HttpHeaderNames(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpHeaderNames) GetSearchIndexOptions.getSearchIndexOptions(com.couchbase.client.java.manager.search.GetSearchIndexOptions.getSearchIndexOptions) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) DropSearchIndexOptions.dropSearchIndexOptions(com.couchbase.client.java.manager.search.DropSearchIndexOptions.dropSearchIndexOptions) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetAllSearchIndexesOptions.getAllSearchIndexesOptions(com.couchbase.client.java.manager.search.GetAllSearchIndexesOptions.getAllSearchIndexesOptions) CbTracing(com.couchbase.client.core.cnc.CbTracing) AllowQueryingSearchIndexOptions.allowQueryingSearchIndexOptions(com.couchbase.client.java.manager.search.AllowQueryingSearchIndexOptions.allowQueryingSearchIndexOptions) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) Mapper(com.couchbase.client.core.json.Mapper) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) UrlQueryStringBuilder.urlEncode(com.couchbase.client.core.util.UrlQueryStringBuilder.urlEncode) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) UpsertSearchIndexOptions.upsertSearchIndexOptions(com.couchbase.client.java.manager.search.UpsertSearchIndexOptions.upsertSearchIndexOptions) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) PauseIngestSearchIndexOptions.pauseIngestSearchIndexOptions(com.couchbase.client.java.manager.search.PauseIngestSearchIndexOptions.pauseIngestSearchIndexOptions) Objects(java.util.Objects) List(java.util.List) GetIndexedSearchIndexOptions.getIndexedSearchIndexOptions(com.couchbase.client.java.manager.search.GetIndexedSearchIndexOptions.getIndexedSearchIndexOptions) UnfreezePlanSearchIndexOptions.unfreezePlanSearchIndexOptions(com.couchbase.client.java.manager.search.UnfreezePlanSearchIndexOptions.unfreezePlanSearchIndexOptions) ResumeIngestSearchIndexOptions.resumeIngestSearchIndexOptions(com.couchbase.client.java.manager.search.ResumeIngestSearchIndexOptions.resumeIngestSearchIndexOptions) JsonObject(com.couchbase.client.java.json.JsonObject) FreezePlanSearchIndexOptions.freezePlanSearchIndexOptions(com.couchbase.client.java.manager.search.FreezePlanSearchIndexOptions.freezePlanSearchIndexOptions) Core(com.couchbase.client.core.Core) DisallowQueryingSearchIndexOptions.disallowQueryingSearchIndexOptions(com.couchbase.client.java.manager.search.DisallowQueryingSearchIndexOptions.disallowQueryingSearchIndexOptions) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) Objects(java.util.Objects) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.couchbase.client.java.json.JsonObject) Map(java.util.Map)

Aggregations

TypeReference (com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)2 Core (com.couchbase.client.core.Core)1 CbTracing (com.couchbase.client.core.cnc.CbTracing)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 SaslAuthenticationFailedEvent (com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent)1 JsonNode (com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode)1 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 HttpHeaderNames (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpHeaderNames)1 CoreHttpClient (com.couchbase.client.core.endpoint.http.CoreHttpClient)1 CoreHttpPath.path (com.couchbase.client.core.endpoint.http.CoreHttpPath.path)1 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 FeatureNotAvailableException (com.couchbase.client.core.error.FeatureNotAvailableException)1 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)1 KeyValueIoErrorContext (com.couchbase.client.core.error.context.KeyValueIoErrorContext)1 Mapper (com.couchbase.client.core.json.Mapper)1 RequestTarget (com.couchbase.client.core.msg.RequestTarget)1 UrlQueryStringBuilder.urlEncode (com.couchbase.client.core.util.UrlQueryStringBuilder.urlEncode)1 Validators.notNull (com.couchbase.client.core.util.Validators.notNull)1