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