Search in sources :

Example 6 with InputStreamBody

use of org.apache.http.entity.mime.content.InputStreamBody in project sling by apache.

the class ConsoleTestClient method testResourceResolver.

@Test
public void testResourceResolver() throws Exception {
    RequestBuilder rb = new RequestBuilder("http://localhost:9000");
    final MultipartEntity entity = new MultipartEntity();
    // Add Sling POST options
    entity.addPart("lang", new StringBody("esp"));
    entity.addPart("code", new InputStreamBody(getClass().getResourceAsStream("/test.js"), "test.js"));
    executor.execute(rb.buildPostRequest("/system/console/scriptconsole.json").withEntity(entity).withCredentials("admin", "admin")).assertStatus(200);
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) Test(org.junit.Test)

Example 7 with InputStreamBody

use of org.apache.http.entity.mime.content.InputStreamBody in project sling by apache.

the class SlingSpecificsSightlyIT method uploadFile.

private void uploadFile(String fileName, String serverFileName, String url) throws IOException {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(launchpadURL + url);
    post.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    InputStreamBody inputStreamBody = new InputStreamBody(this.getClass().getClassLoader().getResourceAsStream(fileName), ContentType.TEXT_PLAIN, fileName);
    entityBuilder.addPart(serverFileName, inputStreamBody);
    post.setEntity(entityBuilder.build());
    httpClient.execute(post);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

Example 8 with InputStreamBody

use of org.apache.http.entity.mime.content.InputStreamBody 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)

Aggregations

InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)8 HttpPost (org.apache.http.client.methods.HttpPost)5 StringBody (org.apache.http.entity.mime.content.StringBody)5 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)4 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)3 InputStream (java.io.InputStream)2 HttpClient (org.apache.http.client.HttpClient)2 HttpDelete (org.apache.http.client.methods.HttpDelete)2 HttpGet (org.apache.http.client.methods.HttpGet)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 Charset (java.nio.charset.Charset)1 HashSet (java.util.HashSet)1