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