/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/ant/taskdefs/Get.java,v 1.3 2001/05/01 21:29:40 remm Exp $
* $Revision: 1.3 $
* $Date: 2001/05/01 21:29:40 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.webdav.ant.taskdefs;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import org.apache.tools.ant.BuildException;
import org.apache.webdav.ant.Scanner;
import org.apache.webdav.ant.taskdefs.WebdavMatchingTask;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* Get a particular resource or collection of resources from a
* WebDAV-compliant web server. This task differs from the
* basic Get task in Ant because it allows regular expressions
* to be used.
*
* <p> To use this task, first you must it in your Ant build file:
*
* <PRE>
* <taskdef name="davget" classname="org.apache.webdav.ant.taskdefs.Get"/>
* </PRE>
*
* <p> Next, specify a specific target, such as:
*
* <PRE>
*
* <target name="download" depends="init">
* <davget baseurl="http://localhost/dav/" dest="." verbose="true">
* <include name="*.html"/>
* </davget>
* </target>
* <PRE>
*
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
*/
public class Get extends WebdavMatchingTask {
private String baseURL = null; // required
private String dest = null; // required
private String userid = null;
private String password = null;
private boolean verbose = false;
String ignoreErrors=null;
/**
* Does the work.
*
* @exception BuildException Thrown in unrecovrable error.
*/
public void execute() throws BuildException {
try {
// first, is the destination directory valid?
File destDir = new File(dest);
if (!destDir.isDirectory()) {
throw new BuildException(
"Destination " + dest + " is not a valid directory");
}
// next, check the include list
if (includeList.size() == 0) {
throw new BuildException(
"Includes must be specified");
}
if ((this.userid != null && !this.userid.equals("")) &&
(this.password != null && !this.password.equals(""))) {
client.setCredentials(new Credentials(this.userid, this.password));
}
// validity check on the URL
URL url = new URL(baseURL);
client.startSession(url.getHost(),
(url.getPort() < 0 ? 80 : url.getPort()));
Scanner scanner = getScanner(url);
GetMethod getMethod = new GetMethod();
String[] resources = scanner.getIncludedFiles();
log("Downloading " + resources.length + " files from " + url);
for (int i = 0; i < resources.length; i++) {
String resource = resources[i];
String file = resource.replace('/',File.separatorChar);
file = concatenate(dest, file, File.separator);
FileOutputStream output = new FileOutputStream(file);
resource = concatenate(url.getFile(), resource, "/");
if (verbose) {
log("Getting: " + resource + " to " + file);
}
getMethod.setPath(resource);
client.executeMethod(getMethod);
InputStream input = getMethod.getData();
if( input == null ) {
throw new BuildException( "Can't get " + resource + " to " + file);
}
byte[] buffer = new byte[100 * 1024];
int length;
while ((length = input.read(buffer)) >= 0) {
output.write(buffer, 0, length);
}
output.close();
input.close();
getMethod.recycle();
}
client.endSession();
} catch (BuildException e) {
log(e.getMessage());
if( ignoreErrors == null ) {
throw e;
}
} catch (HttpException e) {
throw new BuildException(e.toString());
} catch (MalformedURLException e) {
throw new BuildException(e.toString());
} catch (IOException e) {
if( ignoreErrors == null )
throw new BuildException(e.toString());
}
}
/**
* Set the base URL.
*
* @param base URL for the request.
*/
public void setBaseurl(String baseURL) {
if (baseURL.endsWith("/")) {
this.baseURL = baseURL;
} else {
this.baseURL = baseURL + "/";
}
}
/**
* Where to copy the source file.
*
* @param dest Path to file.
*/
public void setDest(String dest) {
this.dest = dest.replace('/',File.separatorChar)
.replace('\\',File.separatorChar);
}
/**
* Userid to access the resources
*
* @param userid - HTTP userid
*/
public void setUserid(String userid) {
this.userid = userid;
}
/**
* Password to access the resources
*
* @param password - HTTP password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Be verbose, if set to "<CODE>true</CODE>".
*
* @param verbose if "true" then be verbose
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/**
* Don't stop if get fails if set to "<CODE>true</CODE>".
*
* @param ignoreErrors if "true" then be verbose
*/
public void setIgnoreErrors(String ignoreErrors) {
this.ignoreErrors = ignoreErrors;
}
protected String concatenate(String first, String second, String separator) {
if (first.endsWith(separator) && !second.startsWith(separator)) {
return first + second;
} else if (!first.endsWith(separator) && second.startsWith(separator)) {
return first + second;
} else if (!first.endsWith(separator) && !second.startsWith(separator)) {
return first + separator + second;
} else {
return first + second.substring(1);
}
}
}