use of org.apache.http.entity.mime.MultipartEntity in project scheduling by ow2-proactive.
the class RestSchedulerPushPullFileTest method testIt.
public void testIt(String spaceName, String spacePath, String destPath, boolean encode) throws Exception {
File testPushFile = RestFuncTHelper.getDefaultJobXmlfile();
// you can test pushing pulling a big file :
// testPushFile = new File("path_to_a_big_file");
File destFile = new File(new File(spacePath, destPath), testPushFile.getName());
if (destFile.exists()) {
destFile.delete();
}
// PUSHING THE FILE
String pushfileUrl = getResourceUrl("dataspace/" + spaceName + "/" + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));
// either we encode or we test human readable path (with no special character inside)
HttpPost reqPush = new HttpPost(pushfileUrl);
setSessionHeader(reqPush);
// we push a xml job as a simple test
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("fileName", new StringBody(testPushFile.getName()));
multipartEntity.addPart("fileContent", new InputStreamBody(FileUtils.openInputStream(testPushFile), MediaType.APPLICATION_OCTET_STREAM, null));
reqPush.setEntity(multipartEntity);
HttpResponse response = executeUriRequest(reqPush);
System.out.println(response.getStatusLine());
assertHttpStatusOK(response);
Assert.assertTrue(destFile + " exists for " + spaceName, destFile.exists());
Assert.assertTrue("Original file and result are equals for " + spaceName, FileUtils.contentEquals(testPushFile, destFile));
// LISTING THE TARGET DIRECTORY
String pullListUrl = getResourceUrl("dataspace/" + spaceName + "/" + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));
HttpGet reqPullList = new HttpGet(pullListUrl);
setSessionHeader(reqPullList);
HttpResponse response2 = executeUriRequest(reqPullList);
System.out.println(response2.getStatusLine());
assertHttpStatusOK(response2);
InputStream is = response2.getEntity().getContent();
List<String> lines = IOUtils.readLines(is);
HashSet<String> content = new HashSet<>(lines);
System.out.println(lines);
Assert.assertTrue("Pushed file correctly listed", content.contains(testPushFile.getName()));
// PULLING THE FILE
String pullfileUrl = getResourceUrl("dataspace/" + spaceName + "/" + (encode ? URLEncoder.encode(destPath + "/" + testPushFile.getName(), "UTF-8") : destPath.replace("\\", "/") + "/" + testPushFile.getName()));
HttpGet reqPull = new HttpGet(pullfileUrl);
setSessionHeader(reqPull);
HttpResponse response3 = executeUriRequest(reqPull);
System.out.println(response3.getStatusLine());
assertHttpStatusOK(response3);
InputStream is2 = response3.getEntity().getContent();
File answerFile = tmpFolder.newFile();
FileUtils.copyInputStreamToFile(is2, answerFile);
Assert.assertTrue("Original file and result are equals for " + spaceName, FileUtils.contentEquals(answerFile, testPushFile));
// DELETING THE HIERARCHY
String rootPath = destPath.substring(0, destPath.contains("/") ? destPath.indexOf("/") : destPath.length());
String deleteUrl = getResourceUrl("dataspace/" + spaceName + "/" + (encode ? URLEncoder.encode(rootPath, "UTF-8") : rootPath.replace("\\", "/")));
HttpDelete reqDelete = new HttpDelete(deleteUrl);
setSessionHeader(reqDelete);
HttpResponse response4 = executeUriRequest(reqDelete);
System.out.println(response4.getStatusLine());
assertHttpStatusOK(response4);
Assert.assertTrue(destFile + " still exist", !destFile.exists());
}
use of org.apache.http.entity.mime.MultipartEntity in project scheduling by ow2-proactive.
the class LoginWithCredentialsCommand method login.
@Override
protected String login(ApplicationContext currentContext) throws CLIException {
File credentials = new File(pathname);
if (!credentials.exists()) {
throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("File does not exist: %s", credentials.getAbsolutePath()));
}
if (warn) {
writeLine(currentContext, "Using the default credentials file: %s", credentials.getAbsolutePath());
}
HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
MultipartEntity entity = new MultipartEntity();
entity.addPart("credential", new ByteArrayBody(FileUtility.byteArray(credentials), APPLICATION_OCTET_STREAM.getMimeType()));
request.setEntity(entity);
HttpResponseWrapper response = execute(request, currentContext);
if (statusCode(OK) == statusCode(response)) {
return StringUtility.responseAsString(response).trim();
} else {
handleError("An error occurred while logging: ", response, currentContext);
throw new CLIException(REASON_OTHER, "An error occurred while logging.");
}
}
use of org.apache.http.entity.mime.MultipartEntity in project cxf by apache.
the class Client method uploadToCatalog.
private static void uploadToCatalog(final String url, final CloseableHttpClient httpClient, final String filename) throws IOException {
System.out.println("Sent HTTP POST request to upload the file into catalog: " + filename);
final HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
byte[] bytes = IOUtils.readBytesFromStream(Client.class.getResourceAsStream("/" + filename));
entity.addPart(filename, new ByteArrayBody(bytes, filename));
post.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 201) {
System.out.println(response.getFirstHeader("Location"));
} else if (response.getStatusLine().getStatusCode() == 409) {
System.out.println("Document already exists: " + filename);
}
} finally {
post.releaseConnection();
}
}
use of org.apache.http.entity.mime.MultipartEntity in project tmdm-studio-se by Talend.
the class HttpClientUtil method createUploadRequest.
private static HttpUriRequest createUploadRequest(String URL, String userName, String localFilename, String filename, String imageCatalog, HashMap<String, String> picturePathMap) {
HttpPost request = new HttpPost(URL);
MultipartEntity entity = new MultipartEntity();
if (!Messages.Util_24.equalsIgnoreCase(localFilename)) {
File file = new File(localFilename);
if (file.exists()) {
// $NON-NLS-1$
entity.addPart("imageFile", new FileBody(file));
}
}
if (imageCatalog != null) {
// $NON-NLS-1$
entity.addPart("catalogName", StringBody.create(imageCatalog, STRING_CONTENT_TYPE, null));
}
if (filename != null) {
int pos = filename.lastIndexOf('.');
if (pos != -1) {
filename = filename.substring(0, pos);
}
// $NON-NLS-1$
entity.addPart("fileName", StringBody.create(filename, STRING_CONTENT_TYPE, null));
}
request.setEntity(entity);
addStudioToken(request, userName);
return request;
}
use of org.apache.http.entity.mime.MultipartEntity in project liferay-ide by liferay.
the class ServerManagerConnection method installApplication.
public Object installApplication(String absolutePath, String appName, IProgressMonitor submon) throws APIException {
try {
FileBody fileBody = new FileBody(new File(absolutePath));
MultipartEntity entity = new MultipartEntity();
// $NON-NLS-1$
entity.addPart("deployWar", fileBody);
HttpPost httpPost = new HttpPost();
httpPost.setEntity(entity);
Object response = httpJSONAPI(httpPost, getDeployURI(appName));
if (response instanceof JSONObject) {
JSONObject json = (JSONObject) response;
if (isSuccess(json)) {
// $NON-NLS-1$
System.out.println("installApplication: Sucess.\n\n");
} else {
if (isError(json)) {
// $NON-NLS-1$
return json.getString("error");
} else {
// $NON-NLS-1$
return "installApplication error " + getDeployURI(appName);
}
}
}
httpPost.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return null;
}
Aggregations