001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client;
024:
025: import java.util.ArrayList;
026: import java.util.ListIterator;
027: import java.util.StringTokenizer;
028: import org.skunk.trace.Debug;
029:
030: /**
031: * not fully compliant with RFC2518:
032: * currently only supports Infinite and one Second value.
033: * may be extended to support TimeTypes of "Other" as per
034: * RFC2518, section 9.8.
035: */
036: public class Timeout {
037: public static final long MAX_SECONDS = Math.round(Math.pow(2.0,
038: 32.0)) - 1;
039: private final static String _INFINITE_VAL = "Infinite";
040: private final static String _SECOND_VAL = "Second-";
041:
042: private ArrayList timeouts = new ArrayList();
043:
044: public Timeout(Long timeout, boolean isInfinite) {
045: if ((timeout != null) && (timeout.longValue() > MAX_SECONDS))
046: throw new IllegalArgumentException(
047: "maximum timeout exceeded");
048: if ((timeout == null) && !isInfinite)
049: throw new IllegalArgumentException("must specify a timeout");
050: if (isInfinite)
051: timeouts.add(this ._INFINITE_VAL);
052: if (timeout != null)
053: timeouts.add(timeout);
054: }
055:
056: public static Timeout getTimeout(String timeoutStr) {
057: boolean isInfinite = false;
058: Long timeout = null;
059: StringTokenizer st = new StringTokenizer(timeoutStr, ":, ");
060: while (st.hasMoreTokens()) {
061: String tok = st.nextToken().trim();
062: if (tok.equals(DAVConstants.TIMEOUT_HEADER))
063: continue;
064: else if ((!isInfinite) && tok.equals(_INFINITE_VAL))
065: isInfinite = true;
066: else if ((timeout == null) && tok.equals(_SECOND_VAL)) {
067: if (st.hasMoreTokens()) {
068: String longStr = st.nextToken();
069: try {
070: timeout = Long.decode(longStr);
071: } catch (NumberFormatException nafta) {
072: Debug.trace(Timeout.class, Debug.DP3, nafta);
073: }
074: }
075: }
076: }
077: Timeout t = null;
078: try {
079: t = new Timeout(timeout, isInfinite);
080: } finally {
081: return t;
082: }
083: }
084:
085: public String toString() {
086: StringBuffer sb = new StringBuffer(DAVConstants.TIMEOUT_HEADER);
087: sb.append(": ");
088: for (ListIterator li = timeouts.listIterator(); li.hasNext();) {
089: if (li.nextIndex() > 0)
090: sb.append(", ");
091: Object nextTimeout = li.next();
092: if (nextTimeout instanceof Long)
093: sb.append(_SECOND_VAL);
094: sb.append(nextTimeout);
095: }
096: return sb.toString();
097: }
098:
099: }
100:
101: /* $Log: Timeout.java,v $
102: /* Revision 1.5 2000/12/19 22:06:15 smulloni
103: /* adding documentation.
104: /*
105: /* Revision 1.4 2000/12/03 23:53:25 smulloni
106: /* added license and copyright preamble to java files.
107: /*
108: /* Revision 1.3 2000/11/09 23:34:54 smullyan
109: /* log added to every Java file, with the help of python. Lock stealing
110: /* implemented, and treatment of locks made more robust.
111: /* */
|