View Javadoc

1   /*
2    * Copyright 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.method;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import nl.hippo.client.api.ClientException;
24  import nl.hippo.client.api.content.Document;
25  import nl.hippo.client.api.content.DocumentPath;
26  import nl.hippo.client.api.event.EventValidity;
27  import nl.hippo.client.api.event.EventValidityImpl;
28  import nl.hippo.client.webdav.WebdavConfig;
29  import nl.hippo.client.webdav.WebdavRequest;
30  import nl.hippo.client.webdav.WebdavResponse;
31  import nl.hippo.client.webdav.binding.content.DocumentFactory;
32  import nl.hippo.client.webdav.caching.WebdavCacheHandler;
33  import nl.hippo.client.webdav.utils.Converters;
34  import nl.hippo.client.webdav.utils.Interpolation;
35  
36  public class SearchDocumentById extends Search {
37  
38      private static final String DASL_TEMPLATE = "resources/dasl-search-document-by-id.xml";
39  
40      private static String template;
41      static {
42          try {
43              InputStream is = SearchDocumentById.class.getResourceAsStream(DASL_TEMPLATE);
44              template = Converters.stream2String(is, "UTF-8");
45          } catch (IOException e) {
46              throw new ExceptionInInitializerError("Failed to load template dasl");
47          }
48      }
49  
50      public SearchDocumentById(WebdavConfig config) {
51          super(config);
52      }
53  
54      public DocumentPath execute(WebdavCacheHandler cache, DocumentFactory documentFactory, String documentId)
55              throws ClientException {
56          if (documentFactory == null) {
57              throw new IllegalArgumentException("documentFactory parameter may not be null");
58          }
59          if (documentId == null) {
60              throw new IllegalArgumentException("documentId parameter may not be null");
61          }
62  
63          WebdavRequest request = new WebdavRequest(config, documentId, "ID");
64          DocumentPath result = cache.getDocumentPath(request);
65  
66          if (result == null) {
67              Map valuesMap = new HashMap();
68              valuesMap.put("UID", documentId);
69              String dasl = Interpolation.interpolate(template, valuesMap);
70  
71              DocumentPath filesPath = config.getBasePath();
72              WebdavResponse searchResult = super.execute(filesPath, dasl);
73  
74              Document doc = documentFactory.bindDocument(WebdavCacheHandler.NULLCACHE, searchResult);
75              if (doc != null) {
76                  result = doc.getPath();
77                  if (result != null) {
78                      EventValidity validity = new EventValidityImpl(result.getFullPath());
79                      cache.store(request, validity, result);
80                  }
81              }
82          }
83          return result;
84      }
85  
86  }