KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > MethodUtil


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/MethodUtil.java,v 1.5 2004/07/28 09:32:17 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:32:17 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method;
25
26 import java.util.List JavaDoc;
27
28 import org.apache.slide.common.ServiceAccessException;
29 import org.apache.slide.common.SlideException;
30 import org.apache.slide.content.RevisionNotFoundException;
31 import org.apache.slide.content.NodeProperty.NamespaceCache;
32 import org.apache.slide.lock.ObjectLockedException;
33 import org.apache.slide.macro.ConflictException;
34 import org.apache.slide.macro.ForbiddenException;
35 import org.apache.slide.security.AccessDeniedException;
36 import org.apache.slide.structure.LinkedObjectNotFoundException;
37 import org.apache.slide.structure.ObjectAlreadyExistsException;
38 import org.apache.slide.structure.ObjectNotFoundException;
39 import org.apache.slide.webdav.util.PreconditionViolationException;
40 import org.apache.slide.webdav.util.ViolatedPrecondition;
41 import org.apache.slide.webdav.util.WebdavConstants;
42 import org.jdom.Element;
43 import org.jdom.JDOMException;
44
45 /**
46  * Static util methods.
47  *
48  */

49 public abstract class MethodUtil {
50     /**
51      * Get return status based on exception type.
52      */

53     public static String JavaDoc getErrorMessage(Throwable JavaDoc ex) {
54         if ( !(ex instanceof SlideException) ) {
55             return "";
56         } else {
57             return getErrorMessage((SlideException)ex);
58         }
59     }
60
61     /**
62      * Get the return status message info on exception type.
63      */

64     public static String JavaDoc getErrorMessage(SlideException ex) {
65         try {
66             throw ex;
67         } catch(ObjectNotFoundException e) {
68             return e.getObjectUri();
69         } catch(ConflictException e) {
70             return e.getObjectUri();
71         } catch(ForbiddenException e) {
72             return e.getObjectUri();
73         } catch(AccessDeniedException e) {
74             return e.getObjectUri();
75         } catch(ObjectAlreadyExistsException e) {
76             return e.getObjectUri();
77         } catch(ServiceAccessException e) {
78             return getErrorMessage((ServiceAccessException)e);
79         } catch(LinkedObjectNotFoundException e) {
80             return e.getTargetUri();
81         } catch(RevisionNotFoundException e) {
82             return e.getObjectUri();
83         } catch(ObjectLockedException e) {
84             return e.getObjectUri();
85         } catch(PreconditionViolationException e) {
86             return e.getObjectUri();
87         } catch(SlideException e) {
88             return e.getMessage();
89         }
90     }
91
92
93
94
95     /**
96      * Get return status based on exception type.
97      */

98     public static String JavaDoc getErrorMessage(ServiceAccessException ex) {
99         Throwable JavaDoc cause = ex.getCauseException();
100         if (cause == null || !(cause instanceof SlideException) ) {
101             return ""; // this is the default}
102
} else {
103             return getErrorMessage((SlideException)cause);
104         }
105     }
106
107     //--
108

109     /**
110      * Generate <error> for the given precondition violation.
111      *
112      * @param violatedPrecondition the ViolatedPrecondition that describes
113      * the violated precondition.
114      *
115      * @return the <error> for the given precondition violation.
116      */

117     public static Element getPreconditionViolationError(ViolatedPrecondition violatedPrecondition) {
118         return getPreconditionViolationError(violatedPrecondition,
119                                              NamespaceCache.DEFAULT_NAMESPACE);
120     }
121
122     /**
123      * Generate <error> for the given precondition violation.
124      *
125      * @param violatedPrecondition the ViolatedPrecondition that describes
126      * the violated precondition.
127      *
128      * @return the <error> for the given precondition violation.
129      */

130     public static Element getPreconditionViolationError(ViolatedPrecondition violatedPrecondition, org.jdom.Namespace namespace) {
131
132         Element error = new Element(WebdavConstants.E_ERROR,
133                                     namespace);
134         Element violatedPreconditionElement = new Element(violatedPrecondition.getPrecondition(),
135                                                           namespace);
136         error.addContent(violatedPreconditionElement);
137         return error;
138     }
139
140
141     /**
142      * Generate <responsedescription> for the given precondition violation.
143      *
144      * @param pve the ProconditionViolationException that describes the violated
145      * precondition.
146      *
147      * @return the <responsedescription> for the given precondition violation.
148      */

149     public static Element getPreconditionViolationResponseDescription(PreconditionViolationException pve) {
150         Element responseDescription = new Element(WebdavConstants.E_RESPONSEDESCRIPTION, NamespaceCache.DEFAULT_NAMESPACE);
151         responseDescription.addContent(MethodUtil.getPreconditionViolationError(pve.getViolatedPrecondition()));
152         return responseDescription;
153     }
154
155     //--
156

157
158     /**
159      * Get return status based on exception type.
160      */

161     public static String JavaDoc getURI(Throwable JavaDoc ex) {
162         if ( !(ex instanceof SlideException) ) {
163             return "";
164         } else {
165             return getURI((SlideException)ex);
166         }
167     }
168
169
170     /**
171      * Get the URI contained in the exception, if available.
172      */

173     public static String JavaDoc getURI(SlideException ex) {
174         try {
175             throw ex;
176         } catch(ObjectNotFoundException e) {
177             return e.getObjectUri();
178         } catch(ConflictException e) {
179             return e.getObjectUri();
180         } catch(ForbiddenException e) {
181             return e.getObjectUri();
182         } catch(AccessDeniedException e) {
183             return e.getObjectUri();
184         } catch(ObjectAlreadyExistsException e) {
185             return e.getObjectUri();
186         } catch(ServiceAccessException e) {
187             return getURI((ServiceAccessException)ex);
188         } catch(LinkedObjectNotFoundException e) {
189             return e.getTargetUri();
190         } catch(RevisionNotFoundException e) {
191             return e.getObjectUri();
192         } catch(ObjectLockedException e) {
193             return e.getObjectUri();
194         } catch(SlideException e) {
195             return "";
196         }
197     }
198
199     /**
200      * Get return status based on exception type.
201      */

202     private static String JavaDoc getURI(ServiceAccessException ex) {
203         Throwable JavaDoc cause = ex.getCauseException();
204         if (cause == null || !(cause instanceof SlideException) ) {
205             return ""; // this is the default}
206
} else {
207             return getURI((SlideException)cause);
208         }
209     }
210
211     //--
212

213     // TODO: find other places that should use this method
214
public static boolean isValidSegment(String JavaDoc segment) {
215         // TODO: more checks?
216
return segment.indexOf('/') == -1;
217     }
218
219
220     // TODO: find other places that should use this method
221
public static String JavaDoc getChildUrl(List JavaDoc children, String JavaDoc elementName) throws JDOMException {
222         // TODO: decode URL?
223
return getChildText(children, elementName);
224     }
225
226     /**
227      * Precondition: ofs < children.size(), children.get(ofs) instanceof Element
228      *
229      * @return never null
230      */

231     public static String JavaDoc getChildText(List JavaDoc children, String JavaDoc elementName) throws JDOMException {
232         // We cannot access elements with their index because there might additional
233
// content elements not mentioned in the DTD
234

235         Element child;
236         int i;
237         int max;
238         Element found;
239
240         found = null;
241         max = children.size();
242         for (i = 0; i < max; i++) {
243             child = (Element) children.get(i);
244             if (child.getName().equals(elementName)) {
245                 if (found != null) {
246                     throw new JDOMException("multiple child elements '" + elementName + "' found.");
247                 }
248                 found = child;
249             }
250         }
251         if (found == null) {
252             throw new JDOMException("child element '" + elementName + "' not found.");
253         }
254         return found.getText();
255     }
256 }
257
258
259
Popular Tags