KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > protocol > http > DavURLLister


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.net.protocol.http;
23
24 import java.io.IOException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpURL;
33 import org.apache.webdav.lib.WebdavResource;
34 import org.jboss.net.protocol.URLListerBase;
35
36 public class DavURLLister extends URLListerBase
37 {
38    public Collection JavaDoc listMembers (URL JavaDoc baseUrl, URLFilter filter) throws IOException JavaDoc
39    {
40       return listMembers (baseUrl, filter, false);
41    }
42
43    public Collection JavaDoc listMembers (URL JavaDoc baseUrl, URLFilter filter, boolean scanNonDottedSubDirs) throws IOException JavaDoc
44    {
45       WebdavResource resource = null;
46       try
47       {
48          resource = new WebdavResource (baseUrl.toString ());
49          WebdavResource[] resources = resource.listWebdavResources ();
50          List JavaDoc urls = new ArrayList JavaDoc (resources.length);
51          for (int i = 0; i < resources.length; i++)
52          {
53             WebdavResource member = resources[i];
54             HttpURL httpURL = member.getHttpURL ();
55             if (filter.accept (baseUrl, httpURL.getName ()))
56             {
57                String JavaDoc uri = httpURL.getURI();
58                if (member.isCollection ())
59                {
60                   if (! uri.endsWith ("/"))
61                      uri += "/";
62
63                   // it is a directory: do we have to recursively list its content?
64
String JavaDoc path = httpURL.getPath();
65                   if (scanNonDottedSubDirs && getFilePartFromUrl(path).indexOf (".") == -1)
66                   {
67                      URL JavaDoc subUrl = new URL JavaDoc (uri) ;
68                      urls.addAll (listMembers (subUrl, filter, scanNonDottedSubDirs));
69                   }
70                   else
71                   {
72                      urls.add (new URL JavaDoc (uri));
73                   }
74                }
75                else
76                {
77                   urls.add (new URL JavaDoc (uri));
78                }
79                
80             }
81          }
82          return urls;
83       } catch (HttpException e)
84       {
85          throw new IOException JavaDoc (e.getMessage ());
86       } catch (MalformedURLException JavaDoc e)
87       {
88          // should not happen
89
throw new IllegalStateException JavaDoc (e.getMessage ());
90       } finally
91       {
92          if (resource != null)
93          {
94             resource.close ();
95          }
96       }
97    }
98    
99    protected static final String JavaDoc getFilePartFromUrl (String JavaDoc name)
100    {
101       int length = name.length ();
102       
103       if (name.charAt (length - 1) == '/')
104       {
105          int start = name.lastIndexOf ("/", length - 2);
106          return name.substring (start, length -2);
107       }
108       else
109       {
110          int start = name.lastIndexOf ("/");
111          return name.substring (start);
112       }
113    }
114 }
115
Popular Tags