KickJava   Java API By Example, From Geeks To Geeks.

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


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.reviewedactions;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Vector JavaDoc;
20 import org.apache.commons.httpclient.HttpClient;
21 import org.apache.commons.httpclient.HttpException;
22 import org.apache.commons.httpclient.methods.DeleteMethod;
23 import org.apache.webdav.lib.PropertyName;
24 import org.apache.webdav.lib.methods.PropFindMethod;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 public class RepositoryHelper
29 {
30
31     private static final Vector JavaDoc COLLECTION_LIST_PROPERTY_NAMES = new Vector JavaDoc();
32     
33     static
34     {
35         COLLECTION_LIST_PROPERTY_NAMES.add(new PropertyName("DAV:", "displayname"));
36     }
37
38     private RepositoryHelper()
39     {
40         super();
41     }
42
43     public static void deletePathIfDangling(String JavaDoc location, HttpClient httpClient) throws IOException JavaDoc, HttpException
44     {
45         DeleteMethod delete;
46         //FIXME: delete ancestors while empty
47
int indexOfLastSlash = location.lastIndexOf('/');
48         while (location != null && indexOfLastSlash != -1)
49         {
50             location = location.substring(0, indexOfLastSlash);
51     
52             PropFindMethod propfind = null;
53             boolean shouldDeleteFolder = false;
54             try
55             {
56                 propfind = new PropFindMethod(location, 1, COLLECTION_LIST_PROPERTY_NAMES.elements());
57                 propfind.setDoAuthentication(true);
58                 int propfindResult = httpClient.executeMethod(propfind);
59                 // TODO: handle result
60
Document JavaDoc responseDocument = propfind.getResponseDocument();
61                 if (responseDocument != null)
62                 {
63                     NodeList JavaDoc displayNames = responseDocument.getElementsByTagNameNS("DAV:", "displayname");
64                     shouldDeleteFolder = displayNames.getLength() == 1;
65                 }
66             }
67             finally
68             {
69                 if (propfind != null)
70                 {
71                     propfind.releaseConnection();
72                 }
73             }
74             
75             if (shouldDeleteFolder)
76             {
77                 delete = null;
78                 try
79                 {
80                     delete = new DeleteMethod(location);
81                     delete.setDoAuthentication(true);
82                     int deleteResult = httpClient.executeMethod(delete);
83                     // TODO: handle result
84
}
85                 finally
86                 {
87                     if (delete != null)
88                     {
89                         delete.releaseConnection();
90                     }
91                 }
92     
93                 indexOfLastSlash = location.lastIndexOf('/');
94             }
95             else
96             {
97                 location = null;
98             }
99         }
100     }
101
102 }
103
Popular Tags