KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > reviewedactions > VersionCreatingComponent


1 package nl.hippo.cms.workflows.reviewedactions;
2
3 import com.opensymphony.workflow.WorkflowException;
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.text.DateFormat JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
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 JavaDoc transientVars, String JavaDoc user, String JavaDoc action, String JavaDoc comment) throws WorkflowException
35     {
36         createVersion(transientVars, user, action, comment, false);
37     }
38     
39     protected void createVersion(Map JavaDoc transientVars, String JavaDoc user, String JavaDoc action, String JavaDoc comment, boolean shouldMakeCopyOfLiveLocation) throws WorkflowException
40     {
41         GetMethod get = null;
42         PutMethod put = null;
43         try
44         {
45             String JavaDoc location = URIUtil.getHTTPURI((String JavaDoc) transientVars.get("location"));
46             HttpState httpState = (HttpState) transientVars.get("httpstate");
47
48             String JavaDoc historyCollectionLocation = URIUtil.getHistoryURI(location);
49             String JavaDoc latestVersion = WebDAVHelper.propfindAsString(location,
50                     Constants.CMS_1_0_NAMESPACE, "latestVersion", httpState);
51             int version = 1;
52             if (latestVersion != null && !latestVersion.equals(""))
53             {
54                 // STATE: document has previous versions
55
try
56                 {
57                     version = Integer.parseInt(latestVersion) + 1;
58                 }
59                 catch (NumberFormatException JavaDoc e)
60                 {
61                     // Ignore: use default value of 1
62
}
63             }
64             String JavaDoc 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 JavaDoc(body));
89                 int putResult = httpClient.executeMethod(put);
90                 // TODO: handle result
91
put.releaseConnection();
92                 
93                 Set JavaDoc propertiesToSet = new HashSet JavaDoc();
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 JavaDoc df = new SimpleDateFormat JavaDoc("yyyyMMddHHmm");
97                 propertiesToSet.add(new nl.hippo.cocoon.webdav.Property("H", Constants.CMS_1_0_NAMESPACE, "versionDate", df.format(new Date JavaDoc())));
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 JavaDoc 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 JavaDoc previewLocation, String JavaDoc liveLocation)
124             throws IOException JavaDoc
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 JavaDoc responseUrls = propfind.getAllResponseURLs();
136             if (responseUrls.hasMoreElements())
137             {
138                 Enumeration JavaDoc properties = propfind.getResponseProperties((String JavaDoc) responseUrls
139                         .nextElement());
140                 boolean executeProppatch = false;
141                 while (properties.hasMoreElements())
142                 {
143                     Property property = (Property) properties.nextElement();
144                     String JavaDoc namespace = property.getNamespaceURI();
145                     if (!namespace.equals("DAV:"))
146                     {
147                         String JavaDoc name = property.getLocalName();
148                         String JavaDoc 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                         // TODO: handle result
161
}
162                     catch (IOException JavaDoc e)
163                     {
164                         // TODO: hanlde exception properly
165
}
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 JavaDoc liveLocation)
188             throws IOException JavaDoc
189     {
190         MkcolMethod mkcol = null;
191         try
192         {
193             int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/',
194                     liveLocation.indexOf('/') + 1) + 1);
195             String JavaDoc protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash);
196             String JavaDoc path = liveLocation.substring(locationOfThirdSlash);
197             path = path.substring(0, path.lastIndexOf('/'));
198             if (path.length() > 0)
199             {
200                 String JavaDoc 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                     // TODO: handle result
208
}
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