View Javadoc

1   /*
2    * Copyright 2007-2009 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.webdav;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.io.Serializable;
22  import java.util.Properties;
23  
24  import nl.hippo.client.api.ClientException;
25  import nl.hippo.client.api.content.DocumentPath;
26  
27  /**
28   * Abstract base class for all configuration classes
29   */
30  public abstract class AbstractWebdavConfig implements Serializable {
31  
32      public static final String WEBDAV_HOST = "hippo.client.host";
33      public static final String WEBDAV_PORT = "hippo.client.port";
34      public static final String WEBDAV_PROTOCOL = "hippo.client.protocol";
35      public static final String WEBDAV_USERNAME = "hippo.client.username";
36      public static final String WEBDAV_PASSWORD = "hippo.client.password";
37      public static final String WEBDAV_REALM = "hippo.client.realm";
38      public static final String WEBDAV_CONTEXTROOT = "hippo.client.contextroot";
39      public static final String WEBDAV_NAMESPACE = "hippo.client.namespace";
40      public static final String WEBDAV_FILESPATH = "hippo.client.filespath";
41      public static final String WEBDAV_LOGLABEL = "hippo.client.loglabel";
42      public static final String WEBDAV_SEARCHNOACL = "hippo.client.searchnoacl";
43  
44      protected String host;
45      protected int port;
46      protected String protocol;
47      protected String username;
48      protected String password;
49      protected String realm;
50      protected String contextroot;
51      protected String namespace;
52      protected String filespath;
53      protected String loglabel;
54      protected boolean searchNoACL;
55  
56      public AbstractWebdavConfig() {
57      }
58  
59      /**
60       * Read property values from an InputStream on a properties file.
61       * @param stream An InputStream on a properties file.
62       * @throws ClientException if something went wrong while reading from the stream
63       */
64      public AbstractWebdavConfig(InputStream stream) throws ClientException  {
65          Properties config = new Properties();
66          try {
67              config.load(stream);
68          } catch (IOException e) {
69              throw new ClientException("Error while loading configuration from stream", e);
70          }
71          init(config);
72      }
73  
74      public AbstractWebdavConfig(Properties properties) {
75          init(properties);
76      }
77  
78      private void init(Properties properties) {
79          host = properties.getProperty(WEBDAV_HOST);
80          try {
81              port = Integer.valueOf(properties.getProperty(WEBDAV_PORT)).intValue();
82          } catch (NumberFormatException e) {
83              port = 0;
84          }
85          protocol = properties.getProperty(WEBDAV_PROTOCOL);
86          username = properties.getProperty(WEBDAV_USERNAME);
87          password = properties.getProperty(WEBDAV_PASSWORD);
88          realm = properties.getProperty(WEBDAV_REALM);
89          contextroot = properties.getProperty(WEBDAV_CONTEXTROOT);
90          namespace = properties.getProperty(WEBDAV_NAMESPACE);
91          filespath = properties.getProperty(WEBDAV_FILESPATH);
92          loglabel = properties.getProperty(WEBDAV_LOGLABEL, "");
93          try {
94              searchNoACL = Boolean.valueOf(properties.getProperty(WEBDAV_SEARCHNOACL, "false")).booleanValue();
95          } catch (NumberFormatException e) {
96              searchNoACL = false;
97          }
98      }
99  
100     public DocumentPath getBasePath() {
101         return new DocumentPathImpl(namespace, contextroot, filespath, "", true);
102     }
103 
104     public String getUsername() {
105         return username;
106     }
107 
108     public String getPassword() {
109         return password;
110     }
111 
112     public String getHost() {
113         return host;
114     }
115 
116     public int getPort() {
117         return port;
118     }
119     
120     public String getProtocol() {
121         return protocol;
122     }
123 
124     public String getRealm() {
125         return realm;
126     }
127 
128     public String getContextRoot() {
129         return contextroot;
130     }
131 
132     public String getNamespace() {
133         return namespace;
134     }
135 
136     public String getFilespath() {
137         return filespath;
138     }
139 
140     public String getLoglabel() {
141         return loglabel;
142     }
143 
144     public boolean getSearchNoACL() {
145         return searchNoACL;
146     }
147 
148     /**
149      * Save the configuration to the specified OutputStream.
150      *
151      * @param stream the OutputStream used to save the configuration to.
152      * @throws ClientException if an error occurs while writing to the output stream 
153      */
154     public void save(OutputStream stream) throws ClientException  {
155         Properties conf = new Properties();
156         conf.setProperty(WEBDAV_HOST, host);
157         conf.setProperty(WEBDAV_PORT, String.valueOf(port));
158         if (protocol != null) {
159             conf.setProperty(WEBDAV_PROTOCOL, protocol);
160         }
161         conf.setProperty(WEBDAV_USERNAME, username);
162         conf.setProperty(WEBDAV_PASSWORD, password);
163         conf.setProperty(WEBDAV_REALM, realm);
164         if (contextroot != null) {
165             conf.setProperty(WEBDAV_CONTEXTROOT, contextroot);
166         }
167         conf.setProperty(WEBDAV_NAMESPACE, namespace);
168         conf.setProperty(WEBDAV_FILESPATH, filespath);
169         conf.setProperty(WEBDAV_LOGLABEL, loglabel);
170         conf.setProperty(WEBDAV_SEARCHNOACL, String.valueOf(searchNoACL));
171         try {
172             conf.store(stream, "Configuration for Hippo Repository Webdav Client");
173         } catch (IOException e) {
174             throw new ClientException("Error while writing configuration to stream", e);
175         }
176     }
177 
178 }