1 16 package nl.hippo.cms.workflows.reviewedactions; 17 18 import java.io.IOException ; 19 import java.util.Vector ; 20 import org.apache.commons.httpclient.HttpClient; 21 import org.apache.commons.httpclient.HttpException; 22 import org.apache.commons.httpclient.methods.DeleteMethod; 23 import org.apache.webdav.lib.PropertyName; 24 import org.apache.webdav.lib.methods.PropFindMethod; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.NodeList ; 27 28 public class RepositoryHelper 29 { 30 31 private static final Vector COLLECTION_LIST_PROPERTY_NAMES = new Vector (); 32 33 static 34 { 35 COLLECTION_LIST_PROPERTY_NAMES.add(new PropertyName("DAV:", "displayname")); 36 } 37 38 private RepositoryHelper() 39 { 40 super(); 41 } 42 43 public static void deletePathIfDangling(String location, HttpClient httpClient) throws IOException , HttpException 44 { 45 DeleteMethod delete; 46 int indexOfLastSlash = location.lastIndexOf('/'); 48 while (location != null && indexOfLastSlash != -1) 49 { 50 location = location.substring(0, indexOfLastSlash); 51 52 PropFindMethod propfind = null; 53 boolean shouldDeleteFolder = false; 54 try 55 { 56 propfind = new PropFindMethod(location, 1, COLLECTION_LIST_PROPERTY_NAMES.elements()); 57 propfind.setDoAuthentication(true); 58 int propfindResult = httpClient.executeMethod(propfind); 59 Document responseDocument = propfind.getResponseDocument(); 61 if (responseDocument != null) 62 { 63 NodeList displayNames = responseDocument.getElementsByTagNameNS("DAV:", "displayname"); 64 shouldDeleteFolder = displayNames.getLength() == 1; 65 } 66 } 67 finally 68 { 69 if (propfind != null) 70 { 71 propfind.releaseConnection(); 72 } 73 } 74 75 if (shouldDeleteFolder) 76 { 77 delete = null; 78 try 79 { 80 delete = new DeleteMethod(location); 81 delete.setDoAuthentication(true); 82 int deleteResult = httpClient.executeMethod(delete); 83 } 85 finally 86 { 87 if (delete != null) 88 { 89 delete.releaseConnection(); 90 } 91 } 92 93 indexOfLastSlash = location.lastIndexOf('/'); 94 } 95 else 96 { 97 location = null; 98 } 99 } 100 } 101 102 } 103 | Popular Tags |