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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations