1 16 package nl.hippo.cms.workflows.multiplesitessite; 17 18 import com.opensymphony.module.propertyset.PropertySet; 19 import com.opensymphony.workflow.WorkflowException; 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 import java.util.Map ; 23 import nl.hippo.cms.repositorylocation.RepositoryInformation; 24 import nl.hippo.cms.workflows.shared.FunctionProviderComponent; 25 import org.apache.commons.httpclient.HttpClient; 26 import org.apache.webdav.lib.Property; 27 import org.apache.webdav.lib.methods.MkcolMethod; 28 import org.apache.webdav.lib.methods.PropFindMethod; 29 import org.apache.webdav.lib.methods.PropPatchMethod; 30 31 public abstract class ObjectCopyingComponent extends FunctionProviderComponent 32 { 33 34 public ObjectCopyingComponent() 35 { 36 super(); 37 } 38 39 public void executeImpl(Map arg0, Map arg1, PropertySet arg2) throws WorkflowException 40 { 41 } 42 43 protected static void copyProperties(HttpClient sourceHttpClient, String sourceLocation, HttpClient destinationHttpClient, String destinationLocation) throws IOException 44 { 45 PropFindMethod propfind = null; 46 PropPatchMethod proppatch = null; 47 try 48 { 49 propfind = new PropFindMethod(sourceLocation); 50 propfind.setDoAuthentication(true); 51 propfind.setDepth(0); 52 sourceHttpClient.executeMethod(propfind); 53 proppatch = new PropPatchMethod(destinationLocation); 54 Enumeration responseUrls = propfind.getAllResponseURLs(); 55 if (responseUrls.hasMoreElements()) 56 { 57 Enumeration properties = propfind.getResponseProperties((String ) responseUrls 58 .nextElement()); 59 boolean executeProppatch = false; 60 while (properties.hasMoreElements()) 61 { 62 Property property = (Property) properties.nextElement(); 63 String namespace = property.getNamespaceURI(); 64 if (!namespace.equals("DAV:")) 65 { 66 String name = property.getLocalName(); 67 String value = property.getPropertyAsString(); 68 proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property", 69 namespace); 70 executeProppatch = true; 71 } 72 } 73 if (executeProppatch) 74 { 75 try 76 { 77 proppatch.setDoAuthentication(true); 78 int status = destinationHttpClient.executeMethod(proppatch); 79 } 81 catch (IOException e) 82 { 83 } 85 finally 86 { 87 proppatch.releaseConnection(); 88 } 89 } 90 propfind.releaseConnection(); 91 } 92 } 93 finally 94 { 95 if (propfind != null) 96 { 97 propfind.releaseConnection(); 98 } 99 if (proppatch != null) 100 { 101 proppatch.releaseConnection(); 102 } 103 } 104 } 105 106 protected static void ensureAncestorCollectionsExist(HttpClient sourceHttpClient, RepositoryInformation sourceRepoInformation, HttpClient destinationHttpClient, RepositoryInformation destinationRepoInformation, String location) throws IOException 107 { 108 MkcolMethod mkcol = null; 109 try 110 { 111 int indexOfLastSlash = location.lastIndexOf('/'); 112 if (indexOfLastSlash != -1) 113 { 114 String parentLocation = location.substring(0, indexOfLastSlash); 115 if (parentLocation.length() > 0) 116 { 117 ensureAncestorCollectionsExist(sourceHttpClient, sourceRepoInformation, destinationHttpClient, destinationRepoInformation, parentLocation); 118 mkcol = new MkcolMethod(destinationRepoInformation.getAbsoluteUri(parentLocation)); 119 mkcol.setDoAuthentication(true); 120 int mkcolResult = destinationHttpClient.executeMethod(mkcol); 121 if (mkcolResult == 201) 122 { 123 copyProperties(sourceHttpClient, sourceRepoInformation.getAbsoluteUri(parentLocation), destinationHttpClient, destinationRepoInformation.getAbsoluteUri(parentLocation)); 124 } 125 else 126 { 127 } 129 mkcol.releaseConnection(); 130 } 131 else 132 { 133 ensureAncestorCollectionsExist(destinationHttpClient, destinationRepoInformation.getAbsoluteUri(location)); 134 } 135 } 136 } 137 finally 138 { 139 if (mkcol != null) 140 { 141 mkcol.releaseConnection(); 142 } 143 } 144 } 145 146 protected static void ensureAncestorCollectionsExist(HttpClient httpClient, String location) throws IOException 147 { 148 MkcolMethod mkcol = null; 149 try 150 { 151 int locationOfThirdSlash = location.indexOf('/', location.indexOf('/', 152 location.indexOf('/') + 1) + 1); 153 String protocolHostAndPort = location.substring(0, locationOfThirdSlash); 154 String path = location.substring(locationOfThirdSlash); 155 path = path.substring(0, path.lastIndexOf('/')); 156 if (path.length() > 0) 157 { 158 String parentLocation = protocolHostAndPort + path; 159 ensureAncestorCollectionsExist(httpClient, parentLocation); 160 mkcol = new MkcolMethod(parentLocation); 161 mkcol.setDoAuthentication(true); 162 int mkcolResult = httpClient.executeMethod(mkcol); 163 if (mkcolResult != 201) 164 { 165 } 167 mkcol.releaseConnection(); 168 } 169 } 170 finally 171 { 172 if (mkcol != null) 173 { 174 mkcol.releaseConnection(); 175 } 176 } 177 } 178 } 179 | Popular Tags |