1 package nl.hippo.cms.workflows.reviewedactions; 2 3 import com.opensymphony.module.propertyset.PropertySet; 4 import com.opensymphony.workflow.WorkflowException; 5 import java.io.ByteArrayInputStream ; 6 import java.io.IOException ; 7 import java.util.Enumeration ; 8 import java.util.Map ; 9 import nl.hippo.cms.workflows.shared.FunctionProviderComponent; 10 import nl.hippo.cms.workflows.shared.URIUtil; 11 import org.apache.commons.httpclient.HttpClient; 12 import org.apache.commons.httpclient.HttpState; 13 import org.apache.commons.httpclient.methods.GetMethod; 14 import org.apache.commons.httpclient.methods.PutMethod; 15 import org.apache.webdav.lib.Property; 16 import org.apache.webdav.lib.methods.MkcolMethod; 17 import org.apache.webdav.lib.methods.PropFindMethod; 18 import org.apache.webdav.lib.methods.PropPatchMethod; 19 20 public class CopyObjectToLiveRepositoryFunction extends FunctionProviderComponent 21 { 22 23 public CopyObjectToLiveRepositoryFunction() 24 { 25 super(); 26 } 27 28 public void executeImpl(final Map transientVars, Map args, PropertySet ps) 29 throws WorkflowException 30 { 31 GetMethod get = null; 32 PutMethod put = null; 33 try 34 { 35 String previewLocation = URIUtil.getHTTPURI((String ) transientVars.get("location")); 36 String liveLocation = URIUtil.getLiveURI(previewLocation); 37 HttpState httpState = (HttpState) transientVars.get("httpstate"); 38 HttpClient httpClient = new HttpClient(); 39 httpClient.setState(httpState); 40 get = new GetMethod(previewLocation); 41 get.setDoAuthentication(true); 42 int getResult = httpClient.executeMethod(get); 43 byte[] body = get.getResponseBody(); 45 get.releaseConnection(); 46 47 ensureAncestorCollectionsExist(httpClient, liveLocation); 48 49 put = new PutMethod(liveLocation); 50 put.setDoAuthentication(true); 51 put.setRequestBody(new ByteArrayInputStream (body)); 52 int putResult = httpClient.executeMethod(put); 53 put.releaseConnection(); 55 56 copyProperties(httpClient, previewLocation, liveLocation); 57 } 58 catch (IOException e) 59 { 60 throw new WorkflowException(e); 61 } 62 finally 63 { 64 if (get != null) 65 { 66 get.releaseConnection(); 67 } 68 if (put != null) 69 { 70 put.releaseConnection(); 71 } 72 } 73 } 74 75 private void copyProperties(HttpClient httpClient, String previewLocation, String liveLocation) throws IOException 76 { 77 PropFindMethod propfind = null; 78 PropPatchMethod proppatch = null; 79 try 80 { 81 propfind = new PropFindMethod(previewLocation); 82 propfind.setDoAuthentication(true); 83 propfind.setDepth(0); 84 httpClient.executeMethod(propfind); 85 proppatch = new PropPatchMethod(liveLocation); 86 Enumeration responseUrls = propfind.getAllResponseURLs(); 87 if (responseUrls.hasMoreElements()) 88 { 89 Enumeration properties = propfind.getResponseProperties((String ) responseUrls 90 .nextElement()); 91 boolean executeProppatch = false; 92 while (properties.hasMoreElements()) 93 { 94 Property property = (Property) properties.nextElement(); 95 String namespace = property.getNamespaceURI(); 96 if (!namespace.equals("DAV:")) 97 { 98 String name = property.getLocalName(); 99 String value = property.getPropertyAsString(); 100 proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property", namespace); 101 executeProppatch = true; 102 } 103 } 104 if (executeProppatch) 105 { 106 try 107 { 108 proppatch.setDoAuthentication(true); 109 int status = httpClient.executeMethod(proppatch); 110 } 112 catch (IOException e) 113 { 114 } 116 finally 117 { 118 proppatch.releaseConnection(); 119 } 120 } 121 propfind.releaseConnection(); 122 } 123 } 124 finally 125 { 126 if (propfind != null) 127 { 128 propfind.releaseConnection(); 129 } 130 if (proppatch != null) 131 { 132 proppatch.releaseConnection(); 133 } 134 } 135 } 136 137 private void ensureAncestorCollectionsExist(HttpClient httpClient, String liveLocation) 138 throws IOException 139 { 140 MkcolMethod mkcol = null; 141 try 142 { 143 int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/', liveLocation 144 .indexOf('/') + 1) + 1); 145 String protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash); 146 String path = liveLocation.substring(locationOfThirdSlash); 147 path = path.substring(0, path.lastIndexOf('/')); 148 if (path.length() > 0) 149 { 150 String parentLocation = protocolHostAndPort + path; 151 ensureAncestorCollectionsExist(httpClient, parentLocation); 152 mkcol = new MkcolMethod(parentLocation); 153 mkcol.setDoAuthentication(true); 154 int mkcolResult = httpClient.executeMethod(mkcol); 155 if (mkcolResult != 201) 156 { 157 } 159 mkcol.releaseConnection(); 160 161 if (parentLocation.indexOf(".www/") != -1) 162 { 163 copyProperties(httpClient, parentLocation.replaceAll("\\.www/", ".preview/"), parentLocation); 164 } 165 } 166 } 167 finally 168 { 169 if (mkcol != null) 170 { 171 mkcol.releaseConnection(); 172 } 173 } 174 } 175 176 } | Popular Tags |