1 16 package org.apache.cocoon.components.webdav; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.Vector ; 27 28 import org.apache.cocoon.components.source.helpers.SourceProperty; 29 import org.apache.commons.httpclient.HttpException; 30 import org.apache.commons.httpclient.HttpURL; 31 import org.apache.webdav.lib.Property; 32 import org.apache.webdav.lib.PropertyName; 33 import org.apache.webdav.lib.ResponseEntity; 34 import org.apache.webdav.lib.WebdavResource; 35 36 39 public class WebDAVUtil { 40 41 static private String staticURI; 42 static private WebdavResource staticResource; 43 44 51 static synchronized public WebdavResource getWebdavResource(String uri) 52 throws HttpException, IOException { 53 54 if (uri == null) return null; 55 if (uri.equals(staticURI)) { 56 staticResource.discoverOwnLocks(); 57 return staticResource; 58 } 59 HttpURL sourceURL = new HttpURL(uri); 60 staticURI = uri; 61 staticResource = new WebdavResource(sourceURL); 62 staticResource.discoverOwnLocks(); 63 return staticResource; 64 } 65 66 74 static public void createResource(final String uri, final String content) 75 throws HttpException, IOException { 76 77 final String filename = uri.substring(uri.lastIndexOf("/")); 78 final String uriPrefix = uri.substring(0, uri.lastIndexOf("/") + 1); 79 final HttpURL sourceURL = new HttpURL(uri); 80 final WebdavResource resource = getWebdavResource(uriPrefix); 81 82 if(!resource.putMethod(uriPrefix + filename, content)) { 83 throw new HttpException("Error creating resource: " + uri 84 + " Status: " + resource.getStatusCode() 85 + " Message: " + resource.getStatusMessage()); 86 } 87 } 88 89 99 static public void copyResource(String from, String to, boolean recurse, boolean overwrite) 100 throws HttpException, IOException { 101 102 String relativeDestination = (to.substring(to.indexOf("://") + 3)); 103 relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/")); 104 105 if (recurse) WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/"))); 107 108 WebdavResource resource = WebDAVUtil.getWebdavResource(from); 110 resource.setOverwrite(overwrite); 111 if (!resource.copyMethod(relativeDestination)) { 112 throw new HttpException("Error copying resource: " + from 113 + " Status: " + resource.getStatusCode() 114 + " Message: " + resource.getStatusMessage()); 115 } 116 } 117 118 128 static public void moveResource(String from, String to, boolean recurse, boolean overwrite) 129 throws HttpException, IOException { 130 131 String relativeDestination = (to.substring(to.indexOf("://") + 3)); 132 relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/")); 133 134 if (recurse) WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/"))); 136 137 WebdavResource resource = WebDAVUtil.getWebdavResource(from); 139 resource.setOverwrite(overwrite); 140 if (!resource.moveMethod(relativeDestination)) { 141 throw new HttpException("Error moving resource: " + from 142 + " Status: " + resource.getStatusCode() 143 + " Message: " + resource.getStatusMessage()); 144 } 145 } 146 147 154 static public void makePath(String path) 155 throws HttpException, IOException { 156 String parentPath = path; 157 while (true) { 158 159 try { 160 HttpURL sourceURL = new HttpURL(parentPath+"/"); 161 new WebdavResource(sourceURL); 162 163 break; 165 } catch (HttpException he) { 166 parentPath = parentPath.substring(0, parentPath.lastIndexOf("/")); 167 } 168 } 169 170 if(parentPath.length() < path.length()) { 172 String pathToMake = path.substring(parentPath.length()+1)+"/"; 173 String colToMake = null; 174 while (pathToMake.indexOf("/") != -1) { 175 colToMake = pathToMake.substring(0, pathToMake.indexOf("/")); 176 WebDAVUtil.makeCollection(path.substring(0, path.lastIndexOf(colToMake)), colToMake); 177 pathToMake = pathToMake.substring(pathToMake.indexOf("/")+1); 178 } 179 } 180 } 181 182 190 static public void makeCollection(String parent, String collection) 191 throws HttpException, IOException { 192 193 WebdavResource parentResource = WebDAVUtil.getWebdavResource(parent); 194 parentResource.mkcolMethod(parent + collection + "/"); 195 } 196 197 206 static public SourceProperty getProperty(String uri, String name, String namespace) 207 throws HttpException, IOException { 208 209 Vector propNames = new Vector (1); 210 propNames.add(new PropertyName(namespace,name)); 211 Enumeration props= null; 212 Property prop = null; 213 WebdavResource resource = WebDAVUtil.getWebdavResource(uri); 214 Enumeration responses = resource.propfindMethod(0, propNames); 215 while (responses.hasMoreElements()) { 216 ResponseEntity response = (ResponseEntity)responses.nextElement(); 217 props = response.getProperties(); 218 if (props.hasMoreElements()) { 219 prop = (Property) props.nextElement(); 220 return new SourceProperty(prop.getElement()); 221 } 222 } 223 return null; 224 } 225 226 234 static public Map getProperties(String uri, Set propNames) 235 throws HttpException, IOException { 236 237 List sourceproperties = new ArrayList (); 238 Enumeration responses = null; 239 Enumeration props = null; 240 Property prop = null; 241 Map propertiesMap = new HashMap (); 242 WebdavResource resource = WebDAVUtil.getWebdavResource(uri); 243 responses = resource.propfindMethod(0, new Vector (propNames)); 244 while (responses.hasMoreElements()) { 245 ResponseEntity response = (ResponseEntity)responses.nextElement(); 246 props = response.getProperties(); 247 while (props.hasMoreElements()) { 248 prop = (Property) props.nextElement(); 249 SourceProperty srcProperty = new SourceProperty(prop.getElement()); 250 sourceproperties.add(srcProperty); 251 } 252 } 253 254 for (int i = 0; i<sourceproperties.size(); i++) { 255 propertiesMap.put(((SourceProperty) sourceproperties.get(i)).getNamespace() 256 + ":" + ((SourceProperty) sourceproperties.get(i)).getName(), 257 sourceproperties.get(i)); 258 } 259 return propertiesMap; 260 } 261 262 269 static public List getAllProperties(String uri) 270 throws HttpException, IOException { 271 272 List sourceproperties = new ArrayList (); 273 WebdavResource resource = WebDAVUtil.getWebdavResource(uri); 274 Enumeration responses = resource.propfindMethod(0); 275 Enumeration props = null; 276 Property prop = null; 277 while (responses.hasMoreElements()) { 278 ResponseEntity response = (ResponseEntity)responses.nextElement(); 279 props = response.getProperties(); 280 while (props.hasMoreElements()) { 281 prop = (Property) props.nextElement(); 282 SourceProperty srcProperty = new SourceProperty(prop.getElement()); 283 sourceproperties.add(srcProperty); 284 } 285 } 286 return sourceproperties; 287 } 288 289 299 static public void setProperty(String uri, String name, String namespace, String value) 300 throws HttpException, IOException { 301 302 WebdavResource resource = WebDAVUtil.getWebdavResource(uri); 303 if(!resource.proppatchMethod(new PropertyName(namespace, name), value, true)) { 304 throw new HttpException("Error setting property " + namespace + ":" + name + " on resource: " + uri 305 + " Status: " + resource.getStatusCode() 306 + " Message: " + resource.getStatusMessage()); 307 } 308 } 309 310 318 static public void setProperties(String uri, Map properties) 319 throws HttpException, IOException { 320 321 WebdavResource resource = WebDAVUtil.getWebdavResource(uri); 322 if (!resource.proppatchMethod(new Hashtable (properties), true)) { 323 throw new HttpException("Error setting properties on resource: " + uri 324 + " Status: " + resource.getStatusCode() 325 + " Message: " + resource.getStatusMessage()); 326 } 327 } 328 329 } | Popular Tags |