View Javadoc

1   /*
2    * Copyright 2007-2008 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.service;
17  
18  import java.io.InputStream;
19  
20  import nl.hippo.client.api.ClientException;
21  import nl.hippo.client.api.DocumentNotFoundException;
22  import nl.hippo.client.api.content.Document;
23  import nl.hippo.client.api.content.DocumentCollection;
24  import nl.hippo.client.api.content.DocumentMetadata;
25  import nl.hippo.client.api.content.DocumentPath;
26  import nl.hippo.client.api.content.FacetCollection;
27  import nl.hippo.client.api.content.Property;
28  import nl.hippo.client.api.content.RawResponse;
29  import nl.hippo.client.api.service.CachingService;
30  import nl.hippo.client.api.service.NOPCachingService;
31  import nl.hippo.client.api.service.WebdavService;
32  import nl.hippo.client.webdav.WebdavConfig;
33  import nl.hippo.client.webdav.binding.content.DocumentFactory;
34  import nl.hippo.client.webdav.binding.facets.FacetFactory;
35  import nl.hippo.client.webdav.caching.WebdavCacheHandler;
36  import nl.hippo.client.webdav.method.Copy;
37  import nl.hippo.client.webdav.method.Delete;
38  import nl.hippo.client.webdav.method.Get;
39  import nl.hippo.client.webdav.method.Head;
40  import nl.hippo.client.webdav.method.MkCol;
41  import nl.hippo.client.webdav.method.Move;
42  import nl.hippo.client.webdav.method.PropFind;
43  import nl.hippo.client.webdav.method.PropPatch;
44  import nl.hippo.client.webdav.method.Put;
45  import nl.hippo.client.webdav.method.Search;
46  
47  public class WebdavServiceImpl implements WebdavService {
48  
49      private DocumentFactory documentFactory;
50      private FacetFactory facetFactory;
51  
52      private Get getMethod;
53      private PropFind propfindMethod;
54      private Search searchMethod;
55      private Put putMethod;
56      private PropPatch propPatchMethod;
57      private MkCol mkcolMethod;
58      private Delete deleteMethod;
59      private Head headMethod;
60      private Move moveMethod;
61      private Copy copyMethod;
62  
63      private boolean documentIdEnabled;
64  
65      private WebdavCacheHandler cache;
66      private WebdavConfig config;
67  
68      public WebdavServiceImpl(WebdavConfig config) {
69          this(config, new NOPCachingService(), false);
70      }
71  
72      public WebdavServiceImpl(WebdavConfig config, CachingService cache) {
73          this(config, cache, false);
74      }
75  
76      public WebdavServiceImpl(WebdavConfig config, boolean documentIdEnabled) {
77          this(config, new NOPCachingService(), documentIdEnabled);
78      }
79  
80      public WebdavServiceImpl(WebdavConfig config, CachingService cachingService, boolean documentIdEnabled) {
81          documentFactory = new DocumentFactory(config);
82          facetFactory = new FacetFactory(config);
83  
84          getMethod = new Get(config);
85          propfindMethod = new PropFind(config);
86          searchMethod = new Search(config);
87          putMethod = new Put(config);
88          propPatchMethod = new PropPatch(config);
89          mkcolMethod = new MkCol(config);
90          deleteMethod = new Delete(config);
91          headMethod = new Head(config);
92          moveMethod = new Move(config);
93          copyMethod = new Copy(config);
94  
95          this.config = config;
96          this.documentIdEnabled = documentIdEnabled;
97          this.cache = new WebdavCacheHandler(cachingService);
98      }
99  
100     public DocumentPath getBasePath() {
101         return config.getBasePath();
102     }
103 
104     public boolean isDocumentIdEnabled() {
105         return documentIdEnabled;
106     }
107 
108     public Document fetchDocument(DocumentPath docPath) throws ClientException {
109         return documentFactory.fetchDocument(cache, docPath);
110     }
111 
112     public RawResponse fetchContent(DocumentPath docPath) throws ClientException {
113         return getMethod.execute(cache, docPath);
114     }
115 
116     public DocumentMetadata fetchMetadata(DocumentPath docPath) throws ClientException {
117         return documentFactory.fetchMetadata(cache, docPath);
118     }
119 
120     public DocumentCollection fetchCollection(DocumentPath targetPath, InputStream dasl, boolean includeContent)
121             throws ClientException {
122         return documentFactory.fetchCollection(cache, targetPath, dasl, includeContent);
123     }
124 
125     public DocumentCollection fetchCollection(DocumentPath targetPath, String dasl, boolean includeContent)
126             throws ClientException {
127         return documentFactory.fetchCollection(cache, targetPath, dasl, includeContent);
128     }
129 
130     public FacetCollection fetchFacets(DocumentPath targetPath, InputStream dasl) throws ClientException {
131         return facetFactory.fetchFacets(cache, targetPath, dasl);
132     }
133 
134     public FacetCollection fetchFacets(DocumentPath targetPath, String dasl) throws ClientException {
135         return facetFactory.fetchFacets(cache, targetPath, dasl);
136     }
137 
138     public RawResponse executePropfind(DocumentPath docPath) throws ClientException {
139         return propfindMethod.execute(cache, docPath);
140     }
141 
142     public RawResponse executeSearch(DocumentPath targetPath, InputStream dasl) throws ClientException {
143         return searchMethod.execute(cache, targetPath, dasl);
144     }
145 
146     public RawResponse executeSearch(DocumentPath targetPath, String dasl) throws ClientException {
147         return searchMethod.execute(cache, targetPath, dasl);
148     }
149 
150     public int executePut(DocumentPath targetPath, InputStream putObject) throws ClientException {
151         return putMethod.execute(targetPath, putObject).getResponseCode();
152     }
153 
154     public int executePropPatch(DocumentPath targetPath, Property[] propertiesToRemove, Property[] propertiesToSet)
155             throws ClientException {
156         return propPatchMethod.execute(targetPath, propertiesToRemove, propertiesToSet).getResponseCode();
157     }
158 
159     public int executeDelete(DocumentPath targetPath) throws ClientException {
160         return deleteMethod.execute(targetPath).getResponseCode();
161     }
162 
163     public int executeMkCol(DocumentPath targetPath) throws ClientException {
164         return mkcolMethod.execute(targetPath).getResponseCode();
165     }
166 
167     public int executeMkCols(DocumentPath targetPath) throws ClientException {
168         String parent = targetPath.getFullPath();
169         parent = parent.substring(0, parent.lastIndexOf("/"));
170         if (parent.length() > 0) {
171             DocumentPath parentPath = this.getBasePath().createAbsolutePath(parent);
172             /* If a DocumentNotFoundException is caught on HEAD, 
173                its parent does not exist and execureMkCols
174                needs to be called recursively.
175              */
176             try {
177                 executeHead(parentPath);
178             } catch (DocumentNotFoundException e) {
179                 executeMkCols(parentPath);
180             }
181         }
182         return mkcolMethod.execute(targetPath).getResponseCode();
183     }
184 
185     public int executeHead(DocumentPath targetPath) throws ClientException {
186         return headMethod.execute(targetPath).getResponseCode();
187     }
188     
189     public int executeMove(DocumentPath targetPath, DocumentPath destinationPath, boolean overwrite) throws ClientException {
190         return moveMethod.execute(targetPath, destinationPath, overwrite).getResponseCode();
191     }
192 
193     public int executeCopy(DocumentPath targetPath, DocumentPath destinationPath, boolean overwrite) throws ClientException {
194         return copyMethod.execute(targetPath, destinationPath, overwrite).getResponseCode();
195     }    
196 
197     public Document fetchDocumentById(String documentId) throws ClientException {
198         assertDocumentIdIsEnabled();
199 
200         DocumentPath path = documentFactory.resolvePath(cache, documentId);
201         return path == null ? null : documentFactory.fetchDocument(cache, path);
202     }
203 
204     public DocumentMetadata fetchMetadataById(String documentId) throws ClientException {
205         assertDocumentIdIsEnabled();
206 
207         DocumentPath path = documentFactory.resolvePath(cache, documentId);
208         return path == null ? null : documentFactory.fetchMetadata(cache, path);
209     }
210 
211     public RawResponse fetchContentById(String documentId) throws ClientException {
212         assertDocumentIdIsEnabled();
213 
214         DocumentPath path = documentFactory.resolvePath(cache, documentId);
215         return path == null ? null : getMethod.execute(cache, path);
216     }
217 
218     private void assertDocumentIdIsEnabled() {
219         if (!isDocumentIdEnabled()) {
220             throw new IllegalStateException("Fetch by document ID has not been enabled.");
221         }
222     }
223 
224 }