001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.webdav;
034:
035: import com.flexive.war.webdav.catalina.FastHttpDateFormat;
036: import com.flexive.war.webdav.catalina.XMLWriter;
037:
038: import java.util.Date;
039: import java.util.Enumeration;
040: import java.util.Vector;
041:
042: /**
043: * Holds a lock information.
044: */
045: class LockInfo {
046:
047: static final int INFINITY = 3;
048:
049: /**
050: * Constructor.
051: */
052: public LockInfo() {
053: }
054:
055: String path = "/";
056: String type = "write";
057: String scope = "exclusive";
058: int depth = 0;
059: String owner = "";
060: Vector<String> tokens = new Vector<String>();
061: long expiresAt = 0;
062: Date creationDate = new Date();
063:
064: /**
065: * Get a String representation of this lock token.
066: */
067: public String toString() {
068:
069: String result = "Type:" + type + "\n";
070: result += "Scope:" + scope + "\n";
071: result += "Depth:" + depth + "\n";
072: result += "Owner:" + owner + "\n";
073: result += "Expiration:"
074: + FastHttpDateFormat.formatDate(expiresAt, null) + "\n";
075: Enumeration tokensList = tokens.elements();
076: while (tokensList.hasMoreElements()) {
077: result += "Token:" + tokensList.nextElement() + "\n";
078: }
079: return result;
080:
081: }
082:
083: /**
084: * Return true if the lock has expired.
085: */
086: public boolean hasExpired() {
087: return (System.currentTimeMillis() > expiresAt);
088: }
089:
090: /**
091: * Return true if the lock is exclusive.
092: */
093: public boolean isExclusive() {
094: return (scope.equals("exclusive"));
095: }
096:
097: /**
098: * Get an XML representation of this lock token. This method will
099: * append an XML fragment to the given XML writer.
100: */
101: public void toXML(XMLWriter generatedXML) {
102:
103: generatedXML
104: .writeElement(null, "activelock", XMLWriter.OPENING);
105:
106: generatedXML.writeElement(null, "locktype", XMLWriter.OPENING);
107: generatedXML.writeElement(null, type, XMLWriter.NO_CONTENT);
108: generatedXML.writeElement(null, "locktype", XMLWriter.CLOSING);
109:
110: generatedXML.writeElement(null, "lockscope", XMLWriter.OPENING);
111: generatedXML.writeElement(null, scope, XMLWriter.NO_CONTENT);
112: generatedXML.writeElement(null, "lockscope", XMLWriter.CLOSING);
113:
114: generatedXML.writeElement(null, "depth", XMLWriter.OPENING);
115: if (depth == INFINITY) {
116: generatedXML.writeText("Infinity");
117: } else {
118: generatedXML.writeText("0");
119: }
120: generatedXML.writeElement(null, "depth", XMLWriter.CLOSING);
121:
122: generatedXML.writeElement(null, "owner", XMLWriter.OPENING);
123: generatedXML.writeText(owner);
124: generatedXML.writeElement(null, "owner", XMLWriter.CLOSING);
125:
126: generatedXML.writeElement(null, "timeout", XMLWriter.OPENING);
127: long timeout = (expiresAt - System.currentTimeMillis()) / 1000;
128: generatedXML.writeText("Second-" + timeout);
129: generatedXML.writeElement(null, "timeout", XMLWriter.CLOSING);
130:
131: generatedXML.writeElement(null, "locktoken", XMLWriter.OPENING);
132: Enumeration tokensList = tokens.elements();
133: while (tokensList.hasMoreElements()) {
134: generatedXML.writeElement(null, "href", XMLWriter.OPENING);
135: generatedXML.writeText("opaquelocktoken:"
136: + tokensList.nextElement());
137: generatedXML.writeElement(null, "href", XMLWriter.CLOSING);
138: }
139: generatedXML.writeElement(null, "locktoken", XMLWriter.CLOSING);
140:
141: generatedXML
142: .writeElement(null, "activelock", XMLWriter.CLOSING);
143:
144: }
145: }
|