Search in sources :

Example 36 with MultipartEntity

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

the class ValidationServiceIT method testPostProcessorWithInvalidModel.

@Test
public void testPostProcessorWithInvalidModel() throws IOException, JsonException {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("Hello World"));
    final String url = String.format("http://localhost:%s", httpPort());
    RequestBuilder requestBuilder = new RequestBuilder(url);
    // test JSON response, because the HTML response overwrites the original exception (https://issues.apache.org/jira/browse/SLING-6703)
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/content/validated/invalidresource").withEntity(entity).withHeader("Accept", "application/json").withCredentials("admin", "admin")).assertStatus(500);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    JsonObject error = jsonResponse.getJsonObject("error");
    assertEquals("org.apache.sling.validation.impl.postprocessor.InvalidResourcePostProcessorException", error.getString("class"));
    assertEquals("Validation errors: field1 : Property does not match the pattern \"^\\p{Upper}+$\"., Missing required property with name \"field2\".", error.getString("message"));
}
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) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 37 with MultipartEntity

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

the class SlingClient method setProperties.

/** Updates a node at specified path, with optional properties
    */
public void setProperties(String path, Map<String, Object> properties) throws IOException {
    final MultipartEntity entity = new MultipartEntity();
    // Add user properties
    if (properties != null) {
        for (Map.Entry<String, Object> e : properties.entrySet()) {
            entity.addPart(e.getKey(), new StringBody(e.getValue().toString()));
        }
    }
    final HttpResponse response = executor.execute(builder.buildPostRequest(path).withEntity(entity).withCredentials(username, password)).assertStatus(200).getResponse();
}
Also used : MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 38 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project instructure-android by instructure.

the class UploadFileSynchronousAPI method uploadFile.

// STEPS 2 AND 3 OF API
public static String uploadFile(File image, String url, String contentType, ArrayList<BasicNameValuePair> pairs, Context context) {
    try {
        // STEP 2 of API
        HttpPost httppost = new HttpPost(url);
        MultipartEntity multipartEntity = new MultipartEntity();
        for (BasicNameValuePair pair : pairs) {
            String value = pair.getValue();
            multipartEntity.addPart(pair.getName(), new StringBody(pair.getValue()));
        }
        multipartEntity.addPart("file", new FileBody(image));
        httppost.setEntity(multipartEntity);
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        if (mHttpClient == null) {
            mHttpClient = AndroidHttpClient.newInstance("agent");
            mHttpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            mHttpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        }
        HttpResponse httpResponse = mHttpClient.execute(httppost);
        String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        // STEP 3 of API
        Header[] headerLocation = httpResponse.getHeaders("Location");
        if (headerLocation.length <= 0) {
            return "Header error";
        }
        String location = headerLocation[0].getValue();
        Header[] headerContent = httpResponse.getHeaders("Content-Length");
        if (headerContent.length <= 0) {
            return "Header content error";
        }
        BasicNameValuePair pair = new BasicNameValuePair(headerContent[0].getName(), headerContent[0].getValue());
        ArrayList<BasicNameValuePair> paramPairs = new ArrayList<BasicNameValuePair>();
        pairs.add(pair);
        APIHttpResponse postResponse = HttpHelpers.httpPost(location, paramPairs, context);
        return postResponse.responseBody;
    } catch (Exception e) {
        return null;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 39 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project cxf by apache.

the class JAXRSMultipartTest method testMultipartRequestTooLargeManyParts.

@Test
public void testMultipartRequestTooLargeManyParts() throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("http://localhost:" + PORT + "/bookstore/books/image");
    String ct = "multipart/mixed";
    post.setHeader("Content-Type", ct);
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("image", new ByteArrayBody(new byte[1024 * 9], "testfile.png"));
    entity.addPart("image", new ByteArrayBody(new byte[1024 * 11], "testfile2.png"));
    post.setEntity(entity);
    try {
        CloseableHttpResponse response = client.execute(post);
        assertEquals(413, response.getStatusLine().getStatusCode());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) 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) Test(org.junit.Test)

Example 40 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project cxf by apache.

the class JAXRSMultipartTest method testMultipartRequestTooLarge.

@Test
public void testMultipartRequestTooLarge() throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("http://localhost:" + PORT + "/bookstore/books/image");
    String ct = "multipart/mixed";
    post.setHeader("Content-Type", ct);
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("image", new ByteArrayBody(new byte[1024 * 11], "testfile.png"));
    post.setEntity(entity);
    try {
        CloseableHttpResponse response = client.execute(post);
        assertEquals(413, response.getStatusLine().getStatusCode());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) 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) Test(org.junit.Test)

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