use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.
the class SlingIntegrationTestClient method uploadToFileNode.
/** Upload to an file node structure, see SLING-168 */
public void uploadToFileNode(String url, File localFile, String fieldName, String typeHint) throws IOException {
final Part[] parts = new Part[typeHint == null ? 1 : 2];
parts[0] = new FilePart(fieldName, localFile);
if (typeHint != null) {
parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
}
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
final int status = httpClient.executeMethod(post);
final int expected = 200;
if (status != expected) {
throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post, 0));
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.
the class DeleteNodeCommand method execute.
@Override
public Result<Void> execute() {
PostMethod post = new PostMethod(getPath());
try {
Part[] parts = { new StringPart(":operation", "delete") };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
httpClient.getParams().setAuthenticationPreemptive(true);
int responseStatus = httpClient.executeMethod(post);
return resultForResponseStatus(responseStatus);
} catch (Exception e) {
return AbstractResult.failure(new RepositoryException(e));
} finally {
post.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.
the class UpdateContentCommand method execute.
@Override
public Result<Void> execute() {
PostMethod post = new PostMethod(getPath());
try {
List<Part> parts = new ArrayList<>();
for (Map.Entry<String, Object> property : properties.entrySet()) {
if (ProtectedNodes.exists(property.getKey())) {
continue;
}
Object propValue = property.getValue();
if (propValue instanceof String) {
parts.add(new StringPart(property.getKey(), (String) propValue));
} else if (property != null) {
// TODO handle multi-valued properties
System.err.println("Unable to handle property " + property.getKey() + " of type " + property.getValue().getClass());
}
}
File f = new File(fileInfo.getLocation());
if (f.isFile()) {
parts.add(new FilePart(fileInfo.getName(), f));
}
post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
httpClient.getParams().setAuthenticationPreemptive(true);
int responseStatus = httpClient.executeMethod(post);
return resultForResponseStatus(responseStatus);
} catch (Exception e) {
return AbstractResult.failure(new RepositoryException(e));
} finally {
post.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.
the class HttpOsgiClient method installBundle.
@Override
public void installBundle(InputStream in, String fileName) throws OsgiClientException {
if (in == null) {
throw new IllegalArgumentException("in may not be null");
}
if (fileName == null) {
throw new IllegalArgumentException("fileName may not be null");
}
// append pseudo path after root URL to not get redirected for nothing
final PostMethod filePost = new PostMethod(repositoryInfo.appendPath("system/console/install"));
try {
// set referrer
filePost.setRequestHeader("referer", "about:blank");
List<Part> partList = new ArrayList<>();
partList.add(new StringPart("action", "install"));
partList.add(new StringPart("_noredir_", "_noredir_"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(in, baos);
PartSource partSource = new ByteArrayPartSource(fileName, baos.toByteArray());
partList.add(new FilePart("bundlefile", partSource));
partList.add(new StringPart("bundlestart", "start"));
Part[] parts = partList.toArray(new Part[partList.size()]);
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
int status = getHttpClient().executeMethod(filePost);
if (status != 200) {
throw new OsgiClientException("Method execution returned status " + status);
}
} catch (IOException e) {
throw new OsgiClientException(e);
} finally {
filePost.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.
the class FelixPostDeployMethod method deploy.
@Override
public void deploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
// append pseudo path after root URL to not get redirected for nothing
final PostMethod filePost = new PostMethod(targetURL + "/install");
try {
// set referrer
filePost.setRequestHeader("referer", "about:blank");
List<Part> partList = new ArrayList<Part>();
partList.add(new StringPart("action", "install"));
partList.add(new StringPart("_noredir_", "_noredir_"));
partList.add(new FilePart("bundlefile", new FilePartSource(file.getName(), file)));
partList.add(new StringPart("bundlestartlevel", context.getBundleStartLevel()));
if (context.isBundleStart()) {
partList.add(new StringPart("bundlestart", "start"));
}
if (context.isRefreshPackages()) {
partList.add(new StringPart("refreshPackages", "true"));
}
Part[] parts = partList.toArray(new Part[partList.size()]);
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
int status = context.getHttpClient().executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
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();
}
}
Aggregations