Search in sources :

Example 1 with CoreCommonOptions

use of com.couchbase.client.core.endpoint.http.CoreCommonOptions in project couchbase-jdbc-driver by couchbaselabs.

the class ConnectionHandle method rawAnalyticsQuery.

/**
 * Sends a raw analytics query, allows to be used where the regular API does not suffice.
 * <p>
 * It should really only be used if the primary query API cannot be used for some reason.
 *
 * @param method the http method to use.
 * @param path the http path to execute.
 * @param headers the optional http headers to send.
 * @param content the optional payload to send.
 * @param timeout the timeout to use.
 * @return the core response to use.
 * @throws SQLException in case the query failed.
 */
public CoreHttpResponse rawAnalyticsQuery(HttpMethod method, String path, Map<String, Object> headers, byte[] content, Duration timeout) throws SQLException {
    CoreHttpClient client = cluster.core().httpClient(RequestTarget.analytics());
    CoreCommonOptions options = CoreCommonOptions.of(timeout == null || timeout.isZero() ? null : timeout, null, null);
    CoreHttpRequest.Builder builder;
    switch(method) {
        case GET:
            builder = client.get(path(path), options);
            break;
        case DELETE:
            builder = client.delete(path(path), options);
            break;
        case POST:
            builder = client.post(path(path), options);
            if (content != null) {
                builder = builder.json(content);
            }
            break;
        default:
            throw new IllegalStateException("Unsupported http verb: " + method);
    }
    if (headers != null) {
        for (Map.Entry<String, Object> header : headers.entrySet()) {
            builder = builder.header(header.getKey(), header.getValue());
        }
    }
    try {
        return builder.build().exec(cluster.core()).get();
    } catch (ExecutionException ex) {
        if (ex.getCause() instanceof CouchbaseException) {
            String ctx = ((CouchbaseException) ex.getCause()).context().exportAsString(Context.ExportFormat.JSON);
            throw new SQLException("Failed to perform analytics query: " + ctx, ex);
        } else {
            throw new SQLException("Failed to perform analytics query - cause: " + ex.getMessage(), ex);
        }
    } catch (Exception ex) {
        throw new SQLException("Failed to perform analytics query - cause: " + ex.getMessage(), ex);
    }
}
Also used : CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest) SQLException(java.sql.SQLException) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ExecutionException(java.util.concurrent.ExecutionException) SQLException(java.sql.SQLException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Example 2 with CoreCommonOptions

use of com.couchbase.client.core.endpoint.http.CoreCommonOptions in project couchbase-jvm-clients by couchbase.

the class CoreAnalyticsLinkManager method sendLink.

private CompletableFuture<Void> sendLink(HttpMethod method, Map<String, String> link, CoreCommonOptions options, String tracingId) {
    // ensure mutability, and don't modify caller's map
    link = new HashMap<>(link);
    CoreHttpPath path = getLinkPathAndAdjustMap(link);
    UrlQueryStringBuilder form = newForm();
    link.forEach(form::set);
    return httpClient.newRequest(method, path, options).trace(tracingId).form(form).exec(core).exceptionally(t -> {
        throw translateCompilationFailureToInvalidArgument(t);
    }).thenApply(result -> null);
}
Also used : Builder.newForm(com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newForm) AnalyticsErrorContext(com.couchbase.client.core.error.context.AnalyticsErrorContext) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) CompilationFailureException(com.couchbase.client.core.error.CompilationFailureException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) CbCollections.mapOf(com.couchbase.client.core.util.CbCollections.mapOf) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) CoreHttpResponse(com.couchbase.client.core.endpoint.http.CoreHttpResponse) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Builder.newQueryString(com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newQueryString) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) CbThrowables(com.couchbase.client.core.util.CbThrowables) Core(com.couchbase.client.core.Core) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder)

Example 3 with CoreCommonOptions

use of com.couchbase.client.core.endpoint.http.CoreCommonOptions in project couchbase-jvm-clients by couchbase.

the class HealthPinger method pingKv.

private static Mono<EndpointPingReport> pingKv(final Core core, final RequestTarget target, final CoreCommonOptions options) {
    return Mono.defer(() -> {
        Duration timeout = options.timeout().orElse(core.context().environment().timeoutConfig().kvTimeout());
        CollectionIdentifier collectionIdentifier = CollectionIdentifier.fromDefault(target.bucketName());
        KvPingRequest request = new KvPingRequest(timeout, core.context(), options.retryStrategy().orElse(null), collectionIdentifier, target.nodeIdentifier());
        core.send(request);
        return Reactor.wrap(request, request.response(), true).map(response -> {
            request.context().logicallyComplete();
            return assembleSuccessReport(request.context(), ((KvPingResponse) response).channelId(), Optional.ofNullable(target.bucketName()));
        }).onErrorResume(throwable -> {
            request.context().logicallyComplete();
            return Mono.just(assembleFailureReport(throwable, request.context(), Optional.ofNullable(target.bucketName())));
        });
    });
}
Also used : ClusterConfig(com.couchbase.client.core.config.ClusterConfig) CbCollections.isNullOrEmpty(com.couchbase.client.core.util.CbCollections.isNullOrEmpty) CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest) TimeoutException(com.couchbase.client.core.error.TimeoutException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) KvPingRequest(com.couchbase.client.core.msg.kv.KvPingRequest) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) HashSet(java.util.HashSet) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Stability(com.couchbase.client.core.annotation.Stability) RequestContext(com.couchbase.client.core.msg.RequestContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) KvPingResponse(com.couchbase.client.core.msg.kv.KvPingResponse) Reactor(com.couchbase.client.core.Reactor) NodeInfo(com.couchbase.client.core.config.NodeInfo) Set(java.util.Set) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) PortInfo(com.couchbase.client.core.config.PortInfo) KvPingRequest(com.couchbase.client.core.msg.kv.KvPingRequest) KvPingResponse(com.couchbase.client.core.msg.kv.KvPingResponse) Duration(java.time.Duration) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 4 with CoreCommonOptions

use of com.couchbase.client.core.endpoint.http.CoreCommonOptions in project couchbase-jvm-clients by couchbase.

the class CoreViewIndexManager method publishDesignDocument.

/**
 * Convenience method that gets a the document from the development namespace
 * and upserts it to the production namespace.
 *
 * @param name name of the development design document
 * @throws DesignDocumentNotFoundException if the development namespace does not contain a document with the given name
 */
public CompletableFuture<Void> publishDesignDocument(String name, CoreCommonOptions options) {
    notNullOrEmpty(name, "Name", () -> new ReducedViewErrorContext(null, null, bucket));
    RequestSpan span = buildSpan(TracingIdentifiers.SPAN_REQUEST_MV_PUBLISH_DD, options.parentSpan());
    CoreCommonOptions childOptions = options.withParentSpan(span);
    return getDesignDocument(name, false, childOptions).thenCompose(doc -> upsertDesignDocument(name, doc, true, childOptions)).whenComplete((r, t) -> span.end());
}
Also used : DesignDocumentNotFoundException(com.couchbase.client.core.error.DesignDocumentNotFoundException) HttpStatusCodeException(com.couchbase.client.core.error.HttpStatusCodeException) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) CompletableFuture(java.util.concurrent.CompletableFuture) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) LinkedHashMap(java.util.LinkedHashMap) CbCollections.mapOf(com.couchbase.client.core.util.CbCollections.mapOf) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) ObjectNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode) CbStrings.removeStart(com.couchbase.client.core.util.CbStrings.removeStart) CbTracing(com.couchbase.client.core.cnc.CbTracing) ReducedViewErrorContext(com.couchbase.client.core.error.context.ReducedViewErrorContext) Mapper(com.couchbase.client.core.json.Mapper) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) UrlQueryStringBuilder.urlEncode(com.couchbase.client.core.util.UrlQueryStringBuilder.urlEncode) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) CoreHttpResponse(com.couchbase.client.core.endpoint.http.CoreHttpResponse) Optional(java.util.Optional) Core(com.couchbase.client.core.Core) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) ReducedViewErrorContext(com.couchbase.client.core.error.context.ReducedViewErrorContext) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with CoreCommonOptions

use of com.couchbase.client.core.endpoint.http.CoreCommonOptions in project couchbase-jvm-clients by couchbase.

the class CoreBucketManager method updateBucket.

public CompletableFuture<Void> updateBucket(Map<String, String> settings, final CoreCommonOptions options) {
    String bucketName = getBucketName(settings);
    RequestSpan span = CbTracing.newSpan(core.context(), TracingIdentifiers.SPAN_REQUEST_MB_UPDATE_BUCKET, options.parentSpan().orElse(null));
    span.attribute(TracingIdentifiers.ATTR_NAME, bucketName);
    CoreCommonOptions getAllBucketOptions = options.withParentSpan(span);
    return Mono.fromFuture(() -> getAllBuckets(getAllBucketOptions)).map(buckets -> buckets.containsKey(bucketName)).flatMap(bucketExists -> {
        if (!bucketExists) {
            return Mono.error(BucketNotFoundException.forBucket(bucketName));
        }
        return Mono.fromFuture(httpClient.post(pathForBucket(bucketName), options).form(convertSettingsToParams(settings, true)).exec(core).thenApply(response -> null));
    }).then().doOnTerminate(span::end).toFuture();
}
Also used : HttpStatusCodeException.couchbaseResponseStatus(com.couchbase.client.core.error.HttpStatusCodeException.couchbaseResponseStatus) RequestTarget(com.couchbase.client.core.msg.RequestTarget) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) Function(java.util.function.Function) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) HttpStatusCodeException.httpResponseBody(com.couchbase.client.core.error.HttpStatusCodeException.httpResponseBody) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) BucketNotFlushableException(com.couchbase.client.core.error.BucketNotFlushableException) CbCollections.mapOf(com.couchbase.client.core.util.CbCollections.mapOf) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CbTracing(com.couchbase.client.core.cnc.CbTracing) BucketNotFoundException(com.couchbase.client.core.error.BucketNotFoundException) CbThrowables.propagate(com.couchbase.client.core.util.CbThrowables.propagate) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder) Mapper(com.couchbase.client.core.json.Mapper) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) Mono(reactor.core.publisher.Mono) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) CoreHttpResponse(com.couchbase.client.core.endpoint.http.CoreHttpResponse) BucketExistsException(com.couchbase.client.core.error.BucketExistsException) Core(com.couchbase.client.core.Core) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Aggregations

CoreCommonOptions (com.couchbase.client.core.endpoint.http.CoreCommonOptions)6 Map (java.util.Map)6 Core (com.couchbase.client.core.Core)5 Stability (com.couchbase.client.core.annotation.Stability)5 CoreHttpPath.path (com.couchbase.client.core.endpoint.http.CoreHttpPath.path)5 RequestTarget (com.couchbase.client.core.msg.RequestTarget)5 CoreHttpClient (com.couchbase.client.core.endpoint.http.CoreHttpClient)4 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)3 CoreHttpPath (com.couchbase.client.core.endpoint.http.CoreHttpPath)3 CoreHttpRequest (com.couchbase.client.core.endpoint.http.CoreHttpRequest)3 CoreHttpResponse (com.couchbase.client.core.endpoint.http.CoreHttpResponse)3 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 CbCollections.mapOf (com.couchbase.client.core.util.CbCollections.mapOf)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Reactor (com.couchbase.client.core.Reactor)2 CbTracing (com.couchbase.client.core.cnc.CbTracing)2 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 BucketConfig (com.couchbase.client.core.config.BucketConfig)2 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)2 NodeInfo (com.couchbase.client.core.config.NodeInfo)2