use of org.apache.commons.httpclient.methods.PostMethod 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.PostMethod 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.PostMethod in project sling by apache.
the class PostServletOutputContentTypeTest method testJsonContentTypeException.
public void testJsonContentTypeException() throws Exception {
// Perform a POST that fails: invalid PostServlet operation
// with Accept header set to JSON
final String url = HTTP_BASE_URL + MY_TEST_PATH;
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
post.addParameter(new NameValuePair(SlingPostConstants.RP_OPERATION, "InvalidTestOperationFor" + getClass().getSimpleName()));
post.addRequestHeader("Accept", CONTENT_TYPE_JSON);
final int status = httpClient.executeMethod(post);
assertEquals(500, status);
final String contentType = post.getResponseHeader("Content-Type").getValue();
final String expected = CONTENT_TYPE_JSON;
assertTrue("Expecting content-type " + expected + " for failed POST request, got " + contentType, contentType != null && contentType.startsWith(expected));
}
use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class PostServletOutputContentTypeTest method runTest.
private void runTest(String acceptHeaderValue, boolean useHttpEquiv, String expectedContentType) throws Exception {
final String info = (useHttpEquiv ? "Using http-equiv parameter" : "Using Accept header") + ": ";
final String url = HTTP_BASE_URL + MY_TEST_PATH;
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
if (acceptHeaderValue != null) {
if (useHttpEquiv) {
post.addParameter(":http-equiv-accept", acceptHeaderValue);
} else {
post.addRequestHeader("Accept", acceptHeaderValue);
}
}
final int status = httpClient.executeMethod(post) / 100;
assertEquals(info + "Expected status 20x for POST at " + url, 2, status);
final Header h = post.getResponseHeader("Content-Type");
assertNotNull(info + "Expected Content-Type header", h);
final String ct = h.getValue();
assertTrue(info + "Expected Content-Type '" + expectedContentType + "' for Accept header=" + acceptHeaderValue + " but got '" + ct + "'", ct.startsWith(expectedContentType));
}
use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class SlingPostProcessorTest method processorsActive.
@Test
public void processorsActive() throws HttpException, IOException {
final PostMethod post = new PostMethod(testUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX);
post.setFollowRedirects(false);
post.setParameter("DummyModification", "true");
try {
T.getHttpClient().executeMethod(post);
final String content = post.getResponseBodyAsString();
final int i1 = content.indexOf("source:SlingPostProcessorOne");
assertTrue("Expecting first processor to be present", i1 > 0);
final int i2 = content.indexOf("source:SlingPostProcessorTwo");
assertTrue("Expecting second processor to be present", i2 > 0);
assertTrue("Expecting service ranking to put processor one first", i1 < i2);
} finally {
post.releaseConnection();
}
}
Aggregations