Search in sources :

Example 41 with MultipartEntity

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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) File(java.io.File) HashSet(java.util.HashSet)

Example 42 with MultipartEntity

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.");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) File(java.io.File)

Example 43 with MultipartEntity

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();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient)

Example 44 with MultipartEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) File(java.io.File)

Example 45 with MultipartEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) JSONObject(org.json.JSONObject) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) JSONObject(org.json.JSONObject) File(java.io.File) APIException(com.liferay.ide.core.remote.APIException) JSONException(org.json.JSONException)

Aggregations

MultipartEntity (org.apache.http.entity.mime.MultipartEntity)62 HttpPost (org.apache.http.client.methods.HttpPost)41 StringBody (org.apache.http.entity.mime.content.StringBody)40 FileBody (org.apache.http.entity.mime.content.FileBody)37 File (java.io.File)31 HttpResponse (org.apache.http.HttpResponse)28 Test (org.junit.Test)24 TestHttpClient (io.undertow.testutils.TestHttpClient)13 IOException (java.io.IOException)13 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 JSONObject (org.json.JSONObject)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5 JSONException (org.json.JSONException)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 InputStream (java.io.InputStream)4