1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.webapp;
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.DocumentCollection;
25 import nl.hippo.client.api.content.DocumentPath;
26 import nl.hippo.client.api.service.WebdavService;
27 import nl.hippo.client.webdav.WebdavResponse;
28 import nl.hippo.client.webdav.caching.WebdavCacheHandler;
29 import nl.hippo.client.webdav.method.ContentIncluder;
30 import nl.hippo.client.webdav.utils.Converters;
31
32 public class ServiceWrapper {
33
34 private WebdavService webdavService;
35 private WebdavCacheHandler cacheHandler;
36
37 public ServiceWrapper(WebdavService webdavService, WebdavCacheHandler cacheHandler) {
38 this.webdavService = webdavService;
39 this.cacheHandler = cacheHandler;
40 }
41
42 public WebdavResponse getQuery(String name, Object[] parameters) {
43 String[] pathElems = new String[] { RepositoryAdapter.QUERY_PATH, name };
44 DocumentPath path = webdavService.getBasePath().createRelativePath(pathElems);
45 WebdavResponse response = executeGet(path);
46
47
48
49 if (name.endsWith(".xml")) {
50 if (response != null && parameters != null) {
51 response = response.interpolate(array2Map(parameters));
52 }
53 }
54 return response;
55 }
56
57 public DocumentCollection getQueries() {
58 DocumentPath path = webdavService.getBasePath().createRelativePath(RepositoryAdapter.QUERY_PATH);
59 InputStream stream = getClass().getResourceAsStream("resources/dasl-getchildren.xml");
60
61 DocumentCollection result = null;
62 try {
63 WebdavResponse dasl = new WebdavResponse(Converters.stream2Bytes(stream));
64 result = webdavService.fetchCollection(path, dasl.getResponseAsStream(), false);
65 } catch (ClientException e) {
66 WebAppLogger.log.error("Exception while retrieving stored queries: ", e);
67 } catch (IOException e) {
68 WebAppLogger.log.error("Exception while retrieving stored queries: ", e);
69 }
70 return result;
71 }
72
73 public WebdavResponse putQuery(String name, InputStream stream) {
74 String[] pathElems = new String[] { RepositoryAdapter.QUERY_PATH, name };
75 DocumentPath path = webdavService.getBasePath().createRelativePath(pathElems);
76
77 int result = 500;
78 try {
79 result = webdavService.executePut(path, stream);
80 } catch (ClientException e) {
81 WebAppLogger.log.error("Put to '" + path.getFullPath() + "' failed", e);
82 }
83 return new WebdavResponse(result);
84 }
85
86 public WebdavResponse deleteQuery(String name) {
87 String[] pathElems = new String[] { RepositoryAdapter.QUERY_PATH, name };
88 DocumentPath path = webdavService.getBasePath().createRelativePath(pathElems);
89
90 int result = 500;
91 try {
92 result = webdavService.executeDelete(path);
93 } catch (ClientException e) {
94 WebAppLogger.log.error("Delete '" + path.getFullPath() + "' failed", e);
95 }
96 return new WebdavResponse(result);
97 }
98
99 public WebdavResponse executeMkCol(DocumentPath path) {
100 int result = 500;
101 try {
102 result = webdavService.executeMkCol(path);
103 } catch (ClientException e) {
104 WebAppLogger.log.error("MkCol '" + path.getFullPath() + "' failed", e);
105 }
106 return new WebdavResponse(result);
107 }
108
109 public WebdavResponse executeGet(DocumentPath path) {
110 WebdavResponse response = null;
111 try {
112 response = (WebdavResponse) webdavService.fetchContent(path);
113 if (response == null || !response.isValid()) {
114 WebAppLogger.log.error("Document '" + path.getFullPath() + "' not found");
115 }
116 } catch (ClientException e) {
117 WebAppLogger.log.error("Failed to fetch document '" + path.getFullPath(), e);
118 }
119 return response;
120 }
121
122 public DocumentPath getBasePath() {
123 return webdavService.getBasePath();
124 }
125
126 public WebdavResponse searchIncludeContent(InputStream dasl, DocumentPath target) {
127 WebdavResponse result = null;
128 try {
129 WebdavResponse searchResult = (WebdavResponse) webdavService.executeSearch(target, dasl);
130 ContentIncluder contentIncluder = new ContentIncluder(webdavService, cacheHandler);
131 result = contentIncluder.includeContent(searchResult);
132 } catch (ClientException e) {
133 WebAppLogger.log.error("Exception while including content", e);
134 }
135 return result;
136 }
137
138 public WebdavResponse transform(WebdavResponse xml, WebdavResponse xsl) {
139 WebdavResponse result = null;
140 try {
141 result = xml.transform(xsl, cacheHandler);
142 } catch (ClientException e) {
143 WebAppLogger.log.error("Exception while transforming content", e);
144 }
145 return result;
146 }
147
148 private Map array2Map(Object[] parameters) {
149 Map map = new HashMap();
150 for (int i = 0; i < parameters.length; i++) {
151 map.put("_" + String.valueOf(i + 1), parameters[i]);
152 }
153 return map;
154 }
155
156 }