Search in sources :

Example 36 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project pulsar by apache.

the class FunctionsImpl method triggerFunctionAsync.

@Override
public CompletableFuture<String> triggerFunctionAsync(String tenant, String namespace, String function, String topic, String triggerValue, String triggerFile) {
    final FormDataMultiPart mp = new FormDataMultiPart();
    if (triggerFile != null) {
        mp.bodyPart(new FileDataBodyPart("dataStream", new File(triggerFile), MediaType.APPLICATION_OCTET_STREAM_TYPE));
    }
    if (triggerValue != null) {
        mp.bodyPart(new FormDataBodyPart("data", triggerValue, MediaType.TEXT_PLAIN_TYPE));
    }
    if (topic != null && !topic.isEmpty()) {
        mp.bodyPart(new FormDataBodyPart("topic", topic, MediaType.TEXT_PLAIN_TYPE));
    }
    WebTarget path = functions.path(tenant).path(namespace).path(function).path("trigger");
    final CompletableFuture<String> future = new CompletableFuture<>();
    try {
        request(path).async().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), new InvocationCallback<String>() {

            @Override
            public void completed(String response) {
                future.complete(response);
            }

            @Override
            public void failed(Throwable throwable) {
                log.warn("[{}] Failed to perform http post request: {}", path.getUri(), throwable.getMessage());
                future.completeExceptionally(getApiException(throwable.getCause()));
            }
        });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) File(java.io.File) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 37 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project pulsar by apache.

the class FunctionsImpl method updateFunctionWithUrlAsync.

@Override
public CompletableFuture<Void> updateFunctionWithUrlAsync(FunctionConfig functionConfig, String pkgUrl, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        final FormDataMultiPart mp = new FormDataMultiPart();
        mp.bodyPart(new FormDataBodyPart("url", pkgUrl, MediaType.TEXT_PLAIN_TYPE));
        mp.bodyPart(new FormDataBodyPart("functionConfig", ObjectMapperFactory.getThreadLocal().writeValueAsString(functionConfig), MediaType.APPLICATION_JSON_TYPE));
        UpdateOptionsImpl options = (UpdateOptionsImpl) updateOptions;
        if (options != null) {
            mp.bodyPart(new FormDataBodyPart("updateOptions", ObjectMapperFactory.getThreadLocal().writeValueAsString(options), MediaType.APPLICATION_JSON_TYPE));
        }
        WebTarget path = functions.path(functionConfig.getTenant()).path(functionConfig.getNamespace()).path(functionConfig.getName());
        return asyncPutRequest(path, Entity.entity(mp, MediaType.MULTIPART_FORM_DATA));
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) UpdateOptionsImpl(org.apache.pulsar.common.functions.UpdateOptionsImpl) WebTarget(javax.ws.rs.client.WebTarget) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException)

Example 38 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project pulsar by apache.

the class SinksImpl method createSinkWithUrlAsync.

@Override
public CompletableFuture<Void> createSinkWithUrlAsync(SinkConfig sinkConfig, String pkgUrl) {
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart("url", pkgUrl, MediaType.TEXT_PLAIN_TYPE));
    mp.bodyPart(new FormDataBodyPart("sinkConfig", new Gson().toJson(sinkConfig), MediaType.APPLICATION_JSON_TYPE));
    CompletableFuture<Void> validationFuture = new CompletableFuture<>();
    if (!validateSinkName(sinkConfig.getTenant(), sinkConfig.getNamespace(), sinkConfig.getName(), validationFuture)) {
        return validationFuture;
    }
    WebTarget path = sink.path(sinkConfig.getTenant()).path(sinkConfig.getNamespace()).path(sinkConfig.getName());
    return asyncPostRequest(path, Entity.entity(mp, MediaType.MULTIPART_FORM_DATA));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Gson(com.google.gson.Gson) WebTarget(javax.ws.rs.client.WebTarget)

Example 39 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project pulsar by apache.

the class SourcesImpl method createSourceWithUrlAsync.

@Override
public CompletableFuture<Void> createSourceWithUrlAsync(SourceConfig sourceConfig, String pkgUrl) {
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart("url", pkgUrl, MediaType.TEXT_PLAIN_TYPE));
    mp.bodyPart(new FormDataBodyPart("sourceConfig", new Gson().toJson(sourceConfig), MediaType.APPLICATION_JSON_TYPE));
    WebTarget path = source.path(sourceConfig.getTenant()).path(sourceConfig.getNamespace()).path(sourceConfig.getName());
    return asyncPostRequest(path, Entity.entity(mp, MediaType.MULTIPART_FORM_DATA));
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Gson(com.google.gson.Gson) WebTarget(javax.ws.rs.client.WebTarget)

Example 40 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project pulsar by apache.

the class SourcesImpl method updateSourceWithUrlAsync.

@Override
public CompletableFuture<Void> updateSourceWithUrlAsync(SourceConfig sourceConfig, String pkgUrl, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        final FormDataMultiPart mp = new FormDataMultiPart();
        mp.bodyPart(new FormDataBodyPart("url", pkgUrl, MediaType.TEXT_PLAIN_TYPE));
        mp.bodyPart(new FormDataBodyPart("sourceConfig", new Gson().toJson(sourceConfig), MediaType.APPLICATION_JSON_TYPE));
        UpdateOptionsImpl options = (UpdateOptionsImpl) updateOptions;
        if (options != null) {
            mp.bodyPart(new FormDataBodyPart("updateOptions", ObjectMapperFactory.getThreadLocal().writeValueAsString(options), MediaType.APPLICATION_JSON_TYPE));
        }
        WebTarget path = source.path(sourceConfig.getTenant()).path(sourceConfig.getNamespace()).path(sourceConfig.getName());
        return asyncPutRequest(path, Entity.entity(mp, MediaType.MULTIPART_FORM_DATA));
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Gson(com.google.gson.Gson) UpdateOptionsImpl(org.apache.pulsar.common.functions.UpdateOptionsImpl) WebTarget(javax.ws.rs.client.WebTarget) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Aggregations

FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)179 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)68 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)65 Response (javax.ws.rs.core.Response)63 Test (org.junit.Test)55 File (java.io.File)50 JerseyTest (org.glassfish.jersey.test.JerseyTest)48 Test (org.junit.jupiter.api.Test)46 WebTarget (javax.ws.rs.client.WebTarget)40 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)32 MediaType (javax.ws.rs.core.MediaType)30 List (java.util.List)27 ResponseFormat (org.openecomp.sdc.exception.ResponseFormat)22 Invocation (javax.ws.rs.client.Invocation)20 Collections.emptyList (java.util.Collections.emptyList)19 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)18 Gson (com.google.gson.Gson)16 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)16 CompletableFuture (java.util.concurrent.CompletableFuture)15 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)15