/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.maven.bundlesupport;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.FilePartSource;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.maven.plugin.MojoExecutionException;
abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
/**
* The URL of the running Sling instance.
*
* @parameter expression="${sling.url}"
* default-value="http://localhost:8080/sling"
* @required
*/
private String slingUrl;
/**
* The user name to authenticate at the running Sling instance.
*
* @parameter expression="${sling.user}" default-value="admin"
* @required
*/
private String user;
/**
* The password to authenticate at the running Sling instance.
*
* @parameter expression="${sling.password}" default-value="admin"
* @required
*/
private String password;
/**
* The startlevel for the uploaded bundle
*
* @parameter expression="${sling.bundle.startlevel}" default-value="20"
* @required
*/
private String bundleStartLevel;
/**
* Whether to start the uploaded bundle or not
*
* @parameter expression="${sling.bundle.start}" default-value="true"
* @required
*/
private boolean bundleStart;
/**
* Whether to refresh the packages after installing the uploaded bundle
*
* @parameter expression="${sling.refreshPackages}" default-value="true"
* @required
*/
private boolean refreshPackages;
public AbstractBundleInstallMojo() {
super();
}
protected abstract String getBundleFileName() throws MojoExecutionException;
public void execute() throws MojoExecutionException {
// get the file to upload
String bundleFileName = getBundleFileName();
// only upload if packaging as an osgi-bundle
File bundleFile = new File(bundleFileName);
String bundleName = getBundleSymbolicName(bundleFile);
if (bundleName == null) {
getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
return;
}
getLog().info(
"Installing Bundle " + bundleName + "(" + bundleFile + ") to "
+ slingUrl);
post(slingUrl, bundleFile);
}
protected void post(String targetURL, File file)
throws MojoExecutionException {
// append pseudo path after root URL to not get redirected for nothing
PostMethod filePost = new PostMethod(targetURL + "/install");
try {
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", bundleStartLevel));
if (bundleStart) {
partList.add(new StringPart("bundlestart", "start"));
}
if (refreshPackages) {
partList.add(new StringPart("refreshPackages", "true"));
}
Part[] parts = partList.toArray(new Part[partList.size()]);
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
// authentication stuff
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(user,
password);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
getLog().info("Bundle installed");
} else {
getLog().error(
"Installation failed, cause: "
+ HttpStatus.getStatusText(status));
}
} catch (Exception ex) {
throw new MojoExecutionException("Installation on " + targetURL
+ " failed, cause: " + ex.getMessage(), ex);
} finally {
filePost.releaseConnection();
}
}
}