Search in sources :

Example 21 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart 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();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FilePartSource(org.apache.commons.httpclient.methods.multipart.FilePartSource) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part)

Example 22 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart in project sling by apache.

the class SlingPostDeployMethod method undeploy.

@Override
public void undeploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
    final PostMethod post = new PostMethod(getURLWithFilename(targetURL, file.getName()));
    try {
        // Add SlingPostServlet operation flag for deleting the content
        Part[] parts = new Part[1];
        parts[0] = new StringPart(":operation", "delete");
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        // Request JSON response from Sling instead of standard HTML
        post.setRequestHeader("Accept", JSON_MIME_TYPE);
        int status = context.getHttpClient().executeMethod(post);
        if (status == HttpStatus.SC_OK) {
            context.getLog().info("Bundle uninstalled");
        } else {
            context.getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(), ex);
    } finally {
        post.releaseConnection();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)22 Part (org.apache.commons.httpclient.methods.multipart.Part)22 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)19 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HttpClient (org.apache.commons.httpclient.HttpClient)7 File (java.io.File)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 Test (org.junit.Test)4 Map (java.util.Map)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)3 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)3 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 RepositoryException (org.apache.sling.ide.transport.RepositoryException)2 PostParameter (weibo4j.model.PostParameter)2 WeiboException (weibo4j.model.WeiboException)2