jakarta-slide-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefan Fromm <s...@inf.tu-dresden.de>
Subject Re: Slide Revision Management + Revision History
Date Mon, 06 Sep 2004 12:29:35 GMT
Didn't work 2nd time. So here is the code as text:

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.slide.authenticate.CredentialsToken;
import org.apache.slide.authenticate.SecurityToken;
import org.apache.slide.common.Domain;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.common.SlideToken;
import org.apache.slide.common.SlideTokenImpl;
import org.apache.slide.content.Content;
import org.apache.slide.content.NodeProperty;
import org.apache.slide.content.NodeRevisionContent;
import org.apache.slide.content.NodeRevisionDescriptor;
import org.apache.slide.content.NodeRevisionDescriptors;
import org.apache.slide.content.NodeRevisionNumber;
import org.apache.slide.security.Security;
import org.apache.slide.structure.ObjectNotFoundException;
import org.apache.slide.structure.Structure;
import org.apache.slide.structure.SubjectNode;

public class SlideVersionsTest {

     private NamespaceAccessToken namespace;
     private SlideToken token;

     private Structure structure;
     private Content content;
     private Security security;

     private static final String AUTHORS_XML =
         "<authors>" +
             "<author>" +
                 "<firstname>Stefan</firstname>" +
                 "<lastname>Fromm</lastname>" +
             "</author>" +
         "</authors>";

     public SlideVersionsTest(String slideuser) {
         namespace = Domain.accessNamespace(new SecurityToken(""), Domain.getDefaultNamespace());
         token = new SlideTokenImpl(new CredentialsToken(slideuser));
         token.setForceStoreEnlistment(true);
         structure = namespace.getStructureHelper();
         content = namespace.getContentHelper();
         security = namespace.getSecurityHelper();
     }

     private String computeEtag(String uri, NodeRevisionDescriptor nrd)
         throws Exception {
         String result =
             System.currentTimeMillis()
                 + "_"
                 + uri.hashCode()
                 + "_"
                 + nrd.getLastModified()
                 + "_"
                 + nrd.getContentLength();
         result = new String(DigestUtils.md5Hex(result));
         return result;
     }

     public void put(String uri, String data) throws Exception {
         try {
             namespace.begin();
             try {
                 byte[] dataBytes = data.getBytes();
                 ByteArrayInputStream fis = new ByteArrayInputStream(dataBytes);
                 try
                 {
                     NodeRevisionNumber lastRevision = null;
                     try {
                         structure.retrieve(token, uri);
                         lastRevision = content.retrieve(token, uri).getLatestRevision();
                         System.out.println("Found resource with latest revision number "
+ (lastRevision == null?"none":lastRevision.toString()));
                     }
                     catch (ObjectNotFoundException e) {
                         SubjectNode subject = new SubjectNode();
                         // create object
                         structure.create(token, subject, uri);
                     }
                     if ( lastRevision != null )
                         lastRevision = new NodeRevisionNumber(lastRevision, false);
                     else
                         lastRevision = new NodeRevisionNumber();
                     // create node revision descriptor
                     NodeRevisionDescriptor revisionDescriptor = new NodeRevisionDescriptor(lastRevision,
NodeRevisionDescriptors.MAIN_BRANCH, new Vector(), new Hashtable());
                     revisionDescriptor.setResourceType("");
                     revisionDescriptor.setSource("");
                     revisionDescriptor.setContentLanguage("en");
                     revisionDescriptor.setLastModified(new Date());
                     revisionDescriptor.setETag(computeEtag(uri, revisionDescriptor));
                     revisionDescriptor.setCreationDate(new Date());
                     // Owner
                     String creator = ((SubjectNode)security.getPrincipal(token)).getPath().lastSegment();
                     revisionDescriptor.setCreationUser(creator);
                     revisionDescriptor.setOwner(creator);
                     String contentType = "text/plain";
                     revisionDescriptor.setContentType(contentType);
                     // properties
                     NodeProperty newProperty = new NodeProperty("authors", AUTHORS_XML, "stefan.fromm:");
                     revisionDescriptor.setProperty(newProperty);
                     // create content
                     NodeRevisionContent revisionContent = new NodeRevisionContent();
                     revisionContent.setContent(fis);
                     // important to create NodeRevisionDescriptors separately to be able
to tell it to use versioning
                     if ( lastRevision.toString().equals("1.0") )
                         content.create(token, uri, true);
                     content.create(token, uri, revisionDescriptor, revisionContent);
                 }
                 finally
                 {
                     fis.close();
                 }
                 namespace.commit();
             }
             catch (Exception e ) {
                 namespace.rollback();
                 throw e;
             }
         }
         catch (OutOfMemoryError e)
         {
             throw new RuntimeException(e);
         }
     }

     public NodeRevisionDescriptors getVersions(String uri) throws Exception {
         namespace.begin();
         try {
             return content.retrieve(token, uri);
         }
         finally {
             namespace.rollback();
         }
     }



     public static void main(String[] args) throws Exception {
         SlideVersionsTest test = new SlideVersionsTest("root");
         test.put("/files/test.txt", "Content 1");
         test.put("/files/test.txt", "Content 2");
         test.put("/files/test.txt", "Content 3");
         NodeRevisionDescriptors revisions = test.getVersions("/files/test.txt");
         for ( Enumeration e = revisions.enumerateRevisionNumbers(); e.hasMoreElements();
) {
             NodeRevisionNumber number = (NodeRevisionNumber)e.nextElement();
             System.out.println("number = " + number.toString());
         }
     }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: slide-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-user-help@jakarta.apache.org


Mime
View raw message