1 16 package nl.hippo.cms.workflows.multiplesitessite; 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 47 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 public static void deletePathIfDangling(String location, String baseLocation, HttpClient httpClient) throws IOException , HttpException 103 { 104 DeleteMethod delete; 105 106 int indexOfLastSlash = location.lastIndexOf('/'); 107 while (location != null && indexOfLastSlash != -1) 108 { 109 location = location.substring(0, indexOfLastSlash); 110 if (location.equals(baseLocation)) 111 { 112 location = null; 113 } 114 else 115 { 116 117 PropFindMethod propfind = null; 118 boolean shouldDeleteFolder = false; 119 try 120 { 121 propfind = new PropFindMethod(location, 1, COLLECTION_LIST_PROPERTY_NAMES.elements()); 122 propfind.setDoAuthentication(true); 123 int propfindResult = httpClient.executeMethod(propfind); 124 Document responseDocument = propfind.getResponseDocument(); 126 if (responseDocument != null) 127 { 128 NodeList displayNames = responseDocument.getElementsByTagNameNS("DAV:", "displayname"); 129 shouldDeleteFolder = displayNames.getLength() == 1; 130 } 131 } 132 finally 133 { 134 if (propfind != null) 135 { 136 propfind.releaseConnection(); 137 } 138 } 139 140 if (shouldDeleteFolder) 141 { 142 delete = null; 143 try 144 { 145 delete = new DeleteMethod(location); 146 delete.setDoAuthentication(true); 147 int deleteResult = httpClient.executeMethod(delete); 148 } 150 finally 151 { 152 if (delete != null) 153 { 154 delete.releaseConnection(); 155 } 156 } 157 158 indexOfLastSlash = location.lastIndexOf('/'); 159 } 160 else 161 { 162 location = null; 163 } 164 } 165 } 166 } 167 168 } 169 | Popular Tags |