1 package nl.hippo.cms.workflows.multiplesitessite; 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 VersioningHelperComponent extends FunctionProviderComponent 30 { 31 32 protected VersioningHelperComponent() 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 int version = getCurrentVersion(location, httpState) + 1; 59 String historyLocation = historyCollectionLocation + "/" + version + ".xml"; 60 61 HttpClient httpClient = new HttpClient(); 62 httpClient.setState(httpState); 63 64 get = new GetMethod(location); 65 get.setDoAuthentication(true); 66 int getResult = httpClient.executeMethod(get); 67 byte[] body = get.getResponseBody(); 69 get.releaseConnection(); 70 71 ensureAncestorCollectionsExist(httpClient, historyLocation); 72 73 put = new PutMethod(historyLocation); 74 put.setDoAuthentication(true); 75 put.setRequestBody(new ByteArrayInputStream (body)); 76 int putResult = httpClient.executeMethod(put); 77 put.releaseConnection(); 79 80 Set propertiesToSet = new HashSet (); 81 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", 82 Constants.CMS_1_0_NAMESPACE, "versionAction", action)); 83 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", 84 Constants.CMS_1_0_NAMESPACE, "versionComment", comment)); 85 DateFormat df = new SimpleDateFormat ("yyyyMMdd"); 86 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", 87 Constants.CMS_1_0_NAMESPACE, "versionDate", df.format(new Date ()))); 88 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", 89 Constants.CMS_1_0_NAMESPACE, "versionUser", user)); 90 WebDAVHelper.proppatch(historyLocation, propertiesToSet, null, httpState); 91 92 propertiesToSet.clear(); 93 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", 94 Constants.CMS_1_0_NAMESPACE, "latestVersion", String.valueOf(version))); 95 WebDAVHelper.proppatch(location, propertiesToSet, null, httpState); 96 } 97 finally 98 { 99 m_manager.release(historyRepo); 100 } 101 } 102 finally 103 { 104 m_manager.release(editorRepo); 105 } 106 } 107 catch (IOException e) 108 { 109 throw new WorkflowException(e); 110 } 111 catch (ServiceException e) 112 { 113 throw new WorkflowException(e); 114 } 115 finally 116 { 117 if (get != null) 118 { 119 get.releaseConnection(); 120 } 121 if (put != null) 122 { 123 put.releaseConnection(); 124 } 125 } 126 } 127 128 134 protected int getCurrentVersion(String location, HttpState httpState) throws IOException 135 { 136 int result = 0; 137 138 String latestVersion = WebDAVHelper.propfindAsString(location, 139 Constants.CMS_1_0_NAMESPACE, "latestVersion", httpState); 140 if (latestVersion != null && !latestVersion.equals("")) 141 { 142 try 144 { 145 result = Integer.parseInt(latestVersion); 146 } 147 catch (NumberFormatException e) 148 { 149 } 151 } 152 153 return result; 154 } 155 156 private void copyProperties(HttpClient httpClient, String previewLocation, String liveLocation) 157 throws IOException 158 { 159 PropFindMethod propfind = null; 160 PropPatchMethod proppatch = null; 161 try 162 { 163 propfind = new PropFindMethod(previewLocation); 164 propfind.setDoAuthentication(true); 165 propfind.setDepth(0); 166 httpClient.executeMethod(propfind); 167 proppatch = new PropPatchMethod(liveLocation); 168 Enumeration responseUrls = propfind.getAllResponseURLs(); 169 if (responseUrls.hasMoreElements()) 170 { 171 Enumeration properties = propfind.getResponseProperties((String ) responseUrls 172 .nextElement()); 173 boolean executeProppatch = false; 174 while (properties.hasMoreElements()) 175 { 176 Property property = (Property) properties.nextElement(); 177 String namespace = property.getNamespaceURI(); 178 if (!namespace.equals("DAV:")) 179 { 180 String name = property.getLocalName(); 181 String value = property.getPropertyAsString(); 182 proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property", 183 namespace); 184 executeProppatch = true; 185 } 186 } 187 if (executeProppatch) 188 { 189 try 190 { 191 proppatch.setDoAuthentication(true); 192 int status = httpClient.executeMethod(proppatch); 193 } 195 catch (IOException e) 196 { 197 } 199 finally 200 { 201 proppatch.releaseConnection(); 202 } 203 } 204 propfind.releaseConnection(); 205 } 206 } 207 finally 208 { 209 if (propfind != null) 210 { 211 propfind.releaseConnection(); 212 } 213 if (proppatch != null) 214 { 215 proppatch.releaseConnection(); 216 } 217 } 218 } 219 220 private void ensureAncestorCollectionsExist(HttpClient httpClient, String liveLocation) 221 throws IOException 222 { 223 MkcolMethod mkcol = null; 224 try 225 { 226 int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/', 227 liveLocation.indexOf('/') + 1) + 1); 228 String protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash); 229 String path = liveLocation.substring(locationOfThirdSlash); 230 path = path.substring(0, path.lastIndexOf('/')); 231 if (path.length() > 0) 232 { 233 String parentLocation = protocolHostAndPort + path; 234 ensureAncestorCollectionsExist(httpClient, parentLocation); 235 mkcol = new MkcolMethod(parentLocation); 236 mkcol.setDoAuthentication(true); 237 int mkcolResult = httpClient.executeMethod(mkcol); 238 if (mkcolResult != 201) 239 { 240 } 242 mkcol.releaseConnection(); 243 244 if (parentLocation.indexOf(".www/") != -1) 245 { 246 copyProperties(httpClient, parentLocation.replaceAll("\\.www/", ".preview/"), 247 parentLocation); 248 } 249 } 250 } 251 finally 252 { 253 if (mkcol != null) 254 { 255 mkcol.releaseConnection(); 256 } 257 } 258 } 259 260 } | Popular Tags |