Throw any exceptions thrown unwrapped. This way, the caller knows what it's dealing...
[pithos] / src / gr / ebs / gss / server / webdav / LockInfo.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package gr.ebs.gss.server.webdav;
18
19 import java.util.Date;
20 import java.util.Enumeration;
21 import java.util.Vector;
22
23 /**
24  * Holds a lock information.
25  */
26 class LockInfo {
27
28
29         // -------------------------------------------------------- Constructor
30
31
32         /**
33          * Constructor.
34          */
35         public LockInfo() {
36
37         }
38
39
40         // ------------------------------------------------- Instance Variables
41
42
43         /**
44          *
45          */
46         String path = "/";
47         /**
48          *
49          */
50         String type = "write";
51         /**
52          *
53          */
54         String scope = "exclusive";
55         /**
56          *
57          */
58         int depth = 0;
59         /**
60          *
61          */
62         String owner = "";
63         /**
64          *
65          */
66         Vector<String> tokens = new Vector<String>();
67         /**
68          *
69          */
70         long expiresAt = 0;
71         /**
72          *
73          */
74         Date creationDate = new Date();
75
76
77         // ----------------------------------------------------- Public Methods
78
79
80         /**
81          * Get a String representation of this lock token.
82          */
83         @Override
84         public String toString() {
85
86                 String result =  "Type:" + type + "\n";
87                 result += "Scope:" + scope + "\n";
88                 result += "Depth:" + depth + "\n";
89                 result += "Owner:" + owner + "\n";
90                 result += "Expiration:"
91                         + FastHttpDateFormat.formatDate(expiresAt, null) + "\n";
92                 Enumeration tokensList = tokens.elements();
93                 while (tokensList.hasMoreElements())
94                         result += "Token:" + tokensList.nextElement() + "\n";
95                 return result;
96
97         }
98
99
100         /**
101          * Return true if the lock has expired.
102          * @return true if the lock has expired.
103          */
104         public boolean hasExpired() {
105                 return System.currentTimeMillis() > expiresAt;
106         }
107
108
109         /**
110          * Return true if the lock is exclusive.
111          * @return true if the lock is exclusive.
112          */
113         public boolean isExclusive() {
114
115                 return scope.equals("exclusive");
116
117         }
118
119
120         /**
121          * Get an XML representation of this lock token. This method will
122          * append an XML fragment to the given XML writer.
123          * @param generatedXML the generated XML writer
124          */
125         public void toXML(XMLWriter generatedXML) {
126
127                 generatedXML.writeElement(null, "activelock", XMLWriter.OPENING);
128
129                 generatedXML.writeElement(null, "locktype", XMLWriter.OPENING);
130                 generatedXML.writeElement(null, type, XMLWriter.NO_CONTENT);
131                 generatedXML.writeElement(null, "locktype", XMLWriter.CLOSING);
132
133                 generatedXML.writeElement(null, "lockscope", XMLWriter.OPENING);
134                 generatedXML.writeElement(null, scope, XMLWriter.NO_CONTENT);
135                 generatedXML.writeElement(null, "lockscope", XMLWriter.CLOSING);
136
137                 generatedXML.writeElement(null, "depth", XMLWriter.OPENING);
138                 if (depth == Webdav.INFINITY)
139                         generatedXML.writeText("Infinity");
140                 else
141                         generatedXML.writeText("0");
142                 generatedXML.writeElement(null, "depth", XMLWriter.CLOSING);
143
144                 generatedXML.writeElement(null, "owner", XMLWriter.OPENING);
145                 generatedXML.writeText(owner);
146                 generatedXML.writeElement(null, "owner", XMLWriter.CLOSING);
147
148                 generatedXML.writeElement(null, "timeout", XMLWriter.OPENING);
149                 long timeout = (expiresAt - System.currentTimeMillis()) / 1000;
150                 generatedXML.writeText("Second-" + timeout);
151                 generatedXML.writeElement(null, "timeout", XMLWriter.CLOSING);
152
153                 generatedXML.writeElement(null, "locktoken", XMLWriter.OPENING);
154                 Enumeration tokensList = tokens.elements();
155                 while (tokensList.hasMoreElements()) {
156                         generatedXML.writeElement(null, "href", XMLWriter.OPENING);
157                         generatedXML.writeText("opaquelocktoken:"
158                                                 + tokensList.nextElement());
159                         generatedXML.writeElement(null, "href", XMLWriter.CLOSING);
160                 }
161                 generatedXML.writeElement(null, "locktoken", XMLWriter.CLOSING);
162
163                 generatedXML.writeElement(null, "activelock", XMLWriter.CLOSING);
164
165         }
166
167
168 }