1 23 24 package org.apache.slide.webdav.util; 25 26 import org.apache.slide.authenticate.CredentialsToken; 27 import org.apache.slide.common.*; 28 import org.apache.slide.content.Content; 29 import org.apache.slide.content.InsufficientStorageException; 30 import org.apache.slide.content.NodeRevisionDescriptor; 31 import org.apache.slide.content.NodeRevisionDescriptors; 32 import org.apache.slide.event.VetoException; 33 import org.apache.slide.lock.ObjectLockedException; 34 import org.apache.slide.macro.ConflictException; 35 import org.apache.slide.macro.ForbiddenException; 36 import org.apache.slide.security.AccessDeniedException; 37 import org.apache.slide.security.UnauthenticatedException; 38 import org.apache.slide.structure.ObjectAlreadyExistsException; 39 import org.apache.slide.structure.ObjectNotFoundException; 40 import org.apache.slide.util.Configuration; 41 import org.apache.slide.util.logger.Logger; 42 import org.apache.slide.webdav.WebdavException; 43 import org.apache.slide.webdav.WebdavServletConfig; 44 import org.apache.slide.webdav.method.MethodNotAllowedException; 45 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpSession ; 48 import java.io.UnsupportedEncodingException ; 49 import java.net.SocketException ; 50 import java.security.Principal ; 51 52 58 public class WebdavUtils { 59 60 61 private static final String CREDENTIALS_ATTRIBUTE = 62 "org.apache.slide.webdav.method.credentials"; 63 64 private static final String LOG_CHANNEL = 65 WebdavUtils.class.getName(); 66 67 77 public static String decodeString(String string, String enc) 78 throws UnsupportedEncodingException { 79 if(string == null) 80 return null; 81 StringBuffer sb = null; 82 int j = 0; 83 int sf = 0; 84 sb = new StringBuffer (); 85 byte utf8buffer[] = new byte[5]; 86 byte bytes[] = string.getBytes("ISO-8859-1"); 88 for (int i = 0; i < bytes.length; i+=1) { 89 byte b = bytes[i]; 90 int bb = (b >= 0) ? b : b+256; 91 utf8buffer[j++] = b; 92 boolean ok = false; 93 if (bb >= 128) { 95 if (sf==0) { 97 if (bb >= 192 && i < 252) { 99 ok = true; 100 if (bb >= 224) 102 if (bb >= 240) 103 if (bb >= 248) 104 sf = 4; 105 else 106 sf = 3; 107 else 108 sf = 2; 109 else 110 sf = 1; 111 } 112 } else if (bb >= 128 && bb < 192) { 113 sf--; 115 if (sf == 0) { 116 sb.append(new String (utf8buffer,0,j,"UTF-8")); 117 j = 0; 118 } 119 ok = true; 120 } 121 } 122 if (!ok) { 124 sb.append(new String (utf8buffer,0,j, enc)); 125 j = 0; 126 sf = 0; 127 } 128 } 129 if (j > 0) { 131 sb.append(new String (utf8buffer,0,j, enc)); 132 } 133 return sb.toString(); 134 } 135 136 137 146 public static String normalizeURL(String path) { 147 148 if (path == null) 149 return null; 150 151 String normalized = path; 152 153 if (normalized == null) 154 return (null); 155 156 if (normalized.indexOf('\\') >= 0) 158 normalized = normalized.replace('\\', '/'); 159 if (!normalized.startsWith("/")) 160 normalized = "/" + normalized; 161 162 while (true) { 164 int index = normalized.indexOf("//"); 165 if (index < 0) 166 break; 167 normalized = normalized.substring(0, index) + 168 normalized.substring(index + 1); 169 } 170 171 while (true) { 173 int index = normalized.indexOf("/./"); 174 if (index < 0) 175 break; 176 normalized = normalized.substring(0, index) + 177 normalized.substring(index + 2); 178 } 179 180 while (true) { 182 int index = normalized.indexOf("/../"); 183 if (index < 0) 184 break; 185 if (index == 0) 186 return (null); int index2 = normalized.lastIndexOf('/', index - 1); 188 normalized = normalized.substring(0, index2) + 189 normalized.substring(index + 3); 190 } 191 normalized = normalized.replace('?','_'); 192 return (normalized); 194 } 195 196 197 202 public static String encodeURL(String path) { 203 return URLUtil.URLEncode(path, Configuration.realUrlEncoding()); 204 } 205 206 207 213 public static String encodeURL(String path, String enc) { 214 return URLUtil.URLEncode(path, enc); 215 } 216 217 227 public static String getAbsolutePath 228 (String uri, HttpServletRequest req, WebdavServletConfig config) 229 { 230 return getAbsolutePath (uri, req.getContextPath(), req.getServletPath(), config); 231 } 232 233 246 public static String getAbsolutePath (String uri, String contextPath, 247 String servletPath, 248 WebdavServletConfig config) 249 { 250 String result = uri; 251 252 String scope = config.getScope(); 253 int scopeLength = scope.length(); 254 if (scopeLength > 0 && uri.startsWith(scope)) { 255 result = uri.substring(scopeLength); 256 } 257 258 if (!config.isDefaultServlet()) { 259 contextPath += servletPath; 260 } 261 result = contextPath + result; 262 263 return encodeURL(result); 264 } 265 266 public static String getAbsolutePath (String uri, String slideContextPath, 267 WebdavServletConfig config) 268 { 269 String result = uri; 270 271 String scope = config.getScope(); 272 int scopeLength = scope.length(); 273 if (scopeLength > 0 && uri.startsWith(scope)) { 274 result = uri.substring(scopeLength); 275 } 276 277 result = slideContextPath + result; 278 279 return encodeURL(result); 280 } 281 282 public static String getAbsolutePath (String uri, WebdavContext context) 283 { 284 String result = uri; 285 286 String scope = context.getServletConfig().getScope(); 287 int scopeLength = scope.length(); 288 if (scopeLength > 0 && uri.startsWith(scope)) { 289 result = uri.substring(scopeLength); 290 } 291 292 result = context.getContextPath() + result; 293 294 return encodeURL(result); 295 } 296 297 306 public static String getRelativePath 307 (HttpServletRequest req, WebdavServletConfig config) { 308 309 String result = null; 312 313 String pathMapperClassName = config.getInitParameter("path-mapper-hook"); 314 if (pathMapperClassName != null) { 315 try { 316 Class storeClass = Class.forName(pathMapperClassName); 317 PathMapper mapper = (PathMapper) storeClass.newInstance(); 318 result = mapper.map(req, config); 319 } catch (Exception e) { 320 Domain.log(e, LOG_CHANNEL, Logger.WARNING); 321 } 322 } 323 324 if (result == null) { 325 PathMapper mapper = (PathMapper) config.getServletContext().getAttribute( 326 "org.apache.slide.webdav.util.PathMapper"); 327 if (mapper != null) { 328 result = mapper.map(req, config); 329 } 330 } 331 332 if (result == null) { 333 if (config.isDefaultServlet()) { 334 result = req.getServletPath(); 335 } else { 336 result = req.getRequestURI(); 337 result = result.substring(req.getContextPath().length()+ req.getServletPath().length()); 338 } 339 } 340 341 if ((result == null) || (result.length() == 0)) { 343 result = "/"; 344 } 345 346 result = config.getScope() + result; 348 result = normalizeURL(fixTomcatURL(result)); return result; 350 351 } 352 353 361 public static String fixTomcatURL(String input) { 362 return fixTomcatHeader(input, "UTF-8"); 363 } 364 365 366 367 376 public static String fixTomcatHeader(String header, String toEncoding) { 377 if (header == null) return null; 379 380 String result = null; 381 try { 382 result = URLUtil.URLDecode(header.getBytes("ISO-8859-1"),"ISO-8859-1"); 383 result = decodeString(result,Configuration.urlEncoding()); 384 if (!Configuration.useUTF8()) { 385 result = new String (result.getBytes(Configuration.urlEncoding()),Configuration.urlEncoding()); 387 } 388 } catch (Exception e) { e.printStackTrace(); } 389 return result; 390 } 391 392 400 public static SlideToken getSlideToken(HttpServletRequest req) { 401 402 CredentialsToken credentialsToken; 403 Principal principal = req.getUserPrincipal(); 404 HttpSession session = req.getSession(); 405 406 if (principal == null) { 410 final String credentials = (String ) session.getAttribute(CREDENTIALS_ATTRIBUTE); 411 credentialsToken = new CredentialsToken(credentials == null ? "" : credentials); 412 } else { 413 session.setAttribute(CREDENTIALS_ATTRIBUTE, principal.getName()); 417 credentialsToken = new CredentialsToken(principal); 418 } 419 420 SlideToken token = new SlideTokenImpl(credentialsToken); 421 token.setEnforceLockTokens(true); 422 423 String [] attr = session.getValueNames(); 426 for (int i = 0; i < attr.length; i++) { 427 String name = attr[i]; 428 token.addParameter(name, session.getAttribute(name)); 429 } 430 431 return token; 432 } 433 434 435 445 public static boolean isCollection 446 (NamespaceAccessToken token, SlideToken slideToken, 447 String path) { 448 449 if( Configuration.useVersionControl() ) { 451 UriHandler uh = UriHandler.getUriHandler( path ); 452 if( uh.isWorkspaceUri() ) 453 return true; 454 if( uh.isHistoryUri() ) 455 return true; 456 if( uh.isVersionUri() ) 457 return false; 458 } 459 461 try { 462 Content content = token.getContentHelper(); 463 NodeRevisionDescriptors revisionDescriptors = 464 content.retrieve(slideToken, path); 465 if (revisionDescriptors.hasRevisions()) { 466 NodeRevisionDescriptor revisionDescriptor = 467 content.retrieve(slideToken, revisionDescriptors); 468 return isCollection(revisionDescriptor); 469 } else { 470 return true; 471 } 472 } catch(ObjectNotFoundException e) { 473 return false; 475 } catch(SlideException e) { 476 return true; 478 } 479 } 480 481 482 489 public static boolean isCollection 490 (NodeRevisionDescriptor revisionDescriptor) { 491 492 boolean result = false; 493 494 if (revisionDescriptor == null) 495 return true; 496 497 if (revisionDescriptor.propertyValueContains( 498 NodeRevisionDescriptor.RESOURCE_TYPE ,"collection")) { 499 result = true; 500 } 501 502 return result; 503 } 504 505 506 514 public static boolean isRedirectref 515 (NodeRevisionDescriptor revisionDescriptor) { 516 if (revisionDescriptor == null) 517 return false; 518 519 return revisionDescriptor.propertyValueContains( 520 NodeRevisionDescriptor.RESOURCE_TYPE, "redirectref"); 521 } 522 523 524 public static String getSlidePath(String fullpath, String slideContextPath) { 525 if (fullpath.indexOf("://") >= 0) { 527 fullpath=fullpath.substring(fullpath.indexOf("://")+3); 528 fullpath=fullpath.substring(fullpath.indexOf("/")); 529 } 530 531 if (fullpath.startsWith(slideContextPath)) { 533 fullpath=fullpath.substring(slideContextPath.length()); 534 } 535 return fullpath; 536 } 537 538 541 public static int getErrorCode(Throwable ex) { 542 if ( !(ex instanceof SlideException) ) { 543 return WebdavStatus.SC_INTERNAL_SERVER_ERROR; 545 } else { 546 return getErrorCode((SlideException)ex); 547 } 548 } 549 550 553 public static int getErrorCode(SlideException ex) { 554 try { 555 throw ex; 556 } catch(ObjectNotFoundException e) { 557 return WebdavStatus.SC_NOT_FOUND; 558 } catch(ConflictException e) { 559 return WebdavStatus.SC_CONFLICT; 560 } catch(ForbiddenException e) { 561 return WebdavStatus.SC_FORBIDDEN; 562 } catch(AccessDeniedException e) { 563 return WebdavStatus.SC_FORBIDDEN; 564 } catch(UnauthenticatedException e) { 565 return WebdavStatus.SC_UNAUTHORIZED; 566 } catch(ObjectAlreadyExistsException e) { 567 return WebdavStatus.SC_PRECONDITION_FAILED; 568 } catch(ServiceAccessException e) { 569 return getErrorCode((ServiceAccessException)ex); 570 } catch(ObjectLockedException e) { 571 return WebdavStatus.SC_LOCKED; 572 } catch(WebdavException e) { 573 return e.getStatusCode(); 574 } catch(MethodNotAllowedException e) { 575 return WebdavStatus.SC_METHOD_NOT_ALLOWED; 576 } catch(InsufficientStorageException e) { 577 return WebdavStatus.SC_INSUFFICIENT_STORAGE; 578 } catch(VetoException e) { 579 if (e != e.getCause()) { 580 return getErrorCode(e.getCause()); 581 } 582 return WebdavStatus.SC_NOT_ACCEPTABLE; 583 } catch(SlideException e) { 584 return WebdavStatus.SC_INTERNAL_SERVER_ERROR; 585 } 586 } 587 588 591 public static int getErrorCode(ServiceAccessException ex) { 592 Throwable cause = ex.getCauseException(); 593 if (cause != null && cause instanceof SocketException ) { 596 return WebdavStatus.SC_PRECONDITION_FAILED; 598 } else if (cause == null || !(cause instanceof SlideException)) { 599 if( cause != null ) cause.printStackTrace(); 601 return WebdavStatus.SC_INTERNAL_SERVER_ERROR; 603 } else { 604 return getErrorCode((SlideException)cause); 605 } 606 } 607 } 608 609 | Popular Tags |