Search in sources :

Example 56 with MultiPart

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

the class BfCoordWorkHelper method delEnvironment.

public boolean delEnvironment(String containerName, String testrigName, String envName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_ENVIRONMENT);
        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, testrigName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ENV_NAME, envName);
        JSONObject jObj = postData(webTarget, multiPart);
        return jObj != null;
    } catch (Exception e) {
        _logger.errorf("exception: ");
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return false;
    }
}
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)

Example 57 with MultiPart

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

the class BfCoordWorkHelper method uploadEnvironment.

public boolean uploadEnvironment(String containerName, String testrigName, String baseEnvName, String envName, String zipfileName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_UPLOAD_ENV);
        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, testrigName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_BASE_ENV_NAME, baseEnvName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ENV_NAME, envName);
        addFileMultiPart(multiPart, CoordConsts.SVC_KEY_ZIPFILE, zipfileName);
        return postData(webTarget, multiPart) != null;
    } catch (Exception e) {
        if (e.getMessage().contains("FileNotFoundException")) {
            _logger.errorf("File not found: %s\n", zipfileName);
        } else {
            _logger.errorf("Exception when uploading environment to %s using (%s, %s, %s): %s\n", _coordWorkMgr, testrigName, envName, zipfileName, 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 58 with MultiPart

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

the class BfCoordWorkHelper method checkApiKey.

@Nullable
public String checkApiKey() {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CHECK_API_KEY);
        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;
        }
        return Boolean.toString(jObj.getBoolean(CoordConsts.SVC_KEY_API_KEY));
    } 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) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Example 59 with MultiPart

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

the class BfCoordWorkHelper method listAnalyses.

@Nullable
public JSONObject listAnalyses(String containerName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_LIST_ANALYSES);
        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);
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_ANALYSIS_LIST)) {
            _logger.errorf("analysis list key not found in: %s\n", jObj);
            return null;
        }
        return jObj.getJSONObject(CoordConsts.SVC_KEY_ANALYSIS_LIST);
    } 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) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Example 60 with MultiPart

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

the class BfCoordWorkHelper method getQuestionTemplates.

/**
 * Gets the questions configured at the coordinator
 *
 * @return JSON Object containing question keys and question content as values null if there is
 *     any failure
 */
@Nullable
JSONObject getQuestionTemplates() {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_QUESTION_TEMPLATES);
        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_QUESTION_LIST)) {
            _logger.errorf("question list key not found in: %s\n", jObj);
            return null;
        }
        return jObj.getJSONObject(CoordConsts.SVC_KEY_QUESTION_LIST);
    } catch (Exception e) {
        _logger.errorf("Exception in getQuestionTemplates from %s\n", _coordWorkMgr);
        _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)

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