Search in sources :

Example 1 with NotFoundException

use of org.apache.pulsar.client.api.PulsarClientException.NotFoundException in project pulsar by apache.

the class HttpClient method get.

public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    try {
        URI hostUri = serviceNameResolver.resolveHostUri();
        String requestUrl = new URL(hostUri.toURL(), path).toString();
        String remoteHostName = hostUri.getHost();
        AuthenticationDataProvider authData = authentication.getAuthData(remoteHostName);
        CompletableFuture<Map<String, String>> authFuture = new CompletableFuture<>();
        // bring a authenticationStage for sasl auth.
        if (authData.hasDataForHttp()) {
            authentication.authenticationStage(requestUrl, authData, null, authFuture);
        } else {
            authFuture.complete(null);
        }
        // auth complete, do real request
        authFuture.whenComplete((respHeaders, ex) -> {
            if (ex != null) {
                log.warn("[{}] Failed to perform http request at authentication stage: {}", requestUrl, ex.getMessage());
                future.completeExceptionally(new PulsarClientException(ex));
                return;
            }
            // auth complete, use a new builder
            BoundRequestBuilder builder = httpClient.prepareGet(requestUrl).setHeader("Accept", "application/json");
            if (authData.hasDataForHttp()) {
                Set<Entry<String, String>> headers;
                try {
                    headers = authentication.newRequestHeader(requestUrl, authData, respHeaders);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get headers: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                    return;
                }
                if (headers != null) {
                    headers.forEach(entry -> builder.addHeader(entry.getKey(), entry.getValue()));
                }
            }
            builder.execute().toCompletableFuture().whenComplete((response2, t) -> {
                if (t != null) {
                    log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
                    future.completeExceptionally(new PulsarClientException(t));
                    return;
                }
                // request not success
                if (response2.getStatusCode() != HttpURLConnection.HTTP_OK) {
                    log.warn("[{}] HTTP get request failed: {}", requestUrl, response2.getStatusText());
                    Exception e;
                    if (response2.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                        e = new NotFoundException("Not found: " + response2.getStatusText());
                    } else {
                        e = new PulsarClientException("HTTP get request failed: " + response2.getStatusText());
                    }
                    future.completeExceptionally(e);
                    return;
                }
                try {
                    T data = ObjectMapperFactory.getThreadLocal().readValue(response2.getResponseBodyAsBytes(), clazz);
                    future.complete(data);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                }
            });
        });
    } catch (Exception e) {
        log.warn("[{}]PulsarClientImpl: {}", path, e.getMessage());
        if (e instanceof PulsarClientException) {
            future.completeExceptionally(e);
        } else {
            future.completeExceptionally(new PulsarClientException(e));
        }
    }
    return future;
}
Also used : AuthenticationDataProvider(org.apache.pulsar.client.api.AuthenticationDataProvider) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) URI(java.net.URI) URL(java.net.URL) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Entry(java.util.Map.Entry) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Map(java.util.Map)

Example 2 with NotFoundException

use of org.apache.pulsar.client.api.PulsarClientException.NotFoundException in project incubator-pulsar by apache.

the class HttpLookupService method getSchema.

@Override
public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, byte[] version) {
    CompletableFuture<Optional<SchemaInfo>> future = new CompletableFuture<>();
    String schemaName = topicName.getSchemaName();
    String path = String.format("admin/v2/schemas/%s/schema", schemaName);
    if (version != null) {
        if (version.length == 0) {
            future.completeExceptionally(new SchemaSerializationException("Empty schema version"));
            return future;
        }
        path = String.format("admin/v2/schemas/%s/schema/%s", schemaName, ByteBuffer.wrap(version).getLong());
    }
    httpClient.get(path, GetSchemaResponse.class).thenAccept(response -> {
        if (response.getType() == SchemaType.KEY_VALUE) {
            try {
                SchemaData data = SchemaData.builder().data(SchemaUtils.convertKeyValueDataStringToSchemaInfoSchema(response.getData().getBytes(StandardCharsets.UTF_8))).type(response.getType()).props(response.getProperties()).build();
                future.complete(Optional.of(SchemaInfoUtil.newSchemaInfo(schemaName, data)));
            } catch (IOException err) {
                future.completeExceptionally(err);
            }
        } else {
            future.complete(Optional.of(SchemaInfoUtil.newSchemaInfo(schemaName, response)));
        }
    }).exceptionally(ex -> {
        if (ex.getCause() instanceof NotFoundException) {
            future.complete(Optional.empty());
        } else {
            log.warn("Failed to get schema for topic {} version {}", topicName, version != null ? Base64.getEncoder().encodeToString(version) : null, ex.getCause());
            future.completeExceptionally(ex);
        }
        return null;
    });
    return future;
}
Also used : Arrays(java.util.Arrays) TopicName(org.apache.pulsar.common.naming.TopicName) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) ArrayList(java.util.ArrayList) GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) Pair(org.apache.commons.lang3.tuple.Pair) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) SchemaInfoUtil(org.apache.pulsar.client.impl.schema.SchemaInfoUtil) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) LookupData(org.apache.pulsar.common.lookup.data.LookupData) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Mode(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode) Base64(java.util.Base64) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) SchemaUtils(org.apache.pulsar.client.impl.schema.SchemaUtils) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) Codec(org.apache.pulsar.common.util.Codec) Optional(java.util.Optional) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) IOException(java.io.IOException)

Example 3 with NotFoundException

use of org.apache.pulsar.client.api.PulsarClientException.NotFoundException in project incubator-pulsar by apache.

the class HttpClient method get.

public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    try {
        URI hostUri = serviceNameResolver.resolveHostUri();
        String requestUrl = new URL(hostUri.toURL(), path).toString();
        String remoteHostName = hostUri.getHost();
        AuthenticationDataProvider authData = authentication.getAuthData(remoteHostName);
        CompletableFuture<Map<String, String>> authFuture = new CompletableFuture<>();
        // bring a authenticationStage for sasl auth.
        if (authData.hasDataForHttp()) {
            authentication.authenticationStage(requestUrl, authData, null, authFuture);
        } else {
            authFuture.complete(null);
        }
        // auth complete, do real request
        authFuture.whenComplete((respHeaders, ex) -> {
            if (ex != null) {
                log.warn("[{}] Failed to perform http request at authentication stage: {}", requestUrl, ex.getMessage());
                future.completeExceptionally(new PulsarClientException(ex));
                return;
            }
            // auth complete, use a new builder
            BoundRequestBuilder builder = httpClient.prepareGet(requestUrl).setHeader("Accept", "application/json");
            if (authData.hasDataForHttp()) {
                Set<Entry<String, String>> headers;
                try {
                    headers = authentication.newRequestHeader(requestUrl, authData, respHeaders);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get headers: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                    return;
                }
                if (headers != null) {
                    headers.forEach(entry -> builder.addHeader(entry.getKey(), entry.getValue()));
                }
            }
            builder.execute().toCompletableFuture().whenComplete((response2, t) -> {
                if (t != null) {
                    log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
                    future.completeExceptionally(new PulsarClientException(t));
                    return;
                }
                // request not success
                if (response2.getStatusCode() != HttpURLConnection.HTTP_OK) {
                    log.warn("[{}] HTTP get request failed: {}", requestUrl, response2.getStatusText());
                    Exception e;
                    if (response2.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                        e = new NotFoundException("Not found: " + response2.getStatusText());
                    } else {
                        e = new PulsarClientException("HTTP get request failed: " + response2.getStatusText());
                    }
                    future.completeExceptionally(e);
                    return;
                }
                try {
                    T data = ObjectMapperFactory.getThreadLocal().readValue(response2.getResponseBodyAsBytes(), clazz);
                    future.complete(data);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                }
            });
        });
    } catch (Exception e) {
        log.warn("[{}]PulsarClientImpl: {}", path, e.getMessage());
        if (e instanceof PulsarClientException) {
            future.completeExceptionally(e);
        } else {
            future.completeExceptionally(new PulsarClientException(e));
        }
    }
    return future;
}
Also used : AuthenticationDataProvider(org.apache.pulsar.client.api.AuthenticationDataProvider) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) URI(java.net.URI) URL(java.net.URL) GeneralSecurityException(java.security.GeneralSecurityException) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Entry(java.util.Map.Entry) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Map(java.util.Map)

Example 4 with NotFoundException

use of org.apache.pulsar.client.api.PulsarClientException.NotFoundException in project pulsar by yahoo.

the class HttpClient method get.

public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    try {
        URI hostUri = serviceNameResolver.resolveHostUri();
        String requestUrl = new URL(hostUri.toURL(), path).toString();
        String remoteHostName = hostUri.getHost();
        AuthenticationDataProvider authData = authentication.getAuthData(remoteHostName);
        CompletableFuture<Map<String, String>> authFuture = new CompletableFuture<>();
        // bring a authenticationStage for sasl auth.
        if (authData.hasDataForHttp()) {
            authentication.authenticationStage(requestUrl, authData, null, authFuture);
        } else {
            authFuture.complete(null);
        }
        // auth complete, do real request
        authFuture.whenComplete((respHeaders, ex) -> {
            if (ex != null) {
                log.warn("[{}] Failed to perform http request at authentication stage: {}", requestUrl, ex.getMessage());
                future.completeExceptionally(new PulsarClientException(ex));
                return;
            }
            // auth complete, use a new builder
            BoundRequestBuilder builder = httpClient.prepareGet(requestUrl).setHeader("Accept", "application/json");
            if (authData.hasDataForHttp()) {
                Set<Entry<String, String>> headers;
                try {
                    headers = authentication.newRequestHeader(requestUrl, authData, respHeaders);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get headers: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                    return;
                }
                if (headers != null) {
                    headers.forEach(entry -> builder.addHeader(entry.getKey(), entry.getValue()));
                }
            }
            builder.execute().toCompletableFuture().whenComplete((response2, t) -> {
                if (t != null) {
                    log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
                    future.completeExceptionally(new PulsarClientException(t));
                    return;
                }
                // request not success
                if (response2.getStatusCode() != HttpURLConnection.HTTP_OK) {
                    log.warn("[{}] HTTP get request failed: {}", requestUrl, response2.getStatusText());
                    Exception e;
                    if (response2.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                        e = new NotFoundException("Not found: " + response2.getStatusText());
                    } else {
                        e = new PulsarClientException("HTTP get request failed: " + response2.getStatusText());
                    }
                    future.completeExceptionally(e);
                    return;
                }
                try {
                    T data = ObjectMapperFactory.getThreadLocal().readValue(response2.getResponseBodyAsBytes(), clazz);
                    future.complete(data);
                } catch (Exception e) {
                    log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
                    future.completeExceptionally(new PulsarClientException(e));
                }
            });
        });
    } catch (Exception e) {
        log.warn("[{}]PulsarClientImpl: {}", path, e.getMessage());
        if (e instanceof PulsarClientException) {
            future.completeExceptionally(e);
        } else {
            future.completeExceptionally(new PulsarClientException(e));
        }
    }
    return future;
}
Also used : AuthenticationDataProvider(org.apache.pulsar.client.api.AuthenticationDataProvider) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) URI(java.net.URI) URL(java.net.URL) GeneralSecurityException(java.security.GeneralSecurityException) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Entry(java.util.Map.Entry) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Map(java.util.Map)

Example 5 with NotFoundException

use of org.apache.pulsar.client.api.PulsarClientException.NotFoundException in project pulsar by yahoo.

the class HttpLookupService method getSchema.

@Override
public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, byte[] version) {
    CompletableFuture<Optional<SchemaInfo>> future = new CompletableFuture<>();
    String schemaName = topicName.getSchemaName();
    String path = String.format("admin/v2/schemas/%s/schema", schemaName);
    if (version != null) {
        if (version.length == 0) {
            future.completeExceptionally(new SchemaSerializationException("Empty schema version"));
            return future;
        }
        path = String.format("admin/v2/schemas/%s/schema/%s", schemaName, ByteBuffer.wrap(version).getLong());
    }
    httpClient.get(path, GetSchemaResponse.class).thenAccept(response -> {
        if (response.getType() == SchemaType.KEY_VALUE) {
            try {
                SchemaData data = SchemaData.builder().data(SchemaUtils.convertKeyValueDataStringToSchemaInfoSchema(response.getData().getBytes(StandardCharsets.UTF_8))).type(response.getType()).props(response.getProperties()).build();
                future.complete(Optional.of(SchemaInfoUtil.newSchemaInfo(schemaName, data)));
            } catch (IOException err) {
                future.completeExceptionally(err);
            }
        } else {
            future.complete(Optional.of(SchemaInfoUtil.newSchemaInfo(schemaName, response)));
        }
    }).exceptionally(ex -> {
        if (ex.getCause() instanceof NotFoundException) {
            future.complete(Optional.empty());
        } else {
            log.warn("Failed to get schema for topic {} version {}", topicName, version != null ? Base64.getEncoder().encodeToString(version) : null, ex.getCause());
            future.completeExceptionally(ex);
        }
        return null;
    });
    return future;
}
Also used : Arrays(java.util.Arrays) TopicName(org.apache.pulsar.common.naming.TopicName) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) ArrayList(java.util.ArrayList) GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) Pair(org.apache.commons.lang3.tuple.Pair) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) SchemaInfoUtil(org.apache.pulsar.client.impl.schema.SchemaInfoUtil) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) LookupData(org.apache.pulsar.common.lookup.data.LookupData) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Mode(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode) Base64(java.util.Base64) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) SchemaUtils(org.apache.pulsar.client.impl.schema.SchemaUtils) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) Codec(org.apache.pulsar.common.util.Codec) Optional(java.util.Optional) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) NotFoundException(org.apache.pulsar.client.api.PulsarClientException.NotFoundException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 URI (java.net.URI)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 NotFoundException (org.apache.pulsar.client.api.PulsarClientException.NotFoundException)6 EventLoopGroup (io.netty.channel.EventLoopGroup)3 InetSocketAddress (java.net.InetSocketAddress)3 URL (java.net.URL)3 ByteBuffer (java.nio.ByteBuffer)3 StandardCharsets (java.nio.charset.StandardCharsets)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Base64 (java.util.Base64)3 List (java.util.List)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 Optional (java.util.Optional)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Pair (org.apache.commons.lang3.tuple.Pair)3 AuthenticationDataProvider (org.apache.pulsar.client.api.AuthenticationDataProvider)3