[prev in list] [next in list] [prev in thread] [next in thread]
List: slide-dev
Subject: cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/ant/taskdefs Put.java
From: bcholmes () locus ! apache ! org
Date: 2000-11-29 6:44:06
[Download RAW message or body]
bcholmes 00/11/28 22:44:06
Added: src/webdav/client/src/org/apache/webdav/ant/taskdefs
Put.java
Log:
Create a basic Ant Put task. More function to come later.
Revision Changes Path
1.1 \
jakarta-slide/src/webdav/client/src/org/apache/webdav/ant/taskdefs/Put.java
Index: Put.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/ant/taskdefs/Put.java,v \
1.1 2000/11/29 06:44:05 bcholmes Exp $
* $Revision: 1.1 $
* $Date: 2000/11/29 06:44:05 $
*
* ====================================================================
* 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.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.FileSet;
import org.apache.webdav.lib.Credentials;
import org.apache.webdav.lib.WebdavClient;
import org.apache.webdav.lib.WebdavException;
import org.apache.webdav.lib.WebdavStatus;
import org.apache.webdav.lib.methods.OptionsMethod;
import org.apache.webdav.lib.methods.PutMethod;
/**
* Put a bunch of files on to a WebDAV-compliant web server.
*
* <p> To use this task, first you must define it in your Ant build file:
*
* <PRE>
* <taskdef name="davput" classname="org.apache.webdav.ant.taskdefs.Put"/>
* </PRE>
*
* <p> Next, specify a specific target, such as:
*
* <PRE>
*
* <target name="upload" depends="init">
* <davput url="http://localhost/dav/"
* userid="hpotter" password="muggle">
* <fileset dir=".">
* <include name="*.html"/>
* </fileset>
* </davput>
* </target>
* <PRE>
*
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
* @version $Revision: 1.1 $
*/
public class Put extends MatchingTask {
private String toURL = null;
private String userid = null;
private String password = null;
private boolean lock = false;
/**
* The sets of files that will be sent to the web server.
*/
private Vector filesets = new Vector ();
protected WebdavClient client = new WebdavClient();
public Put() {
}
/**
* Adds a set of files (nested fileset attribute).
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
}
/**
* Should we try to lock the remote resource as we upload it?
*
* @param userid - HTTP userid
*/
public boolean getLock() {
return this.lock;
}
public void setLock(boolean lock) {
this.lock = lock;
}
/**
* Get a userid to access the resources
*
* @returns userid - HTTP userid
*/
public String getUserid() {
return userid;
}
/**
* Set a userid to access the resources
*
* @param userid - HTTP userid
*/
public void setUserid(String userid) {
this.userid = userid;
}
/**
* Get a password to access the resources
*
* @returns userid - HTTP userid
*/
public String getPassword() {
return password;
}
/**
* Set a password to access the resources
*
* @param password - HTTP password
*/
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return toURL;
}
/**
* Set the base URL.
*
* @param base URL for the request.
*/
public void setUrl(String url) {
if (url.endsWith("/")) {
this.toURL = url;
} else {
this.toURL = url + "/";
}
}
/**
* Does the work.
*
* @exception BuildException Thrown in unrecoverable error.
*/
public void execute() throws BuildException {
try {
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(toURL);
client.startSession(url.getHost(),
(url.getPort() < 0 ? 80 : url.getPort()));
OptionsMethod optionsMethod = new OptionsMethod();
optionsMethod.setPath(url.getFile());
client.executeMethod(optionsMethod);
/*
FIXME: is there a bug in my version of mod_dav (0.9.15)? It doesn't
tell me that PUT is allowed.
if (!optionsMethod.isAllowed("PUT")) {
throw new BuildException(
"Web server doesn't support PUT; can't upload resources");
}
*/
if (!optionsMethod.isAllowed("LOCK") && getLock()) {
throw new BuildException(
"Web server doesn't support LOCK; can't LOCK remote \
resources"); }
for (int i=0; i < filesets.size(); i++) {
FileSet fileset = (FileSet) filesets.elementAt(i);
uploadFiles(url.getFile(),
fileset.getDirectoryScanner(this.project));
}
client.endSession();
} catch (MalformedURLException e) {
throw new BuildException(e.getMessage(), e);
} catch (IOException e) {
throw new BuildException(e.getMessage(), e);
} catch (WebdavException e) {
throw new BuildException(e.getMessage(), e);
}
}
protected void uploadFiles(String urlFile, FileScanner scanner)
throws WebdavException, IOException {
File baseDir = scanner.getBasedir();
String[] files = scanner.getIncludedFiles();
PutMethod method = new PutMethod();
for (int i = 0; i < files.length; i++) {
File file = new File(baseDir, files[i]);
String path = files[i].replace(File.separatorChar,'/');
if (i > 0) {
method.recycle();
}
method.setPath(concatenate(urlFile, path, "/"));
method.sendData(file);
client.executeMethod(method);
int status = method.getStatusCode();
if ((status != WebdavStatus.SC_OK) &&
(status != WebdavStatus.SC_CREATED) &&
(status != WebdavStatus.SC_ACCEPTED) &&
(status != WebdavStatus.SC_NO_CONTENT)) {
log("Status code " + method.getStatusCode() +
" (" + WebdavStatus.getStatusText(method.getStatusCode()) +
") received while uploading " + method.getPath());
}
}
}
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);
}
}
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic