Search in sources :

Example 51 with MultiPart

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

the class BfCoordWorkHelper method killWork.

public boolean killWork(UUID workId) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_KILL_WORK);
        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_WORKID, workId.toString());
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return false;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_RESULT)) {
            _logger.errorf("result key not found in: %s\n", jObj);
            return false;
        }
        return jObj.getBoolean(CoordConsts.SVC_KEY_RESULT);
    } catch (Exception e) {
        _logger.errorf("exception: %s\n", ExceptionUtils.getStackTrace(e));
        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 52 with MultiPart

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

the class BfCoordWorkHelper method configureTemplate.

@Nullable
public String configureTemplate(String inTemplate, JsonNode exceptions, JsonNode assertion) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CONFIGURE_QUESTION_TEMPLATE);
        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_QUESTION, inTemplate);
        if (exceptions != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_EXCEPTIONS, exceptions.toString());
        }
        if (assertion != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ASSERTION, assertion.toString());
        }
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_QUESTION)) {
            _logger.errorf("question key not found in: %s\n", jObj);
            return null;
        }
        String outTemplate = jObj.getString(CoordConsts.SVC_KEY_QUESTION);
        return outTemplate;
    } catch (Exception e) {
        _logger.errorf("Exception in configureTemplate from %s using (%s, %s, %s)\n", _coordWorkMgr, inTemplate, exceptions, assertion);
        _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 53 with MultiPart

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

the class BfCoordWorkHelper method initContainer.

@Nullable
public String initContainer(@Nullable String containerName, @Nullable String containerPrefix) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_INIT_CONTAINER);
        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);
        } else {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_PREFIX, containerPrefix);
        }
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_CONTAINER_NAME)) {
            _logger.errorf("container name key not found in: %s\n", jObj);
            return null;
        }
        return jObj.getString(CoordConsts.SVC_KEY_CONTAINER_NAME);
    } 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 54 with MultiPart

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

the class BfCoordWorkHelper method getWorkStatus.

@Nullable
public Pair<WorkStatusCode, String> getWorkStatus(UUID workId) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_WORKSTATUS);
        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_WORKID, workId.toString());
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_WORKSTATUS)) {
            _logger.errorf("workstatus key not found in: %s\n", jObj);
            return null;
        }
        WorkStatusCode workStatus = WorkStatusCode.valueOf(jObj.getString(CoordConsts.SVC_KEY_WORKSTATUS));
        if (!jObj.has(CoordConsts.SVC_KEY_TASKSTATUS)) {
            _logger.errorf("taskstatus key not found in: %s\n", jObj);
        }
        String taskStr = jObj.getString(CoordConsts.SVC_KEY_TASKSTATUS);
        return new Pair<>(workStatus, taskStr);
    } 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) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Pair(org.batfish.common.Pair) Nullable(javax.annotation.Nullable)

Example 55 with MultiPart

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

the class BfCoordWorkHelper method getObject.

@Nullable
public String getObject(String containerName, String testrigName, String objectName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_OBJECT);
        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_VERSION, Version.getVersion());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_TESTRIG_NAME, testrigName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_OBJECT_NAME, objectName);
        Response response = webTarget.request(MediaType.APPLICATION_OCTET_STREAM).post(Entity.entity(multiPart, multiPart.getMediaType()));
        _logger.debug(response.getStatus() + " " + response.getStatusInfo() + " " + response + "\n");
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            _logger.debugf("GetObject: Did not get an OK response for %s -> %s->%s\n", containerName, testrigName, objectName);
            return null;
        }
        // see if we have a filename header
        // String outFileStr = objectName;
        // 
        // MultivaluedMap<String, String> headers =
        // response.getStringHeaders();
        // 
        // if (headers.containsKey(CoordConsts.SVC_FILENAME_HDR)) {
        // String value = headers.getFirst(CoordConsts.SVC_FILENAME_HDR);
        // if (value != null && !value.equals("")) {
        // outFileStr = value;
        // }
        // }
        File inFile = response.readEntity(File.class);
        File tmpOutFile = Files.createTempFile("batfish_client", null).toFile();
        tmpOutFile.deleteOnExit();
        FileUtils.copyFile(inFile, tmpOutFile);
        if (!inFile.delete()) {
            throw new BatfishException("Failed to delete temporary file: " + inFile.getAbsolutePath());
        }
        return tmpOutFile.getAbsolutePath();
    } catch (Exception e) {
        _logger.errorf("Exception in getObject from %s using (%s, %s)\n", _coordWorkMgr, testrigName, objectName);
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : Response(javax.ws.rs.core.Response) BatfishException(org.batfish.common.BatfishException) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) WebTarget(javax.ws.rs.client.WebTarget) File(java.io.File) 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