juergen     02/03/14 22:44:10

  Modified:    src/webdav/server/org/apache/slide/webdav/util
                        VersioningHelper.java
  Log:
  Method getVersioningHelper() now takes additional parameter 'requestContent'.
  Implemented precondition check for method 'checkout()'.
  (ralf)
  
  Revision  Changes    Path
  1.7       +113 -19   
jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/VersioningHelper.java
  
  Index: VersioningHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/VersioningHelper.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- VersioningHelper.java     13 Mar 2002 11:58:01 -0000      1.6
  +++ VersioningHelper.java     15 Mar 2002 06:44:10 -0000      1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/VersioningHelper.java,v
 1.6 2002/03/13 11:58:01 juergen Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/03/13 11:58:01 $
  + * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/VersioningHelper.java,v
 1.7 2002/03/15 06:44:10 juergen Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/03/15 06:44:10 $
    *
    * ====================================================================
    *
  @@ -66,12 +66,15 @@
   import java.io.*;
   import java.util.*;
   
  -import org.jdom.Document;
   import org.jdom.Element;
  +import org.jdom.Document;
  +import org.jdom.Namespace;
   import org.jdom.JDOMException;
   
   import org.jdom.input.SAXBuilder;
   
  +import org.jdom.output.XMLOutputter;
  +
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   
  @@ -112,9 +115,9 @@
        */
       public static VersioningHelper
       getVersioningHelper( SlideToken sToken, NamespaceAccessToken nsaToken,
  -    HttpServletRequest req, HttpServletResponse resp, WebdavServletConfig sConf ) {
  +                            HttpServletRequest req, HttpServletResponse resp, 
Document requestContent, WebdavServletConfig sConf ) {
                                
  -        return new VersioningHelper( sToken, nsaToken, req, resp, sConf );
  +        return new VersioningHelper( sToken, nsaToken, req, resp, requestContent, 
sConf );
       }
       
       /**
  @@ -128,15 +131,17 @@
       private HttpServletResponse resp = null;
       private WebdavServletConfig sConf = null;
       private PropertyHelper pHelp = null;
  +    private Document requestContent = null;
       
       /**
        * Protected contructor
        */
       protected VersioningHelper( SlideToken sToken, NamespaceAccessToken nsaToken,
  -    HttpServletRequest req, HttpServletResponse resp, WebdavServletConfig sConf ) {
  +                               HttpServletRequest req, HttpServletResponse resp, 
Document requestContent, WebdavServletConfig sConf ) {
           super( sToken, nsaToken );
           this.req = req;
           this.resp = resp;
  +        this.requestContent = requestContent;
           this.sConf = sConf;
           this.content = nsaToken.getContentHelper();
           this.structure = nsaToken.getStructureHelper();
  @@ -294,7 +299,7 @@
        * Checkout the specified resource
        */
       public void checkout( String resourcePath, boolean forkOk, boolean 
applyToVersion )
  -        throws SlideException, JDOMException {
  +        throws SlideException, JDOMException, IOException {
           
           NodeRevisionDescriptors rNrds = retrieveRevisionDescriptors( resourcePath );
           NodeRevisionDescriptor rNrd = retrieveLatestRevisionDescriptor( 
resourcePath, rNrds );
  @@ -306,7 +311,7 @@
        */
       public void checkout( NodeRevisionDescriptors rNrds,
       NodeRevisionDescriptor rNrd, boolean forkOk, boolean applyToVersion )
  -        throws SlideException, JDOMException {
  +        throws SlideException, JDOMException, IOException {
           
           Iterator i;
           ResourceKind rRk = AbstractResourceKind.determineResourceKind( rNrd );
  @@ -314,8 +319,7 @@
           if( !rRk.isSupportedMethod(req.getMethod()) ) {
               // check precondition C_MUST_BE_CHECKED_IN
               if( rRk instanceof CheckedOut ) {
  -                resp.setStatus(WebdavStatus.SC_CONFLICT);
  -                // TODO: set response error element
  +                sendPreconditionViolation(new 
ViolatedPrecondition(C_MUST_BE_CHECKED_IN, WebdavStatus.SC_CONFLICT));
                   return;
               }
               resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  @@ -333,10 +337,11 @@
               NodeRevisionDescriptors cinNrds = content.retrieve(sToken, cinUri);
               NodeRevisionDescriptor cinNrd = content.retrieve(sToken, cinNrds, 
cinNrn);
               
  -            // TODO: check precondition 
C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_FORBIDDEN
  -            // TODO: check precondition 
C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_DISCOURAGED
  -            // TODO: check precondition 
C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_FORBIDDEN
  -            // TODO: check precondition 
C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_DISCOURAGED
  +            ViolatedPrecondition violatedPrecondition = 
getCheckoutPreconditionViolation(cinNrd, cinNrds);
  +            if (violatedPrecondition != null) {
  +                sendPreconditionViolation(violatedPrecondition);
  +                return;
  +            }
               
               addUriToCheckoutSet(cinNrd, rNrds.getUri());
               
  @@ -363,10 +368,66 @@
       }
       
       /**
  +     * Returns the ViolatedPrecondition if one of the precondition defined for
  +     * the <code>CHECKOUT</code> methods has been violated, otherwise
  +     * <code>null</code>.
  +     *
  +     * @param      cinNrd   the NodeRevisionDescriptor of the VR to checkout.
  +     * @param      cinNrds  the NodeRevisionDescriptors of the VR to checkout.
  +     *
  +     * @return     the ViolatedPrecondition (if any).
  +     */
  +    private ViolatedPrecondition 
getCheckoutPreconditionViolation(NodeRevisionDescriptor cinNrd, 
NodeRevisionDescriptors cinNrds) throws IllegalArgumentException, IOException, 
JDOMException {
  +        
  +        ViolatedPrecondition violatedPrecondition = null;
  +        
  +        NodeProperty checkoutForkProperty =cinNrd.getProperty(P_CHECKOUT_FORK);
  +        if (checkoutForkProperty != null) {
  +            Element checkoutForkElement = 
pHelp.parsePropertyValue(checkoutForkProperty.getValue().toString());
  +            if (checkoutForkElement != null) {
  +                
  +                // check if the version has successors
  +                if 
(cinNrds.getSuccessors(cinNrd.getRevisionNumber()).hasMoreElements()) {
  +                    
  +                    // check precondition 
C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_FORBIDDEN
  +                    if (E_FORBIDDEN.equals(checkoutForkElement.getName()))  {
  +                        violatedPrecondition = new 
ViolatedPrecondition(C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_FORBIDDEN, 
WebdavStatus.SC_FORBIDDEN);
  +                    }
  +                    
  +                    // check precondition 
C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_DISCOURAGED
  +                    if (E_DISCOURAGED.equals(checkoutForkElement.getName()) && 
!isForkOK())  {
  +                        violatedPrecondition = new 
ViolatedPrecondition(C_CHECKOUT_OF_VERSION_WITH_DESCENDANT_IS_DISCOURAGED, 
WebdavStatus.SC_CONFLICT);
  +                    }
  +                }
  +                
  +                // check if the version is already checked out
  +                NodeProperty checkoutSetProperty = 
cinNrd.getProperty(P_CHECKOUT_SET);
  +                if ( (checkoutSetProperty != null) && 
(checkoutSetProperty.getValue() != null) ) {
  +                    XMLValue checkoutSetValue = new 
XMLValue(checkoutSetProperty.getValue().toString());
  +                    if (checkoutSetValue.iterator().hasNext()) {
  +                        
  +                        // check precondition 
C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_FORBIDDEN
  +                        if (E_FORBIDDEN.equals(checkoutForkElement.getName()))  {
  +                            violatedPrecondition = new 
ViolatedPrecondition(C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_FORBIDDEN, 
WebdavStatus.SC_FORBIDDEN);
  +                        }
  +                        
  +                        // check precondition 
C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_DISCOURAGED
  +                        if (E_DISCOURAGED.equals(checkoutForkElement.getName()) && 
!isForkOK())  {
  +                            violatedPrecondition = new 
ViolatedPrecondition(C_CHECKOUT_OF_CHECKED_OUT_VERSION_IS_DISCOURAGED, 
WebdavStatus.SC_CONFLICT);
  +                        }
  +                    }
  +                }
  +            }
  +        }
  +        
  +        return violatedPrecondition;
  +    }
  +    
  +    /**
        * Checkin the specified resource
        */
       public void checkin( String resourcePath, boolean forkOk, boolean 
keepCheckedOut )
  -    throws SlideException, JDOMException {
  +        throws SlideException, JDOMException, IOException {
           
           NodeRevisionDescriptors rNrds = retrieveRevisionDescriptors( resourcePath );
           NodeRevisionDescriptor rNrd = retrieveLatestRevisionDescriptor( 
resourcePath, rNrds );
  @@ -378,7 +439,7 @@
        */
       public void checkin( NodeRevisionDescriptors rNrds,
       NodeRevisionDescriptor rNrd, boolean forkOk, boolean keepCheckedOut )
  -    throws SlideException, JDOMException {
  +        throws SlideException, JDOMException, IOException {
           
           Iterator i;
           Enumeration j;
  @@ -388,8 +449,7 @@
           if( !rRk.isSupportedMethod(req.getMethod()) ) {
               // check precondition C_MUST_BE_CHECKED_OUT
               if( !(rRk instanceof CheckedOut) ) {
  -                resp.setStatus(WebdavStatus.SC_CONFLICT);
  -                // TODO: set response error element
  +                sendPreconditionViolation(new 
ViolatedPrecondition(C_MUST_BE_CHECKED_OUT, WebdavStatus.SC_CONFLICT));
                   return;
               }
               resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  @@ -425,6 +485,7 @@
               i = pHelp.createInitialProperties(VersionImpl.getInstance()).iterator();
               while( i.hasNext() )
                   vrNrdNew.setProperty( (NodeProperty)i.next() );
  +            
               // Copy dead properties VCR --> VR-new
               j = rNrd.enumerateProperties();
               while( j.hasMoreElements() ) {
  @@ -542,6 +603,39 @@
               }
           }
       }
  +    
  +    /**
  +     * Sends a precondition vilolation response.
  +     *
  +     * @param     violatedPrecondition  the precondition that has been violated.
  +     */
  +    protected void sendPreconditionViolation(ViolatedPrecondition 
violatedPrecondition) throws IOException {
  +        
  +        if (violatedPrecondition != null) {
  +            
  +            resp.setStatus(violatedPrecondition.getStatusCode());
  +            
  +            Element errorElement = new Element(E_ERROR, 
Namespace.getNamespace(DEFAULT_NAMESPACE));
  +            Element preconditionElement = new 
Element(violatedPrecondition.getPrecondition(),  
Namespace.getNamespace(DEFAULT_NAMESPACE));
  +            errorElement.addContent(preconditionElement);
  +            new XMLOutputter().output(errorElement, resp.getWriter());
  +        }
  +    }
  +    
  +    /**
  +     * Returns <code>true</code> if the request content contains a
  +     * <code>&lt;fork-ok&gt;</code> element.
  +     *
  +     * @return     <code>true</code> if the request content contains a
  +     *             <code>&lt;fork-ok&gt;</code> element.
  +     */
  +    protected boolean isForkOK() {
  +        
  +        return (requestContent != null) &&
  +            (requestContent.getRootElement().getChild(E_FORK_OK,
  +                                                      
Namespace.getNamespace(DEFAULT_NAMESPACE)) != null);
  +    }
  +    
       
       /**
        ** Expects a String containing an XML Element like
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to