View Javadoc

1   package com.bradmcevoy.http;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.Serializable;
7   
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  import org.xml.sax.InputSource;
11  import org.xml.sax.SAXException;
12  import org.xml.sax.XMLReader;
13  import org.xml.sax.helpers.XMLReaderFactory;
14  
15  public class LockInfo implements Serializable{
16  
17      private static final long serialVersionUID = 1L;
18  
19      private static final Logger log = LoggerFactory.getLogger( LockInfo.class );
20  
21      public enum LockScope {
22  
23          NONE,
24          SHARED,
25          EXCLUSIVE
26      }
27  
28      public enum LockType {
29  
30          READ,
31          WRITE
32      }
33  
34      public enum LockDepth {
35  
36          ZERO,
37          INFINITY
38      }
39  
40      public static LockInfo parseLockInfo( Request request ) throws IOException, FileNotFoundException, SAXException {
41          InputStream in = request.getInputStream();
42  
43          XMLReader reader = XMLReaderFactory.createXMLReader();
44          LockInfoSaxHandler handler = new LockInfoSaxHandler();
45          reader.setContentHandler( handler );
46          reader.parse( new InputSource( in ) );
47          LockInfo info = handler.getInfo();
48          info.depth = LockDepth.INFINITY; // todo
49          info.lockedByUser = null;
50          if( request.getAuthorization() != null ) {
51              info.lockedByUser = request.getAuthorization().getUser();
52          }
53          if( info.lockedByUser == null ) {
54              log.warn( "resource is being locked with a null user. This won't really be locked at all..." );
55          }
56          log.debug( "parsed lock info: " + info );
57          return info;
58  
59      }
60      public LockScope scope;
61      public LockType type;
62  
63      /**
64       * The name of the user who has locked this resource.
65       */
66      public String lockedByUser;
67      public LockDepth depth;
68  
69      /**
70       *
71       * @param scope
72       * @param type
73       * @param lockedByUser - the identifier of the user, such as a href
74       * @param depth
75       */
76      public LockInfo( LockScope scope, LockType type, String lockedByUser, LockDepth depth ) {
77          this.scope = scope;
78          this.type = type;
79          this.lockedByUser = lockedByUser;
80          this.depth = depth;
81      }
82  
83      public LockInfo() {
84      }
85  
86      @Override
87      public String toString() {
88          return "scope: " + scope.name() + ", type: " + type.name() + ", owner: " + lockedByUser + ", depth:" + depth;
89      }
90  }