jericho     01/03/06 04:51:45

  Modified:    src/webdav/client/src/org/apache/webdav/util
                        WebdavResource.java
  Log:
  - Make the HTTP-date format checking stronger.
  - Have XML response as the return value of the propfind method with depth.
  
  Revision  Changes    Path
  1.9       +90 -95    
jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java
  
  Index: WebdavResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- WebdavResource.java       2001/03/05 15:00:56     1.8
  +++ WebdavResource.java       2001/03/06 12:51:42     1.9
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v
 1.8 2001/03/05 15:00:56 jericho Exp $
  - * $Revision: 1.8 $
  - * $Date: 2001/03/05 15:00:56 $
  + * $Header: 
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v
 1.9 2001/03/06 12:51:42 jericho Exp $
  + * $Revision: 1.9 $
  + * $Date: 2001/03/06 12:51:42 $
    *
    * ====================================================================
    *
  @@ -68,16 +68,18 @@
   import java.io.IOException;
   import java.io.FileOutputStream;
   import java.util.Date;
  +import java.util.Locale;
   import java.util.Vector;
   import java.util.Hashtable;
   import java.util.Enumeration;
   import java.text.DateFormat;
  +import java.text.ParseException;
  +import java.text.SimpleDateFormat;
   import java.net.URL;
   import java.net.MalformedURLException;
   import org.apache.util.DOMUtils;
   import org.apache.webdav.lib.*;
   import org.apache.webdav.lib.methods.*;
  -import org.apache.webdav.lib.properties.GetLastModifiedProperty;
   import org.apache.webdav.lib.properties.ResourceTypeProperty;
   
   /**
  @@ -167,6 +169,17 @@
       public static final String SUPPORTEDLOCK = "supportedlock";
   
   
  +    /**
  +     * Date formats using for Date parsing.
  +     */
  +    protected static final SimpleDateFormat formats[] = {
  +        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
  +        new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US),
  +        new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
  +        new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US)
  +    };
  +
  +
       // --------------------------------------------------- Instance Variables
   
   
  @@ -290,7 +303,7 @@
       /**
        * A WebDAV property, getlastmodified;
        */
  -    private Date getLastModified;
  +    private String getLastModified;
   
   
       /**
  @@ -520,62 +533,48 @@
   
           // For checking existence of http URL, make false by default always.
           exists = false;
  +        // Basically, get resources with depth 1.
  +        Enumeration responses = propfindMethod(DepthSupport.DEPTH_1);
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  -
  -        // Change the depth with allprop for listing.
  -        // depth:infinity => 1, type:allprop
  -        PropFindMethod method = new PropFindMethod(httpUrl.getPath(), 1);
  -        client.executeMethod(method);
  -
  -        // Save the status code at the most recent.
  -        setStatusCode(method.getStatusCode());
  -
  -        // Getting the result.
  -        Enumeration httpUrls = method.getAllResponseURLs();
  -
           // Prepare the hrefTable empty.
           hrefTable.clear();
  -        // If there is some result.
  -        while (httpUrls.hasMoreElements()) {
  +        while (responses.hasMoreElements()) {
   
  +            ResponseEntity response =
  +                (ResponseEntity) responses.nextElement();
  +
               boolean itself = false;
  +                 String httpUrlPath = httpUrl.getPath();
  +            String href = (String) response.getHref();
  +                 String hrefPath = HttpURL.getPath(href);
  +                 int compared = httpUrlPath.compareTo(hrefPath);
  +            // Compare with the href path and requested-path itself.
  +                     if (compared == 0 || compared == -1 && hrefPath.endsWith("/") 
||
  +                compared == 1 && httpUrlPath.endsWith("/")) {
  +
  +                if (response.getStatusCode() > 0)
  +                    setStatusCode(response.getStatusCode());
  +                         exists = true;
  +                         itself = true;
  +            }
  +
               // Table for the href and its properties
               Hashtable hrefProperties = new Hashtable();
  -            String href = (String) httpUrls.nextElement();
  -            Enumeration properties = method.getResponseProperties(href);
  -            while (properties.hasMoreElements()) {
  -                Property property = (Property) properties.nextElement();
  -                // ifself
  -                String httpUrlPath = httpUrl.getPath();
  -                String owningPath = HttpURL.getPath(property.getOwningURL());
  -                // Compare with the owning path and itself.
  -                if (httpUrlPath.equals(owningPath)) {
  -                    exists = true;
  -                    itself = true;
  -                } else {
  -                    int compared = httpUrlPath.compareTo(owningPath);
  -                    if (compared == -1 && owningPath.endsWith("/")) {
  -                        exists = true;
  -                        itself = true;
  -                        httpUrl = new HttpURL(httpUrl.toString() + "/");
  -                    } else if (compared == 1 && httpUrlPath.endsWith("/")) {
  -                        exists = true;
  -                        itself = true;
  -                    }
  -                }
  +                     Enumeration properties = response.getProperties();
  +                     while (properties.hasMoreElements()) {
  +
  +                             Property property = (Property) 
properties.nextElement();
  +
   
                   // ------------------------------  Checking WebDAV properties
   
                   if (property.getLocalName().equals(DISPLAYNAME)) {
  -                    displayName =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     displayName = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(DISPLAYNAME, displayName);
                   } else
                   if (property.getLocalName().equals(GETCONTENTLENGTH)) {
  -                    getContentLength =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     getContentLength = 
property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put
                               (GETCONTENTLENGTH, getContentLength);
  @@ -586,44 +585,37 @@
                           hrefProperties.put(RESOURCETYPE, resourceType);
                   } else
                   if (property.getLocalName().equals(GETCONTENTTYPE)) {
  -                    getContentType =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     getContentType = 
property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(GETCONTENTTYPE, getContentType);
                   } else
                   if (property.getLocalName().equals(GETLASTMODIFIED)) {
  -                    getLastModified =
  -                        ((GetLastModifiedProperty) property).getDate();
  +                    getLastModified = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(GETLASTMODIFIED, getLastModified);
                   } else
                   if (property.getLocalName().equals(CREATIONDATE)) {
  -                    creationDate =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     creationDate = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(CREATIONDATE, creationDate);
                   } else
                   if (property.getLocalName().equals(GETETAG)) {
  -                    getEtag =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     getEtag = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(GETETAG, getEtag);
                   } else
                   if (property.getLocalName().equals(ISHIDDEN)) {
  -                    isHidden=
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     isHidden= property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(ISHIDDEN, isHidden);
                   } else
                   if (property.getLocalName().equals(ISCOLLECTION)) {
  -                    isCollection =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     isCollection = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(ISCOLLECTION, isCollection);
                   } else
                   if (property.getLocalName().equals(SUPPORTEDLOCK)) {
  -                    supportedLock =
  -                        DOMUtils.getTextValue(property.getElement());
  +                                     supportedLock = property.getPropertyAsString();
                       if (!itself)
                           hrefProperties.put(SUPPORTEDLOCK, supportedLock);
                   }
  @@ -634,9 +626,6 @@
   
           // The given http URL checked.
           setUsed();
  -
  -        // Set the new WebDAV client.
  -        clients.setSession(httpUrl, client);
       }
   
   
  @@ -698,7 +687,6 @@
       }
   
   
  -
       /**
        * Get the value of DAV property, getlastmodified.
        *
  @@ -706,10 +694,31 @@
        */
       public long getGetLastModified() {
   
  -        return getLastModified.getTime();
  +        Date date = parseDate(getLastModified);
  +
  +        return date.getTime();
       }
   
   
  +     /**
  +      * Parse the <code>java.util.Date</code> string for HTTP-date.
  +      *
  +      * @return The parsed date.
  +      */
  +     public Date parseDate(String dateValue) {
  +
  +        Date date = null;
  +        for (int i = 0; (date == null) && (i < formats.length); i++) {
  +            try {
  +                date = formats[i].parse(dateValue);
  +            } catch (ParseException e) {
  +            }
  +        }
  +
  +        return date;
  +     }
  +
  +
       /**
        * Get the value of DAV property, creationdate.
        *
  @@ -939,10 +948,11 @@
                       (String) pairProperties.get(GETCONTENTTYPE);
                   longFormat[2] = resourceTypeProperty.isCollection() ?
                       "COLLECTION" : getContentType ;
  -                Date getLastModified =
  -                    (Date) pairProperties.get(GETLASTMODIFIED);
  +             String getLastModified =
  +                    (String) pairProperties.get(GETLASTMODIFIED);
  +                Date date = parseDate(getLastModified);
                   longFormat[3] =
  -                    DateFormat.getDateTimeInstance().format(getLastModified);
  +                    DateFormat.getDateTimeInstance().format(date);
                   hrefList.addElement(longFormat);
               } catch (Exception e) {
                   // e.printStackTrace();
  @@ -1293,12 +1303,15 @@
        * Execute PROPFIND method with allprop for this WebdavResource.
        * Get list of all WebDAV properties on this WebDAV resource.
        *
  +     * <p>Once used this method, the the status code in the 207
  +     * reponse is need to be set for the method of WebdavResource.
  +     *
        * @param depth
  -     * @return Enumeration list of all WebDAV properties on a resource.
  +     * @return the response entity body that has pairs of href and properties.
        * @exception WebdavException
        * @exception IOException
        */
  -    public Enumeration propfindMethod(short depth)
  +    public Enumeration propfindMethod(int depth)
           throws WebdavException, IOException {
   
           return propfindMethod(httpUrl.getPath(), depth);
  @@ -1309,49 +1322,31 @@
        * Execute PROPFIND method with allprop for the given path.
        * Get list of all WebDAV properties on the given resource.
        *
  -     * HOW TO USE: example)
  -     * while (enum.hasMoreElements()) {
  -     *      Property property = (Property) enum.nextElement();
  -     *      if (property.getOwningHttpURL().equals(dav_httpUrl) &&
  -     *          property.getLocalName().equals("dav property")) {
  -     *              RESULT = DOMUtils.getTextValue(property.getElement());
  +     * <p>Once used this method, the the status code in the 207
  +     * reponse is need to be set for the method of WebdavResource.
        *
        * @param path the path to request.
        * @param depth
  -     * @return Enumeration list of all WebDAV properties on a resource.
  +     * @return the response entity body that has pairs of href and properties.
        * @exception WebdavException
        * @exception IOException
        */
  -    public Enumeration propfindMethod(String path, short depth)
  +    public Enumeration propfindMethod(String path, int depth)
           throws WebdavException, IOException {
  -
  -        WebdavClient client = clients.getSessionInstance(this.httpUrl);
   
  +        WebdavClient client = clients.getSessionInstance(httpUrl);
   
           // Check the path alright.
           path = HttpURL.getPath(path);
           // Change the depth for allprop
  -        PropFindMethod method = new PropFindMethod(path , depth);
  +        PropFindMethod method = new PropFindMethod(path, depth);
           // Default depth=infinity, type=allprop
           client.executeMethod(method);
   
           // Set the status code.
           setStatusCode(method.getStatusCode());
  -
  -        HttpURL httpUrl = new HttpURL(this.httpUrl.getAuthority() + path);
  -        String href = httpUrl.toURL().toString();
  -        Enumeration enum = method.getResponseProperties(href);
  -        while (enum.hasMoreElements()) {
  -            Property property = (Property) enum.nextElement();
  -            if (property.getOwningURL().equals(href) &&
  -                property.getStatusCode() > 0) {
  -                // Set the right status code by multi-status.
  -                setStatusCode(property.getStatusCode());
  -            }
  -        }
   
  -        // FIXME: the better answer to manipulate?
  -        return method.getAllResponseURLs();
  +     return method.getResponses();
       }
   
   
  
  
  

Reply via email to