KickJava   Java API By Example, From Geeks To Geeks.

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


1 package nl.hippo.cms.workflows.reviewedactions;
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.util.Enumeration JavaDoc;
8 import java.util.Map JavaDoc;
9 import nl.hippo.cms.workflows.shared.FunctionProviderComponent;
10 import nl.hippo.cms.workflows.shared.URIUtil;
11 import org.apache.commons.httpclient.HttpClient;
12 import org.apache.commons.httpclient.HttpState;
13 import org.apache.commons.httpclient.methods.GetMethod;
14 import org.apache.commons.httpclient.methods.PutMethod;
15 import org.apache.webdav.lib.Property;
16 import org.apache.webdav.lib.methods.MkcolMethod;
17 import org.apache.webdav.lib.methods.PropFindMethod;
18 import org.apache.webdav.lib.methods.PropPatchMethod;
19
20 public class CopyObjectToLiveRepositoryFunction extends FunctionProviderComponent
21 {
22
23     public CopyObjectToLiveRepositoryFunction()
24     {
25         super();
26     }
27
28     public void executeImpl(final Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps)
29             throws WorkflowException
30     {
31         GetMethod get = null;
32         PutMethod put = null;
33         try
34         {
35             String JavaDoc previewLocation = URIUtil.getHTTPURI((String JavaDoc) transientVars.get("location"));
36             String JavaDoc liveLocation = URIUtil.getLiveURI(previewLocation);
37             HttpState httpState = (HttpState) transientVars.get("httpstate");
38             HttpClient httpClient = new HttpClient();
39             httpClient.setState(httpState);
40             get = new GetMethod(previewLocation);
41             get.setDoAuthentication(true);
42             int getResult = httpClient.executeMethod(get);
43             // TODO: handle result
44
byte[] body = get.getResponseBody();
45             get.releaseConnection();
46
47             ensureAncestorCollectionsExist(httpClient, liveLocation);
48
49             put = new PutMethod(liveLocation);
50             put.setDoAuthentication(true);
51             put.setRequestBody(new ByteArrayInputStream JavaDoc(body));
52             int putResult = httpClient.executeMethod(put);
53             // TODO: handle result
54
put.releaseConnection();
55
56             copyProperties(httpClient, previewLocation, liveLocation);
57         }
58         catch (IOException JavaDoc e)
59         {
60             throw new WorkflowException(e);
61         }
62         finally
63         {
64             if (get != null)
65             {
66                 get.releaseConnection();
67             }
68             if (put != null)
69             {
70                 put.releaseConnection();
71             }
72         }
73     }
74
75     private void copyProperties(HttpClient httpClient, String JavaDoc previewLocation, String JavaDoc liveLocation) throws IOException JavaDoc
76     {
77         PropFindMethod propfind = null;
78         PropPatchMethod proppatch = null;
79         try
80         {
81             propfind = new PropFindMethod(previewLocation);
82             propfind.setDoAuthentication(true);
83             propfind.setDepth(0);
84             httpClient.executeMethod(propfind);
85             proppatch = new PropPatchMethod(liveLocation);
86             Enumeration JavaDoc responseUrls = propfind.getAllResponseURLs();
87             if (responseUrls.hasMoreElements())
88             {
89                 Enumeration JavaDoc properties = propfind.getResponseProperties((String JavaDoc) responseUrls
90                         .nextElement());
91                 boolean executeProppatch = false;
92                 while (properties.hasMoreElements())
93                 {
94                     Property property = (Property) properties.nextElement();
95                     String JavaDoc namespace = property.getNamespaceURI();
96                     if (!namespace.equals("DAV:"))
97                     {
98                         String JavaDoc name = property.getLocalName();
99                         String JavaDoc value = property.getPropertyAsString();
100                         proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property", namespace);
101                         executeProppatch = true;
102                     }
103                 }
104                 if (executeProppatch)
105                 {
106                     try
107                     {
108                         proppatch.setDoAuthentication(true);
109                         int status = httpClient.executeMethod(proppatch);
110                         // TODO: handle result
111
}
112                     catch (IOException JavaDoc e)
113                     {
114                         // TODO: hanlde exception properly
115
}
116                     finally
117                     {
118                         proppatch.releaseConnection();
119                     }
120                 }
121                 propfind.releaseConnection();
122             }
123         }
124         finally
125         {
126             if (propfind != null)
127             {
128                 propfind.releaseConnection();
129             }
130             if (proppatch != null)
131             {
132                 proppatch.releaseConnection();
133             }
134         }
135     }
136
137     private void ensureAncestorCollectionsExist(HttpClient httpClient, String JavaDoc liveLocation)
138             throws IOException JavaDoc
139     {
140         MkcolMethod mkcol = null;
141         try
142         {
143             int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/', liveLocation
144                     .indexOf('/') + 1) + 1);
145             String JavaDoc protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash);
146             String JavaDoc path = liveLocation.substring(locationOfThirdSlash);
147             path = path.substring(0, path.lastIndexOf('/'));
148             if (path.length() > 0)
149             {
150                 String JavaDoc parentLocation = protocolHostAndPort + path;
151                 ensureAncestorCollectionsExist(httpClient, parentLocation);
152                 mkcol = new MkcolMethod(parentLocation);
153                 mkcol.setDoAuthentication(true);
154                 int mkcolResult = httpClient.executeMethod(mkcol);
155                 if (mkcolResult != 201)
156                 {
157                     // TODO: handle result
158
}
159                 mkcol.releaseConnection();
160                 
161                 if (parentLocation.indexOf(".www/") != -1)
162                 {
163                     copyProperties(httpClient, parentLocation.replaceAll("\\.www/", ".preview/"), parentLocation);
164                 }
165             }
166         }
167         finally
168         {
169             if (mkcol != null)
170             {
171                 mkcol.releaseConnection();
172             }
173         }
174     }
175
176 }
Popular Tags