KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitessite > 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.multiplesitessite;
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.repositorylocation.RepositoryInformation;
24 import nl.hippo.cms.workflows.shared.FunctionProviderComponent;
25 import org.apache.commons.httpclient.HttpClient;
26 import org.apache.webdav.lib.Property;
27 import org.apache.webdav.lib.methods.MkcolMethod;
28 import org.apache.webdav.lib.methods.PropFindMethod;
29 import org.apache.webdav.lib.methods.PropPatchMethod;
30
31 public abstract class ObjectCopyingComponent extends FunctionProviderComponent
32 {
33
34     public ObjectCopyingComponent()
35     {
36         super();
37     }
38
39     public void executeImpl(Map JavaDoc arg0, Map JavaDoc arg1, PropertySet arg2) throws WorkflowException
40     {
41     }
42
43     protected static void copyProperties(HttpClient sourceHttpClient, String JavaDoc sourceLocation, HttpClient destinationHttpClient, String JavaDoc destinationLocation) throws IOException JavaDoc
44     {
45         PropFindMethod propfind = null;
46         PropPatchMethod proppatch = null;
47         try
48         {
49             propfind = new PropFindMethod(sourceLocation);
50             propfind.setDoAuthentication(true);
51             propfind.setDepth(0);
52             sourceHttpClient.executeMethod(propfind);
53             proppatch = new PropPatchMethod(destinationLocation);
54             Enumeration JavaDoc responseUrls = propfind.getAllResponseURLs();
55             if (responseUrls.hasMoreElements())
56             {
57                 Enumeration JavaDoc properties = propfind.getResponseProperties((String JavaDoc) responseUrls
58                         .nextElement());
59                 boolean executeProppatch = false;
60                 while (properties.hasMoreElements())
61                 {
62                     Property property = (Property) properties.nextElement();
63                     String JavaDoc namespace = property.getNamespaceURI();
64                     if (!namespace.equals("DAV:"))
65                     {
66                         String JavaDoc name = property.getLocalName();
67                         String JavaDoc value = property.getPropertyAsString();
68                         proppatch.addPropertyToSet(name, "<![CDATA[" + value + "]]>", "property",
69                                 namespace);
70                         executeProppatch = true;
71                     }
72                 }
73                 if (executeProppatch)
74                 {
75                     try
76                     {
77                         proppatch.setDoAuthentication(true);
78                         int status = destinationHttpClient.executeMethod(proppatch);
79                         // TODO: handle result
80
}
81                     catch (IOException JavaDoc e)
82                     {
83                         // TODO: hanlde exception properly
84
}
85                     finally
86                     {
87                         proppatch.releaseConnection();
88                     }
89                 }
90                 propfind.releaseConnection();
91             }
92         }
93         finally
94         {
95             if (propfind != null)
96             {
97                 propfind.releaseConnection();
98             }
99             if (proppatch != null)
100             {
101                 proppatch.releaseConnection();
102             }
103         }
104     }
105
106     protected static void ensureAncestorCollectionsExist(HttpClient sourceHttpClient, RepositoryInformation sourceRepoInformation, HttpClient destinationHttpClient, RepositoryInformation destinationRepoInformation, String JavaDoc location) throws IOException JavaDoc
107     {
108         MkcolMethod mkcol = null;
109         try
110         {
111             int indexOfLastSlash = location.lastIndexOf('/');
112             if (indexOfLastSlash != -1)
113             {
114                 String JavaDoc parentLocation = location.substring(0, indexOfLastSlash);
115                 if (parentLocation.length() > 0)
116                 {
117                     ensureAncestorCollectionsExist(sourceHttpClient, sourceRepoInformation, destinationHttpClient, destinationRepoInformation, parentLocation);
118                     mkcol = new MkcolMethod(destinationRepoInformation.getAbsoluteUri(parentLocation));
119                     mkcol.setDoAuthentication(true);
120                     int mkcolResult = destinationHttpClient.executeMethod(mkcol);
121                     if (mkcolResult == 201)
122                     {
123                         copyProperties(sourceHttpClient, sourceRepoInformation.getAbsoluteUri(parentLocation), destinationHttpClient, destinationRepoInformation.getAbsoluteUri(parentLocation));
124                     }
125                     else
126                     {
127                         // TODO: handle result
128
}
129                     mkcol.releaseConnection();
130                 }
131                 else
132                 {
133                     ensureAncestorCollectionsExist(destinationHttpClient, destinationRepoInformation.getAbsoluteUri(location));
134                 }
135             }
136         }
137         finally
138         {
139             if (mkcol != null)
140             {
141                 mkcol.releaseConnection();
142             }
143         }
144     }
145
146     protected static void ensureAncestorCollectionsExist(HttpClient httpClient, String JavaDoc location) throws IOException JavaDoc
147     {
148         MkcolMethod mkcol = null;
149         try
150         {
151             int locationOfThirdSlash = location.indexOf('/', location.indexOf('/',
152                     location.indexOf('/') + 1) + 1);
153             String JavaDoc protocolHostAndPort = location.substring(0, locationOfThirdSlash);
154             String JavaDoc path = location.substring(locationOfThirdSlash);
155             path = path.substring(0, path.lastIndexOf('/'));
156             if (path.length() > 0)
157             {
158                 String JavaDoc parentLocation = protocolHostAndPort + path;
159                 ensureAncestorCollectionsExist(httpClient, parentLocation);
160                 mkcol = new MkcolMethod(parentLocation);
161                 mkcol.setDoAuthentication(true);
162                 int mkcolResult = httpClient.executeMethod(mkcol);
163                 if (mkcolResult != 201)
164                 {
165                     // TODO: handle result
166
}
167                 mkcol.releaseConnection();
168             }
169         }
170         finally
171         {
172             if (mkcol != null)
173             {
174                 mkcol.releaseConnection();
175             }
176         }
177     }
178 }
179
Popular Tags