Search in sources :

Example 6 with Source

use of org.apache.pulsar.client.admin.Source in project pulsar by apache.

the class SourcesImpl method updateSourceAsync.

@Override
public CompletableFuture<Void> updateSourceAsync(SourceConfig sourceConfig, String fileName, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder = put(source.path(sourceConfig.getTenant()).path(sourceConfig.getNamespace()).path(sourceConfig.getName()).getUri().toASCIIString()).addBodyPart(new StringPart("sourceConfig", ObjectMapperFactory.getThreadLocal().writeValueAsString(sourceConfig), MediaType.APPLICATION_JSON));
        UpdateOptionsImpl options = (UpdateOptionsImpl) updateOptions;
        if (options != null) {
            builder.addBodyPart(new StringPart("updateOptions", ObjectMapperFactory.getThreadLocal().writeValueAsString(options), MediaType.APPLICATION_JSON));
        }
        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(source, builder).build()).toCompletableFuture().thenAccept(response -> {
            if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                future.completeExceptionally(getApiException(Response.status(response.getStatusCode()).entity(response.getResponseBody()).build()));
            } else {
                future.complete(null);
            }
        }).exceptionally(throwable -> {
            future.completeExceptionally(getApiException(throwable));
            return null;
        });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) ConnectorDefinition(org.apache.pulsar.common.io.ConnectorDefinition) UpdateOptions(org.apache.pulsar.common.functions.UpdateOptions) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) SourceStatus(org.apache.pulsar.common.policies.data.SourceStatus) CompletableFuture(java.util.concurrent.CompletableFuture) Source(org.apache.pulsar.client.admin.Source) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) MediaType(javax.ws.rs.core.MediaType) Gson(com.google.gson.Gson) InvocationCallback(javax.ws.rs.client.InvocationCallback) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) RequestBuilder(org.asynchttpclient.RequestBuilder) SourceConfig(org.apache.pulsar.common.io.SourceConfig) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Sources(org.apache.pulsar.client.admin.Sources) UpdateOptionsImpl(org.apache.pulsar.common.functions.UpdateOptionsImpl) FilePart(org.asynchttpclient.request.body.multipart.FilePart) Entity(javax.ws.rs.client.Entity) File(java.io.File) Dsl.post(org.asynchttpclient.Dsl.post) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Dsl.put(org.asynchttpclient.Dsl.put) Response(javax.ws.rs.core.Response) Authentication(org.apache.pulsar.client.api.Authentication) StringPart(org.asynchttpclient.request.body.multipart.StringPart) WebTarget(javax.ws.rs.client.WebTarget) CompletableFuture(java.util.concurrent.CompletableFuture) RequestBuilder(org.asynchttpclient.RequestBuilder) StringPart(org.asynchttpclient.request.body.multipart.StringPart) UpdateOptionsImpl(org.apache.pulsar.common.functions.UpdateOptionsImpl) File(java.io.File) FilePart(org.asynchttpclient.request.body.multipart.FilePart) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Aggregations

Gson (com.google.gson.Gson)6 File (java.io.File)6 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Entity (javax.ws.rs.client.Entity)6 InvocationCallback (javax.ws.rs.client.InvocationCallback)6 WebTarget (javax.ws.rs.client.WebTarget)6 GenericType (javax.ws.rs.core.GenericType)6 MediaType (javax.ws.rs.core.MediaType)6 Response (javax.ws.rs.core.Response)6 Slf4j (lombok.extern.slf4j.Slf4j)6 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)6 Source (org.apache.pulsar.client.admin.Source)6 Sources (org.apache.pulsar.client.admin.Sources)6 Authentication (org.apache.pulsar.client.api.Authentication)6 UpdateOptions (org.apache.pulsar.common.functions.UpdateOptions)6 UpdateOptionsImpl (org.apache.pulsar.common.functions.UpdateOptionsImpl)6 ConnectorDefinition (org.apache.pulsar.common.io.ConnectorDefinition)6 SourceConfig (org.apache.pulsar.common.io.SourceConfig)6 SourceStatus (org.apache.pulsar.common.policies.data.SourceStatus)6