Search in sources :

Example 61 with MultiPart

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

the class BfCoordWorkHelper method listEnvironments.

@Nullable
public String[] listEnvironments(String containerName, String testrigName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_LIST_ENVIRONMENTS);
        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);
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_ENVIRONMENT_LIST)) {
            _logger.errorf("environment list key not found in: %s\n", jObj);
            return null;
        }
        JSONArray environmentArray = jObj.getJSONArray(CoordConsts.SVC_KEY_ENVIRONMENT_LIST);
        String[] environmentList = new String[environmentArray.length()];
        for (int index = 0; index < environmentArray.length(); index++) {
            environmentList[index] = environmentArray.getString(index);
        }
        return environmentList;
    } 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)

Example 62 with MultiPart

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

the class BfCoordWorkHelper method getAnalysisAnswers.

// private JSONObject getJsonResponse(WebTarget webTarget) throws Exception {
// try {
// Response response = webTarget.request(MediaType.APPLICATION_JSON)
// .get();
// 
// _logger.info(response.getStatus() + " " + response.getStatusInfo()
// + " " + response + "\n");
// 
// if (response.getStatus() != Response.Status.OK.getStatusCode()) {
// _logger.errorf("Did not get an OK response from: %s\n", webTarget);
// return null;
// }
// 
// String sobj = response.readEntity(String.class);
// JSONArray array = new JSONArray(sobj);
// 
// _logger.infof("response: %s [%s] [%s]\n", array.toString(),
// array.get(0), array.get(1));
// 
// if (!array.get(0).equals(CoordConsts.SVC_SUCCESS_KEY)) {
// _logger.errorf("did not get success: %s %s\n", array.get(0),
// array.get(1));
// return null;
// }
// 
// return new JSONObject(array.get(1).toString());
// }
// catch (ProcessingException e) {
// if (e.getMessage().contains("ConnectException")) {
// _logger.errorf("unable to connect to coordinator at %s\n",
// _coordWorkMgr);
// return null;
// }
// if (e.getMessage().contains("SSLHandshakeException")) {
// _logger
// .errorf("SSL handshake exception while connecting to coordinator (Is the
// coordinator using SSL and using keys that you trust?)\n");
// return null;
// }
// if (e.getMessage().contains("SocketException: Unexpected end of file")) {
// _logger
// .errorf("SocketException while connecting to coordinator. (Are you using
// SSL?)\n");
// return null;
// }
// throw e;
// }
// }
@Nullable
public String getAnalysisAnswers(String containerName, String baseTestrig, String baseEnvironment, String deltaTestrig, String deltaEnvironment, String analysisName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_ANALYSIS_ANSWERS);
        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, baseEnvironment);
        if (deltaTestrig != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_DELTA_TESTRIG_NAME, deltaTestrig);
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_DELTA_ENV_NAME, deltaEnvironment);
        }
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName);
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_ANSWERS)) {
            _logger.errorf("answer key not found in: %s\n", jObj);
            return null;
        }
        String answer = jObj.getString(CoordConsts.SVC_KEY_ANSWERS);
        return answer;
    } catch (Exception e) {
        _logger.errorf("Exception in getAnswer from %s using (%s, %s)\n", _coordWorkMgr, baseTestrig, analysisName);
        _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 63 with MultiPart

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

the class BfCoordWorkHelper method listTestrigs.

@Nullable
public JSONArray listTestrigs(String containerName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_LIST_TESTRIGS);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        if (containerName != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        }
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_TESTRIG_LIST)) {
            _logger.errorf("testrig key not found in: %s\n", jObj);
            return null;
        }
        JSONArray testrigArray = jObj.getJSONArray(CoordConsts.SVC_KEY_TESTRIG_LIST);
        return testrigArray;
    } 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)

Example 64 with MultiPart

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

the class SaltConnector method upload.

private Response upload(String endpoint, Iterable<String> targets, String path, String fileName, byte[] content) throws IOException {
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(content)) {
        StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", inputStream, fileName);
        MultiPart multiPart = new FormDataMultiPart().field("path", path).field("targets", String.join(",", targets)).bodyPart(streamDataBodyPart);
        MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
        contentType = Boundary.addBoundary(contentType);
        String signature = PkiUtil.generateSignature(signatureKey, content);
        return saltTarget.path(endpoint).request().header(SIGN_HEADER, signature).post(Entity.entity(multiPart, contentType));
    }
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) ByteArrayInputStream(java.io.ByteArrayInputStream) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) MediaType(javax.ws.rs.core.MediaType)

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