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.java;
14  
15  import org.abstracthorizon.danube.http.util.IOUtils;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.InputStream;
19  
20  /**
21   * Convenience class for deletage. It allows resource's value to be obtained
22   * as a string and resource's value to be stored as a string.
23   *
24   * @author Daniel Sendula
25   */
26  public abstract class StringDelegate extends Delegate {
27  
28      /**
29       * Constructor
30       * @param path path
31       */
32      public StringDelegate(String path) {
33          super(path);
34          objectPath = IOUtils.parentPath(path);
35      }
36  
37      /**
38       * Returns value as a string
39       * @param adapter adapter
40       * @return value of the resource as a string
41       */
42      protected abstract String getValueAsString(JavaWebDAVResourceAdapter adapter);
43  
44      /**
45       * Returns resource length by checking resource's value string length
46       * @param adapter adapter
47       * @return resource length
48       */
49      public int resourceLength(JavaWebDAVResourceAdapter adapter) {
50          String p = getValueAsString(adapter);
51          if (p != null) {
52              return p.length();
53          } else {
54              return -1;
55          }
56      }
57  
58      /**
59       * Returns an input stream of the value returned from the {@link #getValueAsString(JavaWebDAVResourceAdapter)}
60       * method.
61       * @param adapter adapter
62       * @return input stream or <code>null</code>
63       */
64      public InputStream getInputStream(JavaWebDAVResourceAdapter adapter) {
65          String value = getValueAsString(adapter);
66          if (value != null) {
67              return new ByteArrayInputStream(value.getBytes());
68          } else {
69              return null;
70          }
71      }
72  
73      /**
74       * Returns type as &quot;text/plain&quot;
75       * @param adapter
76       * @return &quot;text/plain&quot;
77       */
78      public String getContentType(JavaWebDAVResourceAdapter adapter) {
79          return "text/plain";
80      }
81  
82  }