1 /*
2 * Copyright (c) 2006-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.danube.webdav.util;
14
15 /**
16 * Object that represents a "Timout" header of WebDAV specification.
17 *
18 * @author Daniel Sendula
19 */
20 public class Timeout {
21
22 /** Infinite timeout */
23 public static final Timeout INFINITE = new Timeout(-1);
24
25 /** Number of seconds */
26 protected int seconds;
27
28 /**
29 * Constructor
30 * @param timeout timeout in seconds
31 */
32 public Timeout(int timeout) {
33 this.seconds = timeout;
34 }
35
36 /**
37 * Retunrs this object's representation as required by the header
38 * @return this object's representation as required by the header
39 */
40 public String asString() {
41 if (seconds == -1) {
42 return "Infinite";
43 } else {
44 return "Second-" + seconds;
45 }
46 }
47
48 /**
49 * Returns string representation of the object
50 * @return string representation of the object
51 */
52 public String toString() {
53 if (seconds == -1) {
54 return "Timeout[Infinite]";
55 } else {
56 return "Timeout[seconds-" + seconds + "]";
57 }
58 }
59
60 public long calculateValidity() {
61 long now = System.currentTimeMillis();
62 now = now + seconds * 1000;
63 return now;
64 }
65
66 public static Timeout parse(String s) {
67 if ("Infinite".equals(s)) {
68 return INFINITE;
69 }
70 if (s.startsWith("Second-") && (s.length() > 7)) {
71 try {
72 int timeout = Integer.parseInt(s.substring(7));
73 return new Timeout(timeout);
74 } catch (NumberFormatException ignore) {
75 }
76 }
77 return null;
78 }
79 }