Search in sources :

Example 46 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project registry by hortonworks.

the class SchemaRegistryClient method uploadSchemaVersion.

public SchemaIdVersion uploadSchemaVersion(final String schemaBranchName, final String schemaName, final String description, final InputStream schemaVersionInputStream) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
    if (schemaMetadataInfo == null) {
        throw new SchemaNotFoundException("Schema with name " + schemaName + " not found");
    }
    StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", schemaVersionInputStream);
    WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions/upload").queryParam("branch", schemaBranchName);
    MultiPart multipartEntity = new FormDataMultiPart().field("description", description, MediaType.APPLICATION_JSON_TYPE).bodyPart(streamDataBodyPart);
    Entity<MultiPart> multiPartEntity = Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA);
    Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {

        @Override
        public Response run() {
            return target.request().post(multiPartEntity, Response.class);
        }
    });
    return handleSchemaIdVersionResponse(schemaMetadataInfo, response);
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 47 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project kylo by Teradata.

the class AbstractNiFiTemplatesRestClient method create.

@Nonnull
@Override
public TemplateDTO create(@Nullable final String name, @Nonnull final String xml) {
    // Build template body part
    final FormDataBodyPart templatePart = new FormDataBodyPart("template", xml, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    FormDataContentDisposition.FormDataContentDispositionBuilder disposition = FormDataContentDisposition.name(templatePart.getName());
    disposition.fileName((name == null) ? "import_template_" + System.currentTimeMillis() : name);
    templatePart.setFormDataContentDisposition(disposition.build());
    // Combine parts
    MultiPart multiPart = new MultiPart();
    multiPart.bodyPart(templatePart);
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
    // Upload template
    return upload(multiPart);
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) Nonnull(javax.annotation.Nonnull)

Example 48 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project batfish by batfish.

the class BfCoordWorkHelper method getAnswer.

@Nullable
public String getAnswer(String containerName, String baseTestrig, String baseEnv, String deltaTestrig, String deltaEnv, String questionName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_ANSWER);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_TESTRIG_NAME, baseTestrig);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ENV_NAME, baseEnv);
        if (deltaTestrig != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_DELTA_TESTRIG_NAME, deltaTestrig);
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_DELTA_ENV_NAME, deltaEnv);
        }
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_QUESTION_NAME, questionName);
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_ANSWER)) {
            _logger.errorf("answer key not found in: %s\n", jObj);
            return null;
        }
        String answer = jObj.getString(CoordConsts.SVC_KEY_ANSWER);
        return answer;
    } catch (Exception e) {
        _logger.errorf("Exception in getAnswer from %s using (%s, %s)\n", _coordWorkMgr, baseTestrig, questionName);
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) JSONObject(org.codehaus.jettison.json.JSONObject) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Example 49 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project batfish by batfish.

the class BfCoordWorkHelper method delAnalysis.

public boolean delAnalysis(String containerName, String analysisName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_ANALYSIS);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName);
        return postData(webTarget, multiPart) != null;
    } catch (Exception e) {
        _logger.errorf("Exception when deleting analysis to %s using (%s, %s): %s\n", _coordWorkMgr, containerName, analysisName, ExceptionUtils.getStackTrace(e));
        return false;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException)

Example 50 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project batfish by batfish.

the class BfCoordWorkHelper method listContainers.

@Nullable
public String[] listContainers() {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_LIST_CONTAINERS);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_CONTAINER_LIST)) {
            _logger.errorf("container list key not found in: %s\n", jObj);
            return null;
        }
        JSONArray containerArray = jObj.getJSONArray(CoordConsts.SVC_KEY_CONTAINER_LIST);
        String[] containerList = new String[containerArray.length()];
        for (int index = 0; index < containerArray.length(); index++) {
            containerList[index] = containerArray.getString(index);
        }
        return containerList;
    } catch (Exception e) {
        _logger.errorf("exception: ");
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Aggregations

MultiPart (org.glassfish.jersey.media.multipart.MultiPart)64 WebTarget (javax.ws.rs.client.WebTarget)43 ProcessingException (javax.ws.rs.ProcessingException)28 BatfishException (org.batfish.common.BatfishException)28 JSONObject (org.codehaus.jettison.json.JSONObject)20 Test (org.junit.Test)16 Nullable (javax.annotation.Nullable)15 MediaType (javax.ws.rs.core.MediaType)15 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)11 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)11 Response (javax.ws.rs.core.Response)10 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)10 StreamDataBodyPart (org.glassfish.jersey.media.multipart.file.StreamDataBodyPart)8 Client (javax.ws.rs.client.Client)7 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)5 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)3 File (java.io.File)3 Path (javax.ws.rs.Path)3