KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitessite > VersioningHelperComponent


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 JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.text.DateFormat JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
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 JavaDoc transientVars, PropertySet ps, String JavaDoc user, String JavaDoc action,
38             String JavaDoc 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 JavaDoc relativeLocation = ps.getString("location");
53                     String JavaDoc location = editorRepo.getRepositoryInformation().getAbsoluteUri(
54                             relativeLocation);
55                     HttpState httpState = (HttpState) transientVars.get("httpstate");
56
57                     String JavaDoc historyCollectionLocation = historyRepo.getRepositoryInformation().getAbsoluteUri(relativeLocation);
58                     int version = getCurrentVersion(location, httpState) + 1;
59                     String JavaDoc 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                     // TODO: handle result
68
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 JavaDoc(body));
76                     int putResult = httpClient.executeMethod(put);
77                     // TODO: handle result
78
put.releaseConnection();
79
80                     Set propertiesToSet = new HashSet JavaDoc();
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 JavaDoc df = new SimpleDateFormat JavaDoc("yyyyMMdd");
86                     propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H",
87                             Constants.CMS_1_0_NAMESPACE, "versionDate", df.format(new Date JavaDoc())));
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 JavaDoc 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     /**
129      * @param location
130      * @param httpState
131      * @return The current version or <code>0</code> if the document does not have versions.
132      * @throws IOException
133      */

134     protected int getCurrentVersion(String JavaDoc location, HttpState httpState) throws IOException JavaDoc
135     {
136         int result = 0;
137         
138         String JavaDoc latestVersion = WebDAVHelper.propfindAsString(location,
139                 Constants.CMS_1_0_NAMESPACE, "latestVersion", httpState);
140         if (latestVersion != null && !latestVersion.equals(""))
141         {
142             // STATE: document has previous versions
143
try
144             {
145                 result = Integer.parseInt(latestVersion);
146             }
147             catch (NumberFormatException JavaDoc e)
148             {
149                 // Ignore: use default value of 0
150
}
151         }
152         
153         return result;
154     }
155
156     private void copyProperties(HttpClient httpClient, String JavaDoc previewLocation, String JavaDoc liveLocation)
157             throws IOException JavaDoc
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 JavaDoc responseUrls = propfind.getAllResponseURLs();
169             if (responseUrls.hasMoreElements())
170             {
171                 Enumeration JavaDoc properties = propfind.getResponseProperties((String JavaDoc) responseUrls
172                         .nextElement());
173                 boolean executeProppatch = false;
174                 while (properties.hasMoreElements())
175                 {
176                     Property property = (Property) properties.nextElement();
177                     String JavaDoc namespace = property.getNamespaceURI();
178                     if (!namespace.equals("DAV:"))
179                     {
180                         String JavaDoc name = property.getLocalName();
181                         String JavaDoc 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                         // TODO: handle result
194
}
195                     catch (IOException JavaDoc e)
196                     {
197                         // TODO: hanlde exception properly
198
}
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 JavaDoc liveLocation)
221             throws IOException JavaDoc
222     {
223         MkcolMethod mkcol = null;
224         try
225         {
226             int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/',
227                     liveLocation.indexOf('/') + 1) + 1);
228             String JavaDoc protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash);
229             String JavaDoc path = liveLocation.substring(locationOfThirdSlash);
230             path = path.substring(0, path.lastIndexOf('/'));
231             if (path.length() > 0)
232             {
233                 String JavaDoc 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                     // TODO: handle result
241
}
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