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