1 23 24 package org.apache.slide.webdav.method; 25 26 import java.io.IOException ; 27 import java.util.Date ; 28 29 import org.apache.slide.common.NamespaceAccessToken; 30 import org.apache.slide.common.ServiceAccessException; 31 import org.apache.slide.common.SlideException; 32 import org.apache.slide.content.NodeProperty; 33 import org.apache.slide.content.NodeRevisionContent; 34 import org.apache.slide.content.NodeRevisionDescriptor; 35 import org.apache.slide.content.NodeRevisionDescriptors; 36 import org.apache.slide.content.NodeRevisionNumber; 37 import org.apache.slide.content.RevisionAlreadyExistException; 38 import org.apache.slide.content.RevisionDescriptorNotFoundException; 39 import org.apache.slide.event.EventDispatcher; 40 import org.apache.slide.structure.LinkedObjectNotFoundException; 41 import org.apache.slide.structure.ObjectAlreadyExistsException; 42 import org.apache.slide.structure.ObjectNotFoundException; 43 import org.apache.slide.structure.SubjectNode; 44 import org.apache.slide.util.Configuration; 45 import org.apache.slide.webdav.WebdavException; 46 import org.apache.slide.webdav.WebdavServletConfig; 47 import org.apache.slide.webdav.event.WebdavEvent; 48 import org.apache.slide.webdav.util.DeltavConstants; 49 import org.apache.slide.webdav.util.PreconditionViolationException; 50 import org.apache.slide.webdav.util.PropertyHelper; 51 import org.apache.slide.webdav.util.UriHandler; 52 import org.apache.slide.webdav.util.VersioningHelper; 53 import org.apache.slide.webdav.util.ViolatedPrecondition; 54 import org.apache.slide.webdav.util.WebdavStatus; 55 import org.apache.slide.webdav.util.WebdavUtils; 56 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind; 57 import org.apache.slide.webdav.util.resourcekind.CheckedInVersionControlled; 58 import org.apache.slide.webdav.util.resourcekind.ResourceKind; 59 60 64 public class PutMethod 65 extends AbstractWebdavMethod 66 implements DeltavConstants, WriteMethod { 67 68 69 71 74 protected VersioningHelper versioningHelper = null; 75 76 79 protected String resourcePath; 80 81 83 84 90 public PutMethod(NamespaceAccessToken token, WebdavServletConfig config) { 91 super(token, config); 92 } 93 94 96 97 102 protected void parseRequest() 103 throws WebdavException { 104 versioningHelper = VersioningHelper.getVersioningHelper(slideToken, 105 token, 106 req, 107 resp, 108 config); 109 110 resourcePath = requestUri; 111 if (resourcePath == null) { 112 resourcePath = "/"; 113 } 114 } 115 116 121 protected void executeRequest() 122 throws WebdavException, IOException { 123 124 slideToken.setForceStoreEnlistment(true); 126 127 UriHandler destUh = UriHandler.getUriHandler(resourcePath); 129 130 if (destUh.isRestrictedUri()) { 131 boolean sendError = true; 132 if( destUh.isWorkingresourceUri() ) { 133 try { 135 content.retrieve(slideToken, resourcePath); 136 sendError = false; 137 } 138 catch( Exception x ) {}; 139 } 140 if( sendError ) { 141 int statusCode = WebdavStatus.SC_FORBIDDEN; 142 sendError( statusCode, getClass().getName()+".restrictedDestinationUri", new Object []{resourcePath} ); 143 throw new WebdavException( statusCode ); 144 } 145 } 146 147 try { 148 if ( WebdavEvent.PUT.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.PUT, new WebdavEvent(this)); 150 151 try { 152 153 boolean isLockedNullResource = false; 154 155 NodeRevisionDescriptors revisionDescriptors = 156 content.retrieve(slideToken, resourcePath); 157 158 NodeRevisionNumber revisionNumber = 159 revisionDescriptors.getLatestRevision(); 160 NodeRevisionDescriptor oldRevisionDescriptor = null; 161 if (revisionNumber != null) { 162 try { 163 oldRevisionDescriptor = content.retrieve 164 (slideToken, revisionDescriptors); 165 } catch (RevisionDescriptorNotFoundException e) { 166 } 167 } 168 if (WebdavUtils.isCollection(oldRevisionDescriptor)) { 169 int statusCode = WebdavStatus.SC_METHOD_NOT_ALLOWED; 170 sendError( statusCode, getClass().getName()+".mustNotBeCollection" ); 171 throw new WebdavException( statusCode ); 172 } 173 174 NodeRevisionDescriptor revisionDescriptor = null; 175 if (oldRevisionDescriptor == null) { 176 revisionDescriptor = new NodeRevisionDescriptor(); 177 } else { 178 revisionDescriptor = oldRevisionDescriptor; 179 revisionDescriptor.setContentLength(-1); 180 } 181 182 ResourceInfo resourceInfo = 183 new ResourceInfo(resourcePath, revisionDescriptor); 184 185 if (!checkIfHeaders(req, resp, resourceInfo)) { 187 return; 188 } 189 190 ResourceKind resourceKind = AbstractResourceKind.determineResourceKind(token, resourcePath, revisionDescriptor); 191 192 versioningHelper.isWriteLocked(slideToken, revisionDescriptors); 193 194 ViolatedPrecondition violatedPrecondition = getPreconditionViolation(revisionDescriptors, revisionDescriptor, resourceKind); 196 if (violatedPrecondition != null) { 197 throw new PreconditionViolationException(violatedPrecondition, resourcePath); 198 } 199 200 boolean mustCheckIn = false; 202 if( Configuration.useVersionControl() && 203 (resourceKind instanceof CheckedInVersionControlled) && 204 versioningHelper.mustCheckoutAutoVersionedVCR(revisionDescriptors, revisionDescriptor) ) { 205 206 versioningHelper.checkout(revisionDescriptors, revisionDescriptor, false, false, true ); 207 mustCheckIn = versioningHelper.mustCheckinAutoVersionedVCR(slideToken, revisionDescriptors, revisionDescriptor); 208 } 209 211 NodeRevisionContent revisionContent = 212 new NodeRevisionContent(); 213 215 revisionContent.setContent(req.getInputStream()); 216 217 int contentLength = req.getContentLength(); 219 if (contentLength == (-1)) { 220 contentLength = revisionContent.getContentBytes().length; 221 } 222 revisionDescriptor.setContentLength(contentLength); 223 224 revisionDescriptor.setLastModified(new Date ()); 226 227 revisionDescriptor.setETag(PropertyHelper.computeEtag(resourcePath, revisionDescriptor) ); 229 230 String contentType = req.getContentType(); 232 if (contentType == null) { 233 contentType = getConfig().getServletContext() 234 .getMimeType(resourcePath); 235 } 236 if (contentType == null) { 237 contentType = getConfig().getDefaultMimeType(); 238 } 239 revisionDescriptor.setContentType(contentType); 240 241 if (isLockNull(revisionDescriptor)) { 247 isLockedNullResource = true; 249 revisionDescriptor.setContentLanguage("en"); 250 251 if( Configuration.useVersionControl() ) { 253 versioningHelper.setWorkspaceProperty( resourcePath, revisionDescriptor ); 255 } 256 } 258 259 revisionDescriptor.setResourceType(""); 261 262 if ( isLockedNullResource ) { 264 String creator = ((SubjectNode)security.getPrincipal(slideToken)).getPath().lastSegment(); 266 revisionDescriptor.setCreationUser(creator); 267 revisionDescriptor.setOwner(creator); 268 } 269 270 content.store(slideToken, resourcePath, revisionDescriptor, 271 revisionContent); 272 273 if ( isLockedNullResource ) { 276 if ( Configuration.useVersionControl() && isAutoVersionControl(resourcePath) && !isExcludedForVersionControl(resourcePath) ) { 277 versioningHelper.versionControl(resourcePath); 278 } 279 } 280 if( Configuration.useVersionControl() && mustCheckIn) { 281 versioningHelper.checkin(revisionDescriptors, revisionDescriptor, false, false, true ); } 283 285 286 resp.setHeader("ETag", revisionDescriptor.getETag() ); 288 289 resp.setStatus(WebdavStatus.SC_NO_CONTENT); 290 291 } catch (LinkedObjectNotFoundException e) { 292 int statusCode = getErrorCode( e ); 293 sendError( statusCode, e ); 294 throw new WebdavException( statusCode ); 295 } catch (ObjectNotFoundException e) { 296 SubjectNode subject = new SubjectNode(); 297 structure.create(slideToken, subject, resourcePath); 299 300 NodeRevisionDescriptor revisionDescriptor = 301 new NodeRevisionDescriptor(req.getContentLength()); 302 303 ResourceInfo resourceInfo = 304 new ResourceInfo(resourcePath, revisionDescriptor); 305 resourceInfo.exists = false; 306 307 if (!checkIfHeaders(req, resp, resourceInfo)) { 309 int statusCode = WebdavStatus.SC_PRECONDITION_FAILED; 310 sendError( statusCode, "Check If Header failed" ); 311 throw new WebdavException( statusCode ); 312 } 313 314 316 318 revisionDescriptor.setResourceType(""); 320 321 revisionDescriptor.setSource(""); 323 324 revisionDescriptor.setContentLanguage("en"); 326 327 String contentType = req.getContentType(); 329 if (contentType == null) { 330 contentType = getConfig().getServletContext() 331 .getMimeType(resourcePath); 332 } 333 if (contentType == null) { 334 contentType = getConfig().getDefaultMimeType(); 335 } 336 revisionDescriptor.setContentType(contentType); 337 338 revisionDescriptor.setLastModified(new Date ()); 340 341 revisionDescriptor.setETag(PropertyHelper.computeEtag(resourcePath, revisionDescriptor)); 343 344 revisionDescriptor.setCreationDate(new Date ()); 346 347 String creator = ((SubjectNode)security.getPrincipal(slideToken)).getPath().lastSegment(); 349 revisionDescriptor.setCreationUser(creator); 350 revisionDescriptor.setOwner(creator); 351 352 if( Configuration.useVersionControl() ) { 354 versioningHelper.setWorkspaceProperty( resourcePath, revisionDescriptor ); 356 } 357 359 if (isMsProprietarySupport()) { 360 NodeProperty property = null; 361 property = new NodeProperty("ishidden", "0", "MICROSOFT"); 363 revisionDescriptor.setProperty(property); 364 365 property = new NodeProperty("iscollection", "0", 367 "MICROSOFT"); 368 revisionDescriptor.setProperty(property); 369 370 property = new NodeProperty("isreadonly", "0", 372 "MICROSOFT"); 373 revisionDescriptor.setProperty(property); 374 375 property = new NodeProperty("lastaccessed", 377 (new Date ()).toString(), 378 "MICROSOFT"); 379 revisionDescriptor.setProperty(property); 380 381 } 382 383 NodeRevisionContent revisionContent = 385 new NodeRevisionContent(); 386 revisionContent.setContent(req.getInputStream()); 387 388 int contentLength = req.getContentLength(); 390 if (contentLength == -1) { 391 contentLength = revisionContent.getContentBytes().length; 392 } 393 revisionDescriptor.setContentLength(contentLength); 394 395 content.create(slideToken, resourcePath, revisionDescriptor, 396 revisionContent); 397 398 if ( Configuration.useVersionControl() && isAutoVersionControl(resourcePath) && !isExcludedForVersionControl(resourcePath) ) { 400 versioningHelper.versionControl(resourcePath); 401 } 402 403 resp.setHeader("ETag", revisionDescriptor.getETag() ); 405 406 resp.setStatus(WebdavStatus.SC_CREATED); 407 408 } 409 } 410 catch (PreconditionViolationException e) { 411 sendPreconditionViolation(e); 412 throw e; 413 } 414 catch (SlideException e) { 415 int statusCode = getErrorCode( e ); 416 sendError( statusCode, e ); 417 throw new WebdavException( statusCode ); 418 } 419 catch (Exception e) { 420 int statusCode = getErrorCode( e ); 421 sendError( statusCode, e ); 422 throw new WebdavException( statusCode ); 423 } 424 } 425 426 441 private ViolatedPrecondition getPreconditionViolation(NodeRevisionDescriptors revisionDescriptors, NodeRevisionDescriptor revisionDescriptor, ResourceKind resourceKind) 442 throws ServiceAccessException { 443 444 if( Configuration.useVersionControl() ) { 445 446 if (resourceKind instanceof CheckedInVersionControlled) { 447 448 String autoVersion = versioningHelper.getAutoVersionElementName(revisionDescriptor); 450 if (autoVersion == null) { 451 autoVersion = ""; 452 } 453 if ( !E_CHECKOUT_CHECKIN.equals(autoVersion) && 454 !E_CHECKOUT_UNLOCKED_CHECKIN.equals(autoVersion) && 455 !E_CHECKOUT.equals(autoVersion) && 456 !E_CHECKOUT_IGNORE_UNLOCK.equals(autoVersion) && 457 !E_LOCKED_CHECKOUT.equals(autoVersion) ) { 458 return new ViolatedPrecondition(C_CANNOT_MODIFY_VERSION_CONTROLLED_CONTENT, 459 WebdavStatus.SC_FORBIDDEN); 460 } 461 if ( E_LOCKED_CHECKOUT.equals(autoVersion) && 462 ( !versioningHelper.isWriteLocked(slideToken, revisionDescriptors) ) ) { 463 return new ViolatedPrecondition(C_CANNOT_MODIFY_VERSION_CONTROLLED_CONTENT, 464 WebdavStatus.SC_FORBIDDEN); 465 } 466 } 467 468 UriHandler uriHandler = UriHandler.getUriHandler(resourcePath); 470 if (uriHandler.isVersionUri()) { 471 return new ViolatedPrecondition(C_CANNOT_MODIFY_VERSION, 472 WebdavStatus.SC_FORBIDDEN); 473 } 474 } 475 return null; 476 } 477 478 479 480 481 482 483 486 protected int getErrorCode(SlideException ex) { 487 try { 488 throw ex; 489 } catch (RevisionAlreadyExistException e) { 490 return WebdavStatus.SC_CONFLICT; 491 } catch (ObjectAlreadyExistsException e) { 492 return WebdavStatus.SC_CONFLICT; 493 } catch (ObjectNotFoundException e) { 494 return WebdavStatus.SC_CONFLICT; 495 } catch (LinkedObjectNotFoundException e) { 496 return WebdavStatus.SC_NOT_FOUND; 497 } catch (SlideException e) { 498 return super.getErrorCode(e); 499 } catch (Exception e) { 500 return super.getErrorCode(e); 501 } 502 } 503 } 504 505 506 507 508 | Popular Tags |