You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@apache.org on 2001/02/06 07:31:45 UTC
cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/util WebdavClientInfo.java WebdavResource.java
remm 01/02/05 22:31:45
Added: src/webdav/client/src/org/apache/webdav/util
WebdavClientInfo.java WebdavResource.java
Log:
- Add an optional API layer which sits on top on the WebDAV client library,
and which gives a resource oriented view of the Webdav namespace.
- This API cannot be considered stable right now (unlike the client library core).
- Patch submitted by Sung-Gu Park.
Revision Changes Path
1.1 jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavClientInfo.java
Index: WebdavClientInfo.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavClientInfo.java,v 1.1 2001/02/06 06:31:45 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/06 06:31:45 $
*
* ====================================================================
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.util;
import java.util.Hashtable;
import java.util.Enumeration;
import java.net.URL;
import org.apache.webdav.lib.*;
/**
* WebDAV client session management information.
*
* @author Sung-Gu Park
*/
public class WebdavClientInfo {
// -------------------------------------------------------------- Variables
private static Hashtable webdavClientInfo = new Hashtable();
// --------------------------------------------------------- Public Methods
/**
* Restore the webdav client infomation
*
* @param URL
*
* @return WebdavClient
*/
public WebdavClient getWebdavClient(URL url) {
WebdavClient client;
String host = url.getHost();
int port = (url.getPort() == -1) ? 80 : url.getPort();
client = (WebdavClient) webdavClientInfo.get(host + ":" + port);
if (client == null)
client = new WebdavClient(url);
return client;
}
/**
* Save the webdav client infomation
*
* @param WebdavClient
*/
public void setWebdavClient(WebdavClient client) {
webdavClientInfo.put(client.getHost() + ":" +
client.getPort(), client);
}
/**
* Close the webdav client infomation and end the session
*
* @param String host
* @param int port
*/
public void closeWebdavClient(String host, int port) {
try {
URL url = new URL("http://" + host +
((port == 80) ? "" : ":" + port));
WebdavClient webdavClient = getWebdavClient(url);
webdavClient.endSession();
webdavClientInfo.remove(host + ":" + port);
} catch (Exception e) {
}
}
/**
* Get the lock for a url resource
*
* @param url URL
*
* @return String
*/
public String getLock(URL url) {
String lock;
WebdavClient client = getWebdavClient(url);
State state = client.getState();
String path = checkUri(url.getFile());
Enumeration locks = state.getLocks(path);
while (locks.hasMoreElements()) {
lock = (String) locks.nextElement();
if (path.equalsIgnoreCase(lock))
return lock;
}
return null;
}
/**
* Add the lock for a url resource
*
* @param URL
* @param String lock Lock token value
*/
public void addLock(URL url, String lock) {
WebdavClient client = getWebdavClient(url);
State state = client.getState();
String path = checkUri(url.getFile());
if (state != null) {
state.addLock(path, lock);
client.setState(state);
webdavClientInfo.put(url.getHost() + ":"
+ getPort(url), client);
}
}
/**
* Remove the lock for a url resource
*
* @param URL url
*/
public void removeLock(URL url) {
WebdavClient client = getWebdavClient(url);
State state = client.getState();
String path = checkUri(url.getFile());
if (state != null) {
state.removeLocks(path);
client.setState(state);
webdavClientInfo.put(url.getHost() + ":"
+ getPort(url), client);
}
}
/**
* Check URI. because of a bug URL class (below JDK 1.3)
*/
public String checkUri(String uri) {
if (uri == null || uri.equals(""))
uri = "/";
return uri;
}
/**
* Get port. because of a bug URL class (below JDK 1.3)
*/
public int getPort(URL url) {
return (url.getPort() == -1) ? 80 : url.getPort();
}
}
1.1 jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java
Index: WebdavResource.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v 1.1 2001/02/06 06:31:45 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/06 06:31:45 $
*
* ====================================================================
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.util;
import java.net.URL;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.net.MalformedURLException;
import java.io.*;
import java.util.Date;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.text.DateFormat;
import org.apache.webdav.lib.*;
import org.apache.webdav.lib.properties.*;
import org.apache.webdav.lib.methods.*;
import org.apache.webdav.lib.util.DOMUtils;
/**
* Abstracts the concept of a resource on a WebDAV server.
*
* @author Sung-Gu Park
*/
public class WebdavResource implements Comparable {
// -------------------------------------------------------------- Constants
/**
* Constants for DAV properties
*/
public static final String CREATIONDATE = "creationdate";
public static final String DISPLAYNAME ="displayname";
public static final String GETETAG = "getetag";
public static final String GETLASTMODIFIED = "getlastmodified";
public static final String RESOURCETYPE = "resourcetype";
public static final String GETCONTENTLENGTH = "getcontentlength";
public static final String SUPPORTEDLOCK = "supportedlock";
public static final String ISHIDDEN = "ishidden";
public static final String ISCOLLECTION = "iscollection";
public static final String GETCONTENTTYPE = "getcontenttype";
// ----------------------------------------------------- Instance Variables
/**
* WebdavClient & URL
*/
private URL url;
private URL urlChecked; // checking for setAllProp method
private WebdavClient client;
private Vector hrefList;
/**
* File Handling
*/
private InputStream is;
/**
* Clients for session and lock information management
*/
private WebdavClientInfo clients = new WebdavClientInfo();
/**
* DAV properties
*/
private String creationDate;
private String displayName;
private String getEtag;
private GetLastModifiedProperty getLastModified;
private ResourceTypeProperty resourceType;
private String getContentLength;
private String supportedLock;
private String isHidden = "0";
private String isCollection = "0";
private String getContentType = "";
private boolean exists = false;
private boolean overwrite = false;
// -------------------------------------------------------------- Variables
/**
* Owner for lock information
*/
private static final String defaultOwner = "Slide WebDAV Client User";
// ----------------------------------------------------------- Constructors
/**
* Constructor for this class
*
* @see setURL(url)
*/
public WebdavResource() {
}
/**
* Constructor for this class
*
* @param URL
*/
public WebdavResource(URL url) throws WebdavException {
setURL(url);
}
/**
* Constructor for this class
*
* @param URL
*/
public WebdavResource(URL url, String userName, String password)
throws WebdavException {
setURL(url, userName, password);
}
/**
* Set URL in WebdavResource
*
* @param URL
* @see getURL()
*/
public void setURL(URL url) throws WebdavException {
setURL(url, null, null);
}
/**
* Set URL in WebdavResource
*
* @param URL
* @see getURL()
*/
public void setURL(URL url, String userName, String password)
throws WebdavException {
String urlString = url.toString();
String urlHost = url.getHost().toString();
int urlPort = url.getPort();
// Processing for default http port, "http://"
if ((urlPort == 80) && (urlString.indexOf(":80") ==
(7 + urlHost.length()) )) {
try {
this.url = new URL(url.getProtocol(), url.getHost(),
checkUri(url.getFile()));
} catch (MalformedURLException mue) {
}
} else
this.url = url;
exists = false;
setAllProp(userName, password);
}
/**
* Get URL in WebdavResource
*
* @return URL
* @see setURL()
*/
public URL getURL() throws WebdavException {
if (!exists)
throw new WebdavException(WebdavStatus.SC_NOT_FOUND);
return url;
}
/**
* Check URI. because of a bug URL class (below JDK 1.3)
*/
public String checkUri(String uri) {
if (uri == null || uri.equals(""))
uri = "/";
return uri;
}
/**
* Set overwrite flag for PUT method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
/**
* Get overwrite flag for PUT method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean getOverwrite() {
return overwrite;
}
/**
* Check if the current url used
*
* @return boolean
*/
private boolean getUsed() {
if (url != null && urlChecked != null)
return urlChecked.toString().equals(url.toString());
return false;
}
/**
* Set the current url used
*/
private void setUsed() {
try {
urlChecked = new URL(url.toString());
} catch (MalformedURLException mue) {
}
}
/**
* Set WebdavResource with url
*/
private void setAllProp(String userName, String password)
throws WebdavException {
if ( getUsed() )
return;
PropFindMethod method = null;
try {
WebdavClient client = clients.getWebdavClient(url);
if (userName != null && userName.length() > 0)
client.setCredentials(new Credentials(userName, password));
// Set the new WebDAV client
clients.setWebdavClient(client);
// Change the depth with allprop
String path = checkUri(url.getFile());
method = new PropFindMethod(path, 1);
// default depth=infinity, type=allprop
client.executeMethod(method);
// Processing the result
Enumeration urls = method.getAllResponseURLs();
hrefList = new Vector();
while ( urls.hasMoreElements() ) {
String element = (String) urls.nextElement();
hrefList.addElement(element);
Enumeration enum = method.getResponseProperties(element);
while (enum.hasMoreElements()) {
Property property = (Property) enum.nextElement();
String urlString = url.toString();
String owningString = property.getOwningURL();
URL owningUrl = new URL(owningString);
if (url.sameFile(owningUrl)) {
exists = true;
} else {
String urlPath = checkUri(url.getFile());
String owningPath = checkUri(owningUrl.getFile());
int compared = urlPath.compareTo(owningPath);
if (compared == -1 && owningPath.endsWith("/")) {
exists = true;
setURL(new URL(urlString + "/"));
} else if (compared == 1 && urlPath.endsWith("/")) {
exists = true;
} else {
continue;
}
}
// --------------------------- checking dav properties
if (property.getLocalName().equals(CREATIONDATE)) {
creationDate =
DOMUtils.getTextValue(property.getElement());
} else
if (property.getLocalName().equals(DISPLAYNAME)) {
displayName =
DOMUtils.getTextValue(property.getElement());
}
if (property.getLocalName().equals(GETETAG)) {
getEtag =
DOMUtils.getTextValue(property.getElement());
} else if (property.getLocalName().equals(GETLASTMODIFIED)) {
GetLastModifiedProperty getLastModified =
(GetLastModifiedProperty) property;
} else if (property.getLocalName().equals(RESOURCETYPE)) {
resourceType = (ResourceTypeProperty) property;
} else if (property.getLocalName().equals(GETCONTENTLENGTH)) {
getContentLength =
DOMUtils.getTextValue(property.getElement());
} else if (property.getLocalName().equals(SUPPORTEDLOCK)) {
supportedLock =
DOMUtils.getTextValue(property.getElement());
} else if (property.getLocalName().equals(ISHIDDEN)) {
isHidden =
DOMUtils.getTextValue(property.getElement());
} else if (property.getLocalName().equals(ISCOLLECTION)) {
isCollection =
DOMUtils.getTextValue(property.getElement());
} else if (property.getLocalName().equals(GETCONTENTTYPE)) {
getContentType =
DOMUtils.getTextValue(property.getElement());
}
}
}
setUsed();
} catch (WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch (Exception e) {
//file://e.printStackTrace();
}
}
/**
* Check whether url exists
*
* @return String
*/
public boolean exists() {
return exists;
}
/**
* Get all href list
*
* @return Vector
*/
public Vector getHrefList() {
return (hrefList == null) ? new Vector() : hrefList;
}
/**
* Get the value of DAV property, creationdate
*
* @return String
*/
public String getCreationDate() {
return (creationDate == null) ? "" : creationDate;
}
/**
* Get the value of DAV property, displayname
*
* @return String
*/
public String getDisplayName() {
return (displayName == null) ? "" : displayName;
}
/**
* Get the value of DAV property, getetag
*
* @return String
*/
public String getGetEtag() {
return (getEtag == null) ? "" : getEtag;
}
/**
* Get the value of DAV property, getlastmodified
*
* @return String
*/
public long getGetLastModified() {
return getLastModified.getDate().getTime();
}
/**
* Get the value of DAV property, ResourceTypeProperty for resourcetype
*
* @return String
* @see isCollection()
*/
public ResourceTypeProperty getResourceType() {
return resourceType;
}
/**
* Get the value of DAV property, resourcetype
*
* @return String
* @see getResourceType()
* @see getIsCollection()
*/
public boolean isCollection() {
return getResourceType().isCollection();
}
/**
* Get the value of DAV property, getcontentlength
*
* @return String
*/
public long getGetContentLength() {
return Long.parseLong(getContentLength);
}
/**
* Get the value of DAV property, supportedlock
*
* @return String
*/
public String getSupportedLock() {
return (supportedLock == null) ? "" : supportedLock;
}
/**
* Get the value of DAV property, ishidden
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean getIsHidden() {
return isHidden.equals("1") ? true : false;
}
/**
* Get the value of DAV property, iscollection
*
* @return boolean <code>true</code>, <code>false</code>
* @see isCollection()
*/
public boolean getIsCollection() {
return isCollection.equals("1") ? true : false;;
}
/**
* Get the value of DAV property, getcontenttype
*
* @return String
*/
public String getGetContentType() {
return getContentType;
}
/**
* Set Authentication Information
*
* @return WebdavClient
*/
public WebdavClient login() throws WebdavException {
return login(null, null);
}
/**
* Set Authentication Information
*
* @param userName User Account
* @param password User password
* @return WebdavClient
*/
public WebdavClient login(String userName, String password)
throws WebdavException {
if (url == null)
return null;
WebdavClient client;
if (userName != null && userName.length() > 0)
client = new WebdavClient(url, userName, password);
else
client = new WebdavClient(url);
return client;
}
/**
* Remove the WebDAV client session information and close the session
*/
public void logout() throws IOException {
clients.closeWebdavClient(url.getHost(), url.getPort() == -1 ?
80 : url.getPort());
}
/**
* Close a session for a client
*/
public void close() throws IOException {
WebdavClient client = clients.getWebdavClient(url);
client.endSession();
}
/**
* Get InputStream for the GET method and set the temporary directory
*
* @param tempDir
* @return InputStream
*/
public InputStream getInputStream(String tempDir)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
GetMethod method = null;
try {
// use disk to save by default
method = new GetMethod(url.getFile(), tempDir);
client.executeMethod(method);
is = method.getData();
} catch (WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
} catch(Exception e) {
} finally {
return is;
}
}
/**
* Execute the GET method
*
* @param File source file
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean getMethod(String tempFile) throws WebdavException {
return getMethod("temp/", tempFile);
}
/**
* Execute the GET method
*
* @param File source file
* @param String tempDir Temporary Directory
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean getMethod(String tempDir, String tempFile)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
InputStream is = null;
FileOutputStream fos = null;
GetMethod method = null;
try {
// use disk to save by default
String source = checkUri(url.getFile());
method = new GetMethod(source, tempDir, tempFile);
client.executeMethod(method);
is = method.getData();
String tempFileName = tempDir + tempFile;
fos = new FileOutputStream(tempFileName);
int nb = 0;
byte[] buffer = new byte[4096];
while ((nb = is.read (buffer)) >= 0) {
fos.write (buffer, 0, nb);
}
fos.flush();
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
} finally {
try {
if (is != null) is.close();
if (fos != null) fos.close();
} catch(Exception ignored) {
}
}
int statusCode = method.getStatusCode();
return ((statusCode == WebdavStatus.SC_OK) ||
(statusCode == WebdavStatus.SC_CONTINUE)) ? true : false;
}
/**
* Execute the PUT method
*
* @param String data
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean putMethod(String data) throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
PutMethod method = null;
try {
String destination = checkUri(url.getFile());
method = new PutMethod(destination);
method.sendData(data);
client.executeMethod(method);
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return ((statusCode == WebdavStatus.SC_OK) ||
(statusCode == WebdavStatus.SC_CONTINUE)) ? true : false;
}
/**
* Execute the PUT method
*
* @param File file
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean putMethod(File file) throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
PutMethod method = null;
try {
String destination = checkUri(url.getFile());
method = new PutMethod(destination);
method.sendData(file);
client.executeMethod(method);
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return ((statusCode == WebdavStatus.SC_OK) ||
(statusCode == WebdavStatus.SC_CONTINUE)) ? true : false;
}
/**
* Execute OPTIONS method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean optionsMethod() throws WebdavException {
return optionsMethod(null);
}
/**
* Execute OPTIONS method
*
* @param String aMethod
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean optionsMethod(String aMethod) throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
boolean methodAllowed = false;
OptionsMethod method = null;
try {
method = new OptionsMethod(url.getFile());
client.executeMethod(method);
// check if the specific method is possbile
if (aMethod != null) {
Enumeration enum = method.getAllowedMethods();
while (enum.hasMoreElements()) {
if (aMethod.equalsIgnoreCase((String) enum.nextElement()))
methodAllowed = true;
}
}
} catch (WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch (Exception e) {
//file://e.printStackTrace();
}
if (aMethod != null) {
return methodAllowed;
} else {
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_OK) ? true : false;
}
}
/**
* Execute PROPFIND method with allprop
*
* @param URL url
* @param int depth
*
* @return Enumeration lists for a directory and a resource
*
* HOW TO USE: example)
*
* while (enum.hasMoreElements()) {
* Property property = (Property) enum.nextElement();
* if (property.getOwningURL().equals(dav_url) &&
* property.getLocalName().equals("dav property")) {
* RESULT = DOMUtils.getTextValue(property.getElement());
*
*/
public Enumeration propfindMethod(int depth)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
Enumeration urls = null;
PropFindMethod method = null;
try {
// change the depth with allprop
method = new PropFindMethod(url.getFile(), (short) depth);
// default depth=infinity, type=allprop
client.executeMethod(method);
urls = method.getAllResponseURLs();
} catch (WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch (Exception e) {
//file://e.printStackTrace();
}
return urls;
}
/**
* Execute PROPFIND method for checking a property
*
* @param propertyName one of the DAV properties
* @return Enumeration lists for a directory and a resource
*/
public Enumeration propfindMethod(String propertyName)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
Enumeration urls = null;
PropFindMethod method = null;
try {
Vector property = new Vector();
property.addElement(propertyName);
method = new PropFindMethod(url.getFile(), property.elements());
client.executeMethod(method);
urls = method.getAllResponseURLs();
while (urls.hasMoreElements()) {
;
// TODO: return value
//file://method.getResponseproperties(urls.nextElement());
}
} catch (WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch (Exception e) {
//file://e.printStackTrace();
}
return urls;
}
/**
* Execute PROPATCH method
*
* @param String propertyName
* @param String propertyValue
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean proppatchMethod(String propertyName, String propertyValue)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
PropPatchMethod method = null;
try {
method = new PropPatchMethod(url.getFile());
method.addPropertyToSet(propertyName, propertyValue);
client.executeMethod(method);
} catch (WebdavException we) {
// Possbile Status Codes => SC_OK
// WebdavStatus.SC_FORBIDDEN, SC_CONFLICT, SC_LOCKED, 507
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch (Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_OK) ? true : false;
}
/**
* Execute the HEAD method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean headMethod() throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
HeadMethod method = null;
try {
method = new HeadMethod(url.getFile());
client.executeMethod(method);
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
// TODO: analysis required HTTP/1.0
if (("" + statusCode).startsWith("2"))
return true;
else
return false;
}
/**
* Execute the DELETE method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean deleteMethod() throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
DeleteMethod method = null;
try {
method = new DeleteMethod(url.getFile());
client.executeMethod(method);
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
// TODO: analysis required HTTP/1.0
if (("" + statusCode).startsWith("2"))
return true;
else
return false;
}
/**
* Execute the MOVE method
*
* @param URL destination
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean moveMethod(URL destination)
throws WebdavException {
URL source = getURL();
WebdavClient client = clients.getWebdavClient(source);
MoveMethod method = null;
try {
//file://URL destURL = new URL(destination.getCanonicalpath());
//file://TODO:local to local else...
method = new MoveMethod(source.getFile(),
destination.getFile());
client.executeMethod(method);
} catch(WebdavException we) {
// Possbile MOVE Status Codes => SC_CREATED, SC_NO_CONTENT
// WebdavStatus.SC_FORBIDDEN, SC_CONFLICT,
// SC_PRECONDITION_FAILED,
// SC_LOCKED, SC_BAD_GATEWAY
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_CREATED ||
statusCode == WebdavStatus.SC_NO_CONTENT) ? true : false;
}
/**
* Execute the COPY method
*
* @param URL destination
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean copyMethod(URL destination)
throws WebdavException {
URL source = getURL();
WebdavClient client = clients.getWebdavClient(source);
CopyMethod method = null;
try {
//file://TODO:local to local else...
method = new CopyMethod(source.getFile(),
destination.getFile());
client.executeMethod(method);
} catch(WebdavException we) {
// Possbile COPY Status Codes => SC_CREATED, SC_NO_CONTENT
// WebdavStatus.SC_FORBIDDEN, SC_CONFLICT, SC_PRECONDITION_FAILED,
// SC_LOCKED, SC_BAD_GATEWAY, SC_INSUFFICIENT_STORAGE
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_CREATED ||
statusCode == WebdavStatus.SC_NO_CONTENT) ? true : false;
}
/**
* Execute the MKCOL method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean mkcolMethod() throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
MkcolMethod method = null;
try {
String path = checkUri(url.getFile());
method = new MkcolMethod(path);
client.executeMethod(method);
} catch(WebdavException we) {
// Possbile MKCOL Status Codes => SC_CREATED
// WebdavStatus.SC_FORBIDDEN, SC_METHOD_NOT_ALLOWED, SC_CONFLICT,
// SC_LOCKED, SC_UNSUPPORTED_MEDIA_TYPE, SC_INSUFFICIENT_STORAGE
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_CREATED) ? true : false;
}
/**
* Execute the LOCK method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean lockMethod() throws WebdavException {
// lock a resource shortly
return lockMethod(null, (short) 120);
}
/**
* Execute the LOCK method
*
* @param String owner
* @param short timetout
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean lockMethod(String owner, short timeout)
throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
if (owner == null)
owner = defaultOwner;
short lock = LockMethod.SCOPE_EXCLUSIVE;
LockMethod method = null;
try {
method =
new LockMethod(url.getFile(), owner, lock, timeout);
client.executeMethod(method);
clients.addLock(url, method.getLockToken());
} catch(WebdavException we) {
// Possbile LOCK Status Codes => SC_OK
// WebdavStatus.SC_SC_PRECONDITION_FAILED, SC_LOCKED
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
return (statusCode == WebdavStatus.SC_OK) ? true : false;
}
/**
* Execute the Unlock method
*
* @return boolean <code>true</code>, <code>false</code>
*/
public boolean unlockMethod() throws WebdavException {
URL url = getURL();
WebdavClient client = clients.getWebdavClient(url);
UnlockMethod method = null;
try {
method = new UnlockMethod();
//file://String lock = getLockToken(url);
String lock = clients.getLock(url);
method.setLockToken(lock);
client.executeMethod(method);
} catch(WebdavException we) {
throw new WebdavException
(we.getMessage(), method.getStatusCode());
//file://we.printStackTrace();
} catch(Exception e) {
//file://e.printStackTrace();
}
int statusCode = method.getStatusCode();
// TODO: analysis required HTTP/1.0
if (("" + statusCode).startsWith("2")) {
//file://removeLockToken(url);
clients.removeLock(url);
return true;
} else
return false;
}
/**
* Get the parent directory
*
* @return String
*/
public String getParent() {
String path = checkUri(url.getFile());
int i = path.lastIndexOf("/");
if (i > 0)
path = path.substring(0, i);
return path;
}
/**
* Get the resource name
*
* @return String
*/
public String getName() {
String path = checkUri(url.getFile());
int i = path.lastIndexOf("/");
return (i >= 0) ? path.substring(i+1) : path;
}
/**
* Compare to the WebdavResource object.
*
* @param WebdavResource other
* @return int
*/
public int compareToWebdavResource(WebdavResource other) {
try {
URL url = getURL();
String thisHost = url.getHost();
String otherHost= other.url.getHost();
if (thisHost == null)
thisHost = "";
if (otherHost == null)
otherHost = "";
if (!thisHost.equalsIgnoreCase(otherHost))
return thisHost.compareTo(otherHost);
int thisPort = url.getPort();
int otherPort= other.url.getPort();
if (thisPort == -1)
thisPort = 80;
if (otherPort == -1)
otherPort = 80;
if (thisPort != otherPort)
return (new Integer(thisPort)).compareTo
(new Integer(otherPort));
boolean thisCollection = isCollection();
boolean otherCollection = other.isCollection();
if (thisCollection && !otherCollection)
return -1;
if (otherCollection && !thisCollection)
return 1;
String thisPath = checkUri(url.getFile());
String otherPath= checkUri(other.url.getFile());
return thisPath.compareTo(otherPath);
// TODO: consider of more dead properties?
} catch (WebdavException we) {
System.out.println("DEBUG: File Not Found Exception?");
}
return 0;
}
/**
* Compare to the given object.
*
* @param Object o
* @return int
*/
public int compareTo(Object o) {
if ((o != null) && (o instanceof WebdavResource)) {
return compareToWebdavResource((WebdavResource) o);
}
String thisUrl = toString();
String otherUrl = o.toString();
return thisUrl.compareTo(otherUrl);
}
/**
* Test the object.
*
* @param Object obj
* @return boolean
*/
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof WebdavResource)) {
return compareTo((WebdavResource) obj) == 0;
}
return false;
}
/**
* toString()
*
* @return String
*/
public String toString() {
return url.toString();
}
}