1 23 24 package org.apache.slide.webdav.method; 25 26 import java.util.List ; 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 49 public abstract class MethodUtil { 50 53 public static String getErrorMessage(Throwable ex) { 54 if ( !(ex instanceof SlideException) ) { 55 return ""; 56 } else { 57 return getErrorMessage((SlideException)ex); 58 } 59 } 60 61 64 public static String 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 98 public static String getErrorMessage(ServiceAccessException ex) { 99 Throwable cause = ex.getCauseException(); 100 if (cause == null || !(cause instanceof SlideException) ) { 101 return ""; } else { 103 return getErrorMessage((SlideException)cause); 104 } 105 } 106 107 109 117 public static Element getPreconditionViolationError(ViolatedPrecondition violatedPrecondition) { 118 return getPreconditionViolationError(violatedPrecondition, 119 NamespaceCache.DEFAULT_NAMESPACE); 120 } 121 122 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 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 157 158 161 public static String getURI(Throwable ex) { 162 if ( !(ex instanceof SlideException) ) { 163 return ""; 164 } else { 165 return getURI((SlideException)ex); 166 } 167 } 168 169 170 173 public static String 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 202 private static String getURI(ServiceAccessException ex) { 203 Throwable cause = ex.getCauseException(); 204 if (cause == null || !(cause instanceof SlideException) ) { 205 return ""; } else { 207 return getURI((SlideException)cause); 208 } 209 } 210 211 213 public static boolean isValidSegment(String segment) { 215 return segment.indexOf('/') == -1; 217 } 218 219 220 public static String getChildUrl(List children, String elementName) throws JDOMException { 222 return getChildText(children, elementName); 224 } 225 226 231 public static String getChildText(List children, String elementName) throws JDOMException { 232 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 |