use of org.apache.commons.httpclient.methods.multipart.FilePart in project sling by apache.
the class AbstractBundleDeployMojo method post.
private void post(String targetURL, File file) throws MojoExecutionException {
PostMethod filePost = new PostMethod(targetURL);
try {
Part[] parts = { new FilePart(file.getName(), new FilePartSource(file.getName(), file)), new StringPart("_noredir_", "_noredir_") };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
getLog().info("Bundle deployed");
} else {
String msg = "Deployment failed, cause: " + HttpStatus.getStatusText(status);
if (failOnError) {
throw new MojoExecutionException(msg);
} else {
getLog().error(msg);
}
}
} catch (Exception ex) {
throw new MojoExecutionException("Deployment on " + targetURL + " failed, cause: " + ex.getMessage(), ex);
} finally {
filePost.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.FilePart 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.FilePart in project h2o-2 by h2oai.
the class WebAPI method importModel.
/**
* Imports a model from a JSON file.
*/
public static void importModel() throws Exception {
// Upload file to H2O
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(URL + "/Upload.json?key=" + JSON_FILE.getName());
Part[] parts = { new FilePart(JSON_FILE.getName(), JSON_FILE) };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
if (200 != client.executeMethod(post))
throw new RuntimeException("Request failed: " + post.getStatusLine());
post.releaseConnection();
// Parse the key into a model
GetMethod get = new GetMethod(//
URL + "/2/ImportModel.json?" + //
"destination_key=MyImportedNeuralNet&" + //
"type=NeuralNetModel&" + "json=" + JSON_FILE.getName());
if (200 != client.executeMethod(get))
throw new RuntimeException("Request failed: " + get.getStatusLine());
get.releaseConnection();
}
use of org.apache.commons.httpclient.methods.multipart.FilePart in project pinot by linkedin.
the class ServerSegmentCompletionProtocolHandler method segmentCommit.
public SegmentCompletionProtocol.Response segmentCommit(long offset, final String segmentName, final File segmentTarFile) throws FileNotFoundException {
SegmentCompletionProtocol.Request.Params params = new SegmentCompletionProtocol.Request.Params();
params.withInstanceId(_instance).withOffset(offset).withSegmentName(segmentName);
SegmentCompletionProtocol.SegmentCommitRequest request = new SegmentCompletionProtocol.SegmentCommitRequest(params);
final InputStream inputStream = new FileInputStream(segmentTarFile);
Part[] parts = { new FilePart(segmentName, new PartSource() {
@Override
public long getLength() {
return segmentTarFile.length();
}
@Override
public String getFileName() {
return "fileName";
}
@Override
public InputStream createInputStream() throws IOException {
return new BufferedInputStream(inputStream);
}
}) };
return doHttp(request, parts);
}
use of org.apache.commons.httpclient.methods.multipart.FilePart 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");
}
Aggregations