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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Aggregations