Search in sources :

Example 26 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart 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();
    }
}
Also used : ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException)

Example 27 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart 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)

Aggregations

FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)27 Part (org.apache.commons.httpclient.methods.multipart.Part)25 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)23 PostMethod (org.apache.commons.httpclient.methods.PostMethod)20 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)18 HttpClient (org.apache.commons.httpclient.HttpClient)12 File (java.io.File)10 IOException (java.io.IOException)7 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 HttpState (org.apache.commons.httpclient.HttpState)4 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)4 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)4 ServiceException (com.zimbra.common.service.ServiceException)3 Element (com.zimbra.common.soap.Element)3 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)3 HeaderElement (org.apache.commons.httpclient.HeaderElement)3 HttpException (org.apache.commons.httpclient.HttpException)3 PartSource (org.apache.commons.httpclient.methods.multipart.PartSource)3