1 21 package org.apache.webdav.ant; 22 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Vector ; 29 30 import org.apache.commons.httpclient.HttpClient; 31 import org.apache.commons.httpclient.HttpURL; 32 import org.apache.commons.httpclient.URIException; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.webdav.lib.PropertyName; 36 import org.apache.webdav.lib.methods.DepthSupport; 37 import org.apache.webdav.lib.methods.PropFindMethod; 38 import org.apache.webdav.lib.properties.ResourceTypeProperty; 39 40 45 public class CollectionScanner extends Scanner { 46 47 private HttpURL baseURL = null; 48 private HttpClient client = null; 49 private ResourceProperties properties = null; 50 51 private Vector propertyNames = new Vector (); 52 53 public CollectionScanner() { 54 propertyNames.add(Utils.RESOURCETYPE); 55 propertyNames.add(Utils.GETLASTMODIFIED); 56 } 57 58 69 public void scan() { 70 71 if (baseURL == null) { 72 throw new IllegalStateException ( 73 "BaseURL must be set before calling the scan() method"); 74 } 75 76 filesIncluded = new ArrayList (); 78 filesExcluded = new ArrayList (); 79 filesNotIncluded = new ArrayList (); 80 dirsIncluded = new ArrayList (); 81 dirsExcluded = new ArrayList (); 82 dirsNotIncluded = new ArrayList (); 83 this.properties = new ResourceProperties(); 84 85 try { 86 readCollection(baseURL); 87 } 88 catch (Exception e) { 92 e.printStackTrace(); 93 } 94 } 95 96 protected void readCollection(HttpURL collURL) 97 throws URIException 98 { 99 if (!collURL.getPath().endsWith(SEPARATOR)) { 100 collURL = Utils.createHttpURL(collURL, ""); 101 collURL.setPath(collURL.getPath() + SEPARATOR); 102 } 103 104 PropFindMethod propFind = new PropFindMethod(collURL.getURI(), 106 DepthSupport.DEPTH_1, 107 PropFindMethod.BY_NAME); 108 propFind.setPropertyNames(propertyNames.elements()); 109 propFind.setFollowRedirects(true); 110 try { 111 this.client.executeMethod(propFind); 112 } 113 catch (IOException e) { 114 Utils.makeBuildException("Can't read collection content!", e); 115 } 116 117 List subCollections = new ArrayList (); 118 this.properties.storeProperties(propFind); 119 120 addResource(collURL.getPath(), true); 122 123 for (Enumeration e = propFind.getAllResponseURLs(); e.hasMoreElements(); ) 125 { 126 String href = (String ) e.nextElement(); 127 128 ResourceTypeProperty property = 129 this.properties.getResourceType(collURL, href); 130 131 if (property != null) { 132 if (property.isCollection()) { 133 if (!href.endsWith(SEPARATOR)) href = href + SEPARATOR; 134 HttpURL sub = Utils.createHttpURL(collURL, href); 137 if (!sub.equals(collURL)) { 138 subCollections.add(Utils.createHttpURL(collURL, href)); 139 } 140 } else { 141 addResource(href, false); 142 } 143 } else { 144 throw new BuildException("Can't determine resourcetype."); 145 } 146 } 147 148 for(Iterator i = subCollections.iterator(); i.hasNext();) { 150 readCollection((HttpURL)i.next()); 151 } 152 } 153 154 protected void addResource(String href, boolean isCollection) 155 throws ScanException 156 { 157 try { 158 String path = (Utils.createHttpURL(getBaseURL(), href)).getPath(); 159 String relPath = path.substring(getBaseURL().getPath().length()); 160 if (relPath.startsWith(SEPARATOR)) { 161 relPath = relPath.substring(1); 162 } 163 if (isCollection) { 164 if (isIncluded(relPath)) { 165 if (isExcluded(relPath)) { 166 dirsExcluded.add(relPath); 167 } else { 168 dirsIncluded.add(relPath); 169 } 170 } else { 171 dirsNotIncluded.add(relPath); 172 } 173 } else { 174 if (isIncluded(relPath)) { 175 if (isExcluded(relPath)) { 176 filesExcluded.add(relPath); 177 } else { 178 filesIncluded.add(relPath); 179 } 180 } else { 181 filesNotIncluded.add(relPath); 182 } 183 } 184 } 185 catch (URIException e) { 186 throw new ScanException( 187 "The XML response returned an invalid URL: " + e.getMessage(), e); 188 } 189 } 190 191 public HttpURL getBaseURL() { 192 return this.baseURL; 193 } 194 195 public void setBaseURL(HttpURL baseURL) { 196 this.baseURL = baseURL; 197 } 198 199 public void setHttpClient(HttpClient client) { 200 this.client = client; 201 } 202 203 public ResourceProperties getProperties() 204 { 205 return this.properties; 206 } 207 208 213 public void addProperty(PropertyName property) { 214 if (property == null) throw new NullPointerException (); 215 this.propertyNames.add(property); 216 } 217 } 218 | Popular Tags |