View Javadoc

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.xml.dav.response.properties;
14  
15  import org.abstracthorizon.danube.http.Status;
16  import org.abstracthorizon.danube.webdav.lock.Lock;
17  import org.abstracthorizon.danube.webdav.lock.LockingMechanism;
18  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
19  import org.abstracthorizon.danube.webdav.xml.common.XMLUtils;
20  import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
21  import org.abstracthorizon.danube.webdav.xml.dav.HRef;
22  import org.abstracthorizon.danube.webdav.xml.dav.response.Depth;
23  import org.abstracthorizon.danube.webdav.xml.dav.response.LockScope;
24  import org.abstracthorizon.danube.webdav.xml.dav.response.LockType;
25  import org.abstracthorizon.danube.webdav.xml.dav.response.Owner;
26  
27  import java.io.PrintWriter;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * This class models WebDAV's lockdiscovery response tag
33   *
34   * @author Daniel Sendula
35   */
36  public class LockDiscovery extends ResponseProperty {
37  
38      /** Tag name */
39      public static final String TAG_NAME = "lockdiscovery";
40  
41      /** Active lock tag name */
42      public static final String ACTIVE_LOCK_TAG_NAME = "activelock";
43  
44      /** Timeout tag name */
45      public static final String TIMEOUT_TAG_NAME = "timeout";
46  
47      /** Lock token tag name */
48      public static final String LOCK_TOKEN_TAG_NAME = "locktoken";
49  
50      /** List of locks */
51      protected List<Lock> locks;
52  
53      /**
54       * Constructor
55       * @param status status
56       */
57      public LockDiscovery(Status status) {
58          super(status);
59      }
60  
61      @Override
62      public String toString() {
63          return "LockDiscovery[]";
64      }
65  
66      /**
67       * Returns list of locks. It will never be <code>null</code>.
68       * @return list of locks
69       */
70      public List<Lock> getLocks() {
71          if (locks == null) {
72              locks = new ArrayList<Lock>();
73          }
74          return locks;
75      }
76  
77      /**
78       * Renders the tag
79       * @param writer writer
80       * @param provider namespace provider
81       */
82      public void render(PrintWriter writer, NamespacesProvider provider) {
83          if (locks == null) {
84              writer.println(XMLUtils.createEmptyTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
85          } else {
86              writer.println(XMLUtils.createStartTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
87              for (Lock lock : locks) {
88                  writer.println(XMLUtils.createStartTag(provider, DAVNamespace.DAV_NAMESPACE_URL, ACTIVE_LOCK_TAG_NAME));
89                  LockType.WRITE.render(writer, provider);
90                  if (lock.getScope() == LockingMechanism.SCOPE_EXCLUSIVE) {
91                      LockScope.EXCLUSIVE.render(writer, provider);
92                  } else {
93                      LockScope.SHARED.render(writer, provider);
94                  }
95                  Depth.DEPTHS[lock.getDepth()].render(writer, provider);
96                  if (lock.getOwner() != null) {
97                      Owner owner = new Owner(lock.getOwner());
98                      owner.render(writer, provider);
99                  }
100                 if (lock.getTimeout() != null) {
101                     writer.println(XMLUtils.createTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TIMEOUT_TAG_NAME, lock.getTimeout().asString()));
102                 }
103                 if (lock.getToken() != null) {
104                     HRef href = new HRef(lock.getToken().toString());
105                     writer.println(XMLUtils.createStartTag(provider, DAVNamespace.DAV_NAMESPACE_URL, LOCK_TOKEN_TAG_NAME));
106                     href.render(writer, provider);
107                     writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, LOCK_TOKEN_TAG_NAME));
108                 }
109 
110                 writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, ACTIVE_LOCK_TAG_NAME));
111             }
112             writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
113         }
114     }
115 }