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 import org.abstracthorizon.danube.http.HTTPConnection;
16
17 /**
18 * Object that represents a "Depth" header of WebDAV specification.
19 *
20 * @author Daniel Sendula
21 */
22 public class Depth {
23
24 /** Depth not defined */
25 public static final int NONE = -1;
26
27 /** Depth of 0 */
28 public static final int ZERO = 0;
29
30 /** Depth of 1 */
31 public static final int ONE = 1;
32
33 /** Depth of infinity */
34 public static final int INFINITY = 2;
35
36 /**
37 * Parses the header (if present) and returns depth object
38 * @param connection connection to hold the header
39 * @return one of static objects defined here
40 */
41 public static int collectDepth(HTTPConnection connection) {
42 String depthHeader = connection.getRequestHeaders().getOnly("Depth");
43 if (depthHeader == null) {
44 return NONE;
45 } else if ("0".equals(depthHeader)) {
46 return ZERO;
47 } else if ("1".equals(depthHeader)) {
48 return ONE;
49 } else if ("infinity".equals(depthHeader)) {
50 return INFINITY;
51 } else {
52 return NONE;
53 }
54 }
55
56 /**
57 * String representation of a depth object
58 * @param depth depth object
59 * @return representation of a depth object
60 */
61 public static String toString(int depth) {
62 if (depth == NONE) {
63 return "NONE";
64 } else if (depth == ZERO) {
65 return "0";
66 } else if (depth == ONE) {
67 return "1";
68 } else if (depth == INFINITY) {
69 return "infinity";
70 } else {
71 return "NONE";
72 }
73 }
74 }