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.HTTPServerConnectionHandler;
16  import org.abstracthorizon.danube.http.Status;
17  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
18  import org.abstracthorizon.danube.webdav.xml.common.XMLUtils;
19  import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
20  
21  import java.io.PrintWriter;
22  import java.util.Date;
23  
24  public class GetLastModified extends ResponseProperty {
25  
26      public static final String TAG_NAME = "getlastmodified";
27  
28      protected long modified = -1;
29      protected String cachedDate;
30  
31      /**
32       * Constructor
33       * @param status status
34       */
35      public GetLastModified(Status status) {
36          super(status);
37      }
38  
39      /**
40       * Constructor
41       * @param modified modified timestamp
42       */
43      public GetLastModified(long modified) {
44          this.modified = modified;
45      }
46  
47      /**
48       * Returns last modified timestamp
49       * @return last modified timestamp
50       */
51      public long getLastModified() {
52          return modified;
53      }
54  
55      /**
56       * Returns last modified as a formatted string. Format used is {@link HTTPServerConnectionHandler#DATE_FORMAT}
57       * @return last modified as a formatted string
58       */
59      public String asString() {
60          if (cachedDate == null) {
61              if (modified != -1) {
62                  cachedDate = HTTPServerConnectionHandler.DATE_FORMAT.format(new Date(modified));
63              }
64          }
65          return cachedDate;
66      }
67  
68      @Override
69      public String toString() {
70          if (modified != -1) {
71              return "GetLastModified[" + asString() + "]";
72          } else {
73              return "GetLastModified[]";
74          }
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          writer.println(XMLUtils.createTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME, asString()));
84      }
85  
86  }