1 package nl.hippo.cms.workflows.reviewedactions; 2 3 import com.opensymphony.workflow.WorkflowException; 4 import java.io.ByteArrayInputStream ; 5 import java.io.IOException ; 6 import java.text.DateFormat ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 9 import java.util.Enumeration ; 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.Set ; 13 import nl.hippo.cms.Constants; 14 import nl.hippo.cms.workflows.shared.FunctionProviderComponent; 15 import nl.hippo.cms.workflows.shared.URIUtil; 16 import nl.hippo.cocoon.webdav.WebDAVHelper; 17 import org.apache.commons.httpclient.HttpClient; 18 import org.apache.commons.httpclient.HttpState; 19 import org.apache.commons.httpclient.methods.GetMethod; 20 import org.apache.commons.httpclient.methods.PutMethod; 21 import org.apache.webdav.lib.Property; 22 import org.apache.webdav.lib.methods.MkcolMethod; 23 import org.apache.webdav.lib.methods.PropFindMethod; 24 import org.apache.webdav.lib.methods.PropPatchMethod; 25 26 public abstract class VersionCreatingComponent extends FunctionProviderComponent 27 { 28 29 protected VersionCreatingComponent() 30 { 31 super(); 32 } 33 34 protected void createVersion(Map transientVars, String user, String action, String comment) throws WorkflowException 35 { 36 createVersion(transientVars, user, action, comment, false); 37 } 38 39 protected void createVersion(Map transientVars, String user, String action, String comment, boolean shouldMakeCopyOfLiveLocation) throws WorkflowException 40 { 41 GetMethod get = null; 42 PutMethod put = null; 43 try 44 { 45 String location = URIUtil.getHTTPURI((String ) transientVars.get("location")); 46 HttpState httpState = (HttpState) transientVars.get("httpstate"); 47 48 String historyCollectionLocation = URIUtil.getHistoryURI(location); 49 String latestVersion = WebDAVHelper.propfindAsString(location, 50 Constants.CMS_1_0_NAMESPACE, "latestVersion", httpState); 51 int version = 1; 52 if (latestVersion != null && !latestVersion.equals("")) 53 { 54 try 56 { 57 version = Integer.parseInt(latestVersion) + 1; 58 } 59 catch (NumberFormatException e) 60 { 61 } 63 } 64 String historyLocation = historyCollectionLocation + "/" + version + ".xml"; 65 66 HttpClient httpClient = new HttpClient(); 67 httpClient.setState(httpState); 68 69 if (shouldMakeCopyOfLiveLocation) 70 { 71 get = new GetMethod(URIUtil.getLiveURI(location)); 72 } 73 else 74 { 75 get = new GetMethod(location); 76 } 77 get.setDoAuthentication(true); 78 int getResult = httpClient.executeMethod(get); 79 if (getResult / 100 == 2) 80 { 81 byte[] body = get.getResponseBody(); 82 get.releaseConnection(); 83 84 ensureAncestorCollectionsExist(httpClient, historyLocation); 85 86 put = new PutMethod(historyLocation); 87 put.setDoAuthentication(true); 88 put.setRequestBody(new ByteArrayInputStream (body)); 89 int putResult = httpClient.executeMethod(put); 90 put.releaseConnection(); 92 93 Set propertiesToSet = new HashSet (); 94 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "versionAction", action)); 95 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "versionComment", comment)); 96 DateFormat df = new SimpleDateFormat ("yyyyMMddHHmm"); 97 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "versionDate", df.format(new Date ()))); 98 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "versionUser", user)); 99 WebDAVHelper.proppatch(historyLocation, propertiesToSet, null, httpState); 100 101 propertiesToSet.clear(); 102 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "latestVersion", String.valueOf(version))); 103 WebDAVHelper.proppatch(location, propertiesToSet, null, httpState); 104 } 105 } 106 catch (IOException e) 107 { 108 throw new WorkflowException(e); 109 } 110 finally 111 { 112 if (get != null) 113 { 114 get.releaseConnection(); 115 } 116 if (put != null) 117 { 118 put.releaseConnection(); 119 } 120 } 121 } 122 123 private void copyProperties(HttpClient httpClient, String previewLocation, String liveLocation) 124 throws IOException 125 { 126 PropFindMethod propfind = null; 127 PropPatchMethod proppatch = null; 128 try 129 { 130 propfind = new PropFindMethod(previewLocation); 131 propfind.setDoAuthentication(true); 132 propfind.setDepth(0); 133 httpClient.executeMethod(propfind); 134 proppatch = new PropPatchMethod(liveLocation); 135 Enumeration responseUrls = propfind.getAllResponseURLs(); 136 if (responseUrls.hasMoreElements()) 137 { 138 Enumeration properties = propfind.getResponseProperties((String ) responseUrls 139 .nextElement()); 140 boolean executeProppatch = false; 141 while (properties.hasMoreElements()) 142 { 143 Property property = (Property) properties.nextElement(); 144 String namespace = property.getNamespaceURI(); 145 if (!namespace.equals("DAV:")) 146 { 147 String name = property.getLocalName(); 148 String value = property.getPropertyAsString(); 149 proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property", 150 namespace); 151 executeProppatch = true; 152 } 153 } 154 if (executeProppatch) 155 { 156 try 157 { 158 proppatch.setDoAuthentication(true); 159 int status = httpClient.executeMethod(proppatch); 160 } 162 catch (IOException e) 163 { 164 } 166 finally 167 { 168 proppatch.releaseConnection(); 169 } 170 } 171 propfind.releaseConnection(); 172 } 173 } 174 finally 175 { 176 if (propfind != null) 177 { 178 propfind.releaseConnection(); 179 } 180 if (proppatch != null) 181 { 182 proppatch.releaseConnection(); 183 } 184 } 185 } 186 187 private void ensureAncestorCollectionsExist(HttpClient httpClient, String liveLocation) 188 throws IOException 189 { 190 MkcolMethod mkcol = null; 191 try 192 { 193 int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/', 194 liveLocation.indexOf('/') + 1) + 1); 195 String protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash); 196 String path = liveLocation.substring(locationOfThirdSlash); 197 path = path.substring(0, path.lastIndexOf('/')); 198 if (path.length() > 0) 199 { 200 String parentLocation = protocolHostAndPort + path; 201 ensureAncestorCollectionsExist(httpClient, parentLocation); 202 mkcol = new MkcolMethod(parentLocation); 203 mkcol.setDoAuthentication(true); 204 int mkcolResult = httpClient.executeMethod(mkcol); 205 if (mkcolResult != 201) 206 { 207 } 209 mkcol.releaseConnection(); 210 211 if (parentLocation.indexOf(".www/") != -1) 212 { 213 copyProperties(httpClient, parentLocation.replaceAll("\\.www/", ".preview/"), 214 parentLocation); 215 } 216 } 217 } 218 finally 219 { 220 if (mkcol != null) 221 { 222 mkcol.releaseConnection(); 223 } 224 } 225 } 226 227 } | Popular Tags |