use of org.apache.commons.httpclient.methods.multipart.StringPart in project sling by apache.
the class SlingPostDeployMethod method deploy.
@Override
public void deploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
/* truncate off trailing slash as this has special behaviorisms in
* the SlingPostServlet around created node name conventions */
if (targetURL.endsWith("/")) {
targetURL = targetURL.substring(0, targetURL.length() - 1);
}
// append pseudo path after root URL to not get redirected for nothing
final PostMethod filePost = new PostMethod(targetURL);
try {
Part[] parts = new Part[2];
// Add content type to force the configured mimeType value
parts[0] = new FilePart("*", new FilePartSource(file.getName(), file), context.getMimeType(), null);
// Add TypeHint to have jar be uploaded as file (not as resource)
parts[1] = new StringPart("*@TypeHint", "nt:file");
/* Request JSON response from Sling instead of standard HTML, to
* reduce the payload size (if the PostServlet supports it). */
filePost.setRequestHeader("Accept", JSON_MIME_TYPE);
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
int status = context.getHttpClient().executeMethod(filePost);
// SlingPostServlet may return 200 or 201 on creation, accept both
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) {
context.getLog().info("Bundle installed");
} else {
String msg = "Installation failed, cause: " + HttpStatus.getStatusText(status);
if (context.isFailOnError()) {
throw new MojoExecutionException(msg);
} else {
context.getLog().error(msg);
}
}
} catch (Exception ex) {
throw new MojoExecutionException("Installation on " + targetURL + " failed, cause: " + ex.getMessage(), ex);
} finally {
filePost.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.
the class HttpBridgeMultipartRouteTest method testHttpClient.
@Test
public void testHttpClient() throws Exception {
File jpg = new File("src/test/resources/java.jpg");
String body = "TEST";
Part[] parts = new Part[] { new StringPart("body", body), new FilePart(jpg.getName(), jpg) };
PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello");
MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
method.setRequestEntity(requestEntity);
HttpClient client = new HttpClient();
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
assertEquals(body, responseBody);
String numAttachments = method.getResponseHeader("numAttachments").getValue();
assertEquals(numAttachments, "2");
}
use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.
the class MultiPartFormTest method createMultipartRequestEntity.
private RequestEntity createMultipartRequestEntity() throws Exception {
File file = new File("src/main/resources/META-INF/NOTICE.txt");
Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
return new MultipartRequestEntity(parts, new HttpMethodParams());
}
use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.
the class MultiPartFormWithCustomFilterTest method testSendMultiPartFormOverrideEnableMultpartFilterFalse.
@Test
public void testSendMultiPartFormOverrideEnableMultpartFilterFalse() throws Exception {
HttpClient httpclient = new HttpClient();
File file = new File("src/main/resources/META-INF/NOTICE.txt");
PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test2");
Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
httppost.setRequestEntity(reqEntity);
int status = httpclient.executeMethod(httppost);
assertEquals("Get a wrong response status", 200, status);
assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.
the class MultiPartFormWithCustomFilterTest method testSendMultiPartForm.
@Test
public void testSendMultiPartForm() throws Exception {
HttpClient httpclient = new HttpClient();
File file = new File("src/main/resources/META-INF/NOTICE.txt");
PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test");
Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
httppost.setRequestEntity(reqEntity);
int status = httpclient.executeMethod(httppost);
assertEquals("Get a wrong response status", 200, status);
String result = httppost.getResponseBodyAsString();
assertEquals("Get a wrong result", "A binary file of some kind", result);
assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
Aggregations