use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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.MultipartRequestEntity 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.MultipartRequestEntity 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.MultipartRequestEntity in project pinot by linkedin.
the class ServerSegmentCompletionProtocolHandler method doHttp.
private SegmentCompletionProtocol.Response doHttp(SegmentCompletionProtocol.Request request, Part[] parts) {
SegmentCompletionProtocol.Response response = SegmentCompletionProtocol.RESP_NOT_SENT;
HttpClient httpClient = new HttpClient();
ControllerLeaderLocator leaderLocator = ControllerLeaderLocator.getInstance();
final String leaderAddress = leaderLocator.getControllerLeader();
if (leaderAddress == null) {
LOGGER.error("No leader found {}", this.toString());
return SegmentCompletionProtocol.RESP_NOT_LEADER;
}
final String url = request.getUrl(leaderAddress);
HttpMethodBase method;
if (parts != null) {
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
method = postMethod;
} else {
method = new GetMethod(url);
}
LOGGER.info("Sending request {} for {}", url, this.toString());
try {
int responseCode = httpClient.executeMethod(method);
if (responseCode >= 300) {
LOGGER.error("Bad controller response code {} for {}", responseCode, this.toString());
return response;
} else {
response = new SegmentCompletionProtocol.Response(method.getResponseBodyAsString());
LOGGER.info("Controller response {} for {}", response.toJsonString(), this.toString());
if (response.getStatus().equals(SegmentCompletionProtocol.ControllerResponseStatus.NOT_LEADER)) {
leaderLocator.refreshControllerLeader();
}
return response;
}
} catch (IOException e) {
LOGGER.error("IOException {}", this.toString(), e);
leaderLocator.refreshControllerLeader();
return response;
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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