View Javadoc

1   package nl.hippo.adapter.webservice;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.util.Properties;
7   
8   import javax.servlet.ServletConfig;
9   import javax.servlet.ServletException;
10  
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import nl.hippo.client.api.ClientException;
15  import nl.hippo.client.api.content.Document;
16  import nl.hippo.client.api.content.DocumentCollection;
17  import nl.hippo.client.api.content.DocumentMetadata;
18  import nl.hippo.client.api.content.DocumentPath;
19  import nl.hippo.client.api.content.FacetCollection;
20  import nl.hippo.client.api.event.EventAwareManager;
21  import nl.hippo.client.api.service.CachingService;
22  import nl.hippo.client.api.service.NOPCachingService;
23  import nl.hippo.client.api.service.NOPUpdateNotificationService;
24  import nl.hippo.client.api.service.UpdateNotificationService;
25  import nl.hippo.client.api.service.WebdavService;
26  import nl.hippo.client.caching.JCSDefaultCache;
27  import nl.hippo.client.caching.service.CachingServiceImpl;
28  import nl.hippo.client.event.service.UpdateNotificationConfig;
29  import nl.hippo.client.event.service.UpdateNotificationServiceImpl;
30  import nl.hippo.client.webdav.WebdavConfig;
31  import nl.hippo.client.webdav.service.WebdavServiceImpl;
32  
33  import com.caucho.hessian.server.HessianServlet;
34  
35  public class ContentServiceImpl extends HessianServlet implements ContentService {
36      private static final long serialVersionUID = 1L;
37  
38      public static Logger log = LoggerFactory.getLogger(ContentServiceImpl.class.getPackage().getName());
39  
40      private WebdavService webdavService;
41      private UpdateNotificationService updateNotifier;
42  
43      //@Override
44      public void init(ServletConfig servletConfig) throws ServletException {
45          super.init(servletConfig);        
46          String configParam = getInitParameter("config");
47          if (configParam == null || configParam.equals("")) {
48              configParam = getServletContext().getInitParameter("config");
49          }
50          if (configParam == null || configParam.equals("")) {
51              throw new ServletException("Missing configuration parameter 'config'");
52          }
53          log.info("Initializing Hippo Repository WebService from " + configParam);
54  
55          String filename = "WEB-INF/" + configParam;
56          Properties properties = new Properties();
57          InputStream propStream = getServletContext().getResourceAsStream(filename);
58          //InputStream propStream = getClass().getClassLoader().getResourceAsStream(filename);
59          if (propStream == null) {
60              throw new ServletException("Unable to find configuration file " + filename);
61          }
62          try {
63              properties.load(propStream);
64          } catch (IOException e) {
65              throw new ServletException("Failed to load configuration file " + filename);
66          }
67  
68          WebdavConfig webdavConfig = new WebdavConfig(properties);
69          CachingService cache;
70          if ("true".equals(properties.getProperty("hippo.client.caching"))) {
71              try {
72                  UpdateNotificationConfig updatenotifierConfig = new UpdateNotificationConfig(properties);
73                  updateNotifier = new UpdateNotificationServiceImpl(updatenotifierConfig);
74                  updateNotifier.start();
75                  log.info("UpdateNotifier initialized");
76  
77                  String namespace = webdavConfig.getNamespace();
78                  EventAwareManager manager = updateNotifier.getEventAwareManager();
79                  cache = new CachingServiceImpl(new JCSDefaultCache("default"), manager, namespace);
80                  log.info("Cachemanager initialized");
81              } catch (ClientException e) {
82                  log.error("Failed to initialize caching service, falling back to uncached service", e);
83                  updateNotifier = new NOPUpdateNotificationService();
84                  cache = new NOPCachingService();
85              }
86          } else {
87              updateNotifier = new NOPUpdateNotificationService();
88              cache = new NOPCachingService();
89          }
90          webdavService = new WebdavServiceImpl(webdavConfig, cache);
91          log.info("WebdavService initialized");
92      }
93  
94      //@Override
95      public void destroy() {
96          super.destroy();
97          updateNotifier.stop();
98      }
99  
100     //  low level webdav
101 
102     public byte[] get(String path) {
103         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
104         byte[] result;
105         try {
106             result = webdavService.fetchContent(docPath).getResponseAsBytes();
107         } catch (ClientException e) {
108             result = e.getMessage().getBytes();
109         }
110         return result;
111     }
112 
113     public int put(String path, String content) {
114         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
115         int result;
116         try {
117             result = webdavService.executePut(docPath, new ByteArrayInputStream(content.getBytes()));
118         } catch (ClientException e) {
119             result = -1;
120         }
121         return result;
122     }
123 
124     public int delete(String path) {
125         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
126         int result;
127         try {
128             result = webdavService.executeDelete(docPath);
129         } catch (ClientException e) {
130             result = -1;
131         }
132         return result;
133     }
134 
135     public int mkcol(String path) {
136         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
137         int result;
138         try {
139             result = webdavService.executeMkCol(docPath);
140         } catch (ClientException e) {
141             result = -1;
142         }
143         return result;
144     }
145 
146     public byte[] propfind(String path) {
147         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
148         byte[] result;
149         try {
150             result = webdavService.executePropfind(docPath).getResponseAsBytes();
151         } catch (ClientException e) {
152             result = e.getMessage().getBytes();
153         }
154         return result;
155     }
156 
157     public byte[] search(String target, String query) {
158         DocumentPath docPath = webdavService.getBasePath().createRelativePath(target);
159         byte[] result;
160         try {
161             result = webdavService.executeSearch(docPath, query).getResponseAsBytes();
162         } catch (ClientException e) {
163             result = e.getMessage().getBytes();
164         }
165         return result;
166     }
167 
168     //  high level webdav, results are bound to objects
169 
170     public Document getDocument(String target) {
171         DocumentPath docPath = webdavService.getBasePath().createRelativePath(target);
172         Document result;
173         try {
174             return webdavService.fetchDocument(docPath);
175         } catch (ClientException e) {
176             log.error("An exception occurred: " + e.getMessage());
177             result = null;
178         }
179         return result;
180     }
181 
182     public DocumentCollection searchDocuments(String target, String query, boolean includeContent) {
183         DocumentPath docPath = webdavService.getBasePath().createRelativePath(target);
184         DocumentCollection result;
185         try {
186             return webdavService.fetchCollection(docPath, query, includeContent);
187         } catch (ClientException e) {
188             log.error("An exception occurred: " + e.getMessage());
189             result = null;
190         }
191         return result;
192     }
193 
194     public FacetCollection searchFacets(String target, String query) {
195         DocumentPath docPath = webdavService.getBasePath().createRelativePath(target);
196         FacetCollection result;
197         try {
198             return webdavService.fetchFacets(docPath, query);
199         } catch (ClientException e) {
200             log.error("An exception occurred: " + e.getMessage());
201             result = null;
202         }
203         return result;
204     }
205 
206     public DocumentMetadata getDocumentMetadata(String path) {
207         DocumentPath docPath = webdavService.getBasePath().createRelativePath(path);
208         DocumentMetadata result;
209         try {
210             return webdavService.fetchMetadata(docPath);
211         } catch (ClientException e) {
212             log.error("An exception occurred: " + e.getMessage());
213             result = null;
214         }
215         return result;
216     }
217 
218     // byId methods
219 
220     public byte[] getById(String id) {
221         byte[] result;
222         try {
223             result = webdavService.fetchContentById(id).getResponseAsBytes();
224         } catch (ClientException e) {
225             result = e.getMessage().getBytes();
226         }
227         return result;
228     }
229 
230     public Document getDocumentById(String id) {
231         Document result;
232         try {
233             return webdavService.fetchDocumentById(id);
234         } catch (ClientException e) {
235             log.error("An exception occurred: " + e.getMessage());
236             result = null;
237         }
238         return result;
239     }
240 
241     public DocumentMetadata getDocumentMetadataById(String id) {
242         DocumentMetadata result;
243         try {
244             return webdavService.fetchMetadataById(id);
245         } catch (ClientException e) {
246             log.error("An exception occurred: " + e.getMessage());
247             result = null;
248         }
249         return result;
250     }
251 
252 }