KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitesdocument > ObjectCopyingComponent


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package nl.hippo.cms.workflows.multiplesitesdocument;
17
18 import com.opensymphony.module.propertyset.PropertySet;
19 import com.opensymphony.workflow.WorkflowException;
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Map JavaDoc;
23 import nl.hippo.cms.workflows.shared.FunctionProviderComponent;
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.webdav.lib.Property;
26 import org.apache.webdav.lib.methods.MkcolMethod;
27 import org.apache.webdav.lib.methods.PropFindMethod;
28 import org.apache.webdav.lib.methods.PropPatchMethod;
29
30 public abstract class ObjectCopyingComponent extends FunctionProviderComponent
31 {
32
33     public ObjectCopyingComponent()
34     {
35         super();
36     }
37
38     public void executeImpl(Map JavaDoc arg0, Map JavaDoc arg1, PropertySet arg2) throws WorkflowException
39     {
40     }
41
42     protected static void copyProperties(HttpClient httpClient, String JavaDoc previewLocation, String JavaDoc liveLocation) throws IOException JavaDoc
43     {
44         PropFindMethod propfind = null;
45         PropPatchMethod proppatch = null;
46         try
47         {
48             propfind = new PropFindMethod(previewLocation);
49             propfind.setDoAuthentication(true);
50             propfind.setDepth(0);
51             httpClient.executeMethod(propfind);
52             proppatch = new PropPatchMethod(liveLocation);
53             Enumeration JavaDoc responseUrls = propfind.getAllResponseURLs();
54             if (responseUrls.hasMoreElements())
55             {
56                 Enumeration JavaDoc properties = propfind.getResponseProperties((String JavaDoc) responseUrls
57                         .nextElement());
58                 boolean executeProppatch = false;
59                 while (properties.hasMoreElements())
60                 {
61                     Property property = (Property) properties.nextElement();
62                     String JavaDoc namespace = property.getNamespaceURI();
63                     if (!namespace.equals("DAV:"))
64                     {
65                         String JavaDoc name = property.getLocalName();
66                         String JavaDoc value = property.getPropertyAsString();
67                         proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property",
68                                 namespace);
69                         executeProppatch = true;
70                     }
71                 }
72                 if (executeProppatch)
73                 {
74                     try
75                     {
76                         proppatch.setDoAuthentication(true);
77                         int status = httpClient.executeMethod(proppatch);
78                         // TODO: handle result
79
}
80                     catch (IOException JavaDoc e)
81                     {
82                         // TODO: hanlde exception properly
83
}
84                     finally
85                     {
86                         proppatch.releaseConnection();
87                     }
88                 }
89                 propfind.releaseConnection();
90             }
91         }
92         finally
93         {
94             if (propfind != null)
95             {
96                 propfind.releaseConnection();
97             }
98             if (proppatch != null)
99             {
100                 proppatch.releaseConnection();
101             }
102         }
103     }
104
105     protected static void ensureAncestorCollectionsExist(HttpClient httpClient, String JavaDoc liveLocation) throws IOException JavaDoc
106     {
107         MkcolMethod mkcol = null;
108         try
109         {
110             int locationOfThirdSlash = liveLocation.indexOf('/', liveLocation.indexOf('/',
111                     liveLocation.indexOf('/') + 1) + 1);
112             String JavaDoc protocolHostAndPort = liveLocation.substring(0, locationOfThirdSlash);
113             String JavaDoc path = liveLocation.substring(locationOfThirdSlash);
114             path = path.substring(0, path.lastIndexOf('/'));
115             if (path.length() > 0)
116             {
117                 String JavaDoc parentLocation = protocolHostAndPort + path;
118                 ensureAncestorCollectionsExist(httpClient, parentLocation);
119                 mkcol = new MkcolMethod(parentLocation);
120                 mkcol.setDoAuthentication(true);
121                 int mkcolResult = httpClient.executeMethod(mkcol);
122                 if (mkcolResult != 201)
123                 {
124                     // TODO: handle result
125
}
126                 mkcol.releaseConnection();
127     
128                 if (parentLocation.indexOf(".www/") != -1)
129                 {
130                     copyProperties(httpClient, parentLocation.replaceAll("\\.www/", ".preview/"),
131                             parentLocation);
132                 }
133             }
134         }
135         finally
136         {
137             if (mkcol != null)
138             {
139                 mkcol.releaseConnection();
140             }
141         }
142     }
143
144 }
145
Popular Tags