1
19
20 package com.liferay.portlet.journal.webdav;
21
22 import com.liferay.portal.PortalException;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.webdav.BaseResourceImpl;
26 import com.liferay.portal.webdav.BaseWebDAVStorageImpl;
27 import com.liferay.portal.webdav.Resource;
28 import com.liferay.portal.webdav.WebDAVException;
29 import com.liferay.portal.webdav.WebDAVRequest;
30 import com.liferay.portlet.journal.NoSuchStructureException;
31 import com.liferay.portlet.journal.NoSuchTemplateException;
32 import com.liferay.portlet.journal.model.JournalStructure;
33 import com.liferay.portlet.journal.model.JournalTemplate;
34 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
35 import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
36 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
37 import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
38
39 import java.io.File;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47
53 public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
54
55 public int deleteResource(WebDAVRequest webDavRequest)
56 throws WebDAVException {
57
58 try {
59 Resource resource = getResource(webDavRequest);
60
61 if (resource == null) {
62 return HttpServletResponse.SC_NOT_FOUND;
63 }
64
65 Object model = resource.getModel();
66
67 if (model instanceof JournalStructure) {
68 JournalStructure structure = (JournalStructure)model;
69
70 JournalStructureServiceUtil.deleteStructure(
71 structure.getGroupId(), structure.getStructureId());
72
73 return HttpServletResponse.SC_NO_CONTENT;
74 }
75 else if (model instanceof JournalTemplate) {
76 JournalTemplate template = (JournalTemplate)model;
77
78 JournalTemplateServiceUtil.deleteTemplate(
79 template.getGroupId(), template.getTemplateId());
80
81 return HttpServletResponse.SC_NO_CONTENT;
82 }
83 else {
84 return HttpServletResponse.SC_FORBIDDEN;
85 }
86 }
87 catch (PortalException pe) {
88 return HttpServletResponse.SC_FORBIDDEN;
89 }
90 catch (Exception e) {
91 throw new WebDAVException(e);
92 }
93 }
94
95 public Resource getResource(WebDAVRequest webDavRequest)
96 throws WebDAVException {
97
98 try {
99 String[] pathArray = webDavRequest.getPathArray();
100
101 if (pathArray.length == 3) {
102 String path = getRootPath() + webDavRequest.getPath();
103
104 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
105 }
106 else if (pathArray.length == 4) {
107 String type = pathArray[3];
108
109 return toResource(webDavRequest, type, false);
110 }
111 else if (pathArray.length == 5) {
112 String type = pathArray[3];
113 String journalTypeId = pathArray[4];
114
115 if (type.equals(_TYPE_STRUCTURES)) {
116 try {
117 JournalStructure journalStructure =
118 JournalStructureLocalServiceUtil.getStructure(
119 webDavRequest.getGroupId(), journalTypeId);
120
121 return toResource(
122 webDavRequest, journalStructure, false);
123 }
124 catch (NoSuchStructureException nsse) {
125 return null;
126 }
127 }
128 else if (type.equals(_TYPE_TEMPLATES)) {
129 try {
130 JournalTemplate journalTemplate =
131 JournalTemplateLocalServiceUtil.getTemplate(
132 webDavRequest.getGroupId(), journalTypeId);
133
134 return toResource(
135 webDavRequest, journalTemplate, false);
136 }
137 catch (NoSuchTemplateException nste) {
138 return null;
139 }
140 }
141 }
142
143 return null;
144 }
145 catch (Exception e) {
146 throw new WebDAVException(e);
147 }
148 }
149
150 public List<Resource> getResources(WebDAVRequest webDavRequest)
151 throws WebDAVException {
152
153 try {
154 String[] pathArray = webDavRequest.getPathArray();
155
156 if (pathArray.length == 3) {
157 return getFolders(webDavRequest);
158 }
159 else if (pathArray.length == 4) {
160 String type = pathArray[3];
161
162 if (type.equals(_TYPE_STRUCTURES)) {
163 return getStructures(webDavRequest);
164 }
165 else if (type.equals(_TYPE_TEMPLATES)) {
166 return getTemplates(webDavRequest);
167 }
168 }
169
170 return new ArrayList<Resource>();
171 }
172 catch (Exception e) {
173 throw new WebDAVException(e);
174 }
175 }
176
177 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
178 try {
179 Resource resource = getResource(webDavRequest);
180
181 if (resource == null) {
182 return HttpServletResponse.SC_NOT_FOUND;
183 }
184
185 Object model = resource.getModel();
186
187 if (model instanceof JournalStructure) {
188 JournalStructure structure = (JournalStructure)model;
189
190 HttpServletRequest request =
191 webDavRequest.getHttpServletRequest();
192
193 String xsd = StringUtil.read(request.getInputStream());
194
195 JournalStructureServiceUtil.updateStructure(
196 structure.getGroupId(), structure.getStructureId(),
197 structure.getName(), structure.getDescription(), xsd);
198
199 return HttpServletResponse.SC_CREATED;
200 }
201 else if (model instanceof JournalTemplate) {
202 JournalTemplate template = (JournalTemplate)model;
203
204 HttpServletRequest request =
205 webDavRequest.getHttpServletRequest();
206
207 String xsl = StringUtil.read(request.getInputStream());
208 boolean formatXsl = true;
209 File smallFile = null;
210
211 JournalTemplateServiceUtil.updateTemplate(
212 template.getGroupId(), template.getTemplateId(),
213 template.getStructureId(), template.getName(),
214 template.getDescription(), xsl, formatXsl,
215 template.getLangType(), template.isCacheable(),
216 template.isSmallImage(), template.getSmallImageURL(),
217 smallFile);
218
219 return HttpServletResponse.SC_CREATED;
220 }
221 else {
222 return HttpServletResponse.SC_FORBIDDEN;
223 }
224 }
225 catch (PortalException pe) {
226 return HttpServletResponse.SC_FORBIDDEN;
227 }
228 catch (Exception e) {
229 throw new WebDAVException(e);
230 }
231 }
232
233 protected List<Resource> getFolders(WebDAVRequest webDavRequest)
234 throws Exception {
235
236 List<Resource> folders = new ArrayList<Resource>();
237
238 folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
240 folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
241
242 return folders;
243 }
244
245 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
246 throws Exception {
247
248 List<Resource> resources = new ArrayList<Resource>();
249
250 long groupId = webDavRequest.getGroupId();
251
252 List<JournalStructure> structures =
253 JournalStructureLocalServiceUtil.getStructures(groupId);
254
255 for (JournalStructure structure : structures) {
256 Resource resource = toResource(webDavRequest, structure, true);
257
258 resources.add(resource);
259 }
260
261 return resources;
262 }
263
264 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
265 throws Exception {
266
267 List<Resource> resources = new ArrayList<Resource>();
268
269 long groupId = webDavRequest.getGroupId();
270
271 List<JournalTemplate> templates =
272 JournalTemplateLocalServiceUtil.getTemplates(groupId);
273
274 for (JournalTemplate template : templates) {
275 Resource resource = toResource(webDavRequest, template, true);
276
277 resources.add(resource);
278 }
279
280 return resources;
281 }
282
283 protected Resource toResource(
284 WebDAVRequest webDavRequest, String type, boolean appendPath) {
285
286 String parentPath = getRootPath() + webDavRequest.getPath();
287 String name = StringPool.BLANK;
288
289 if (appendPath) {
290 name = type;
291 }
292
293 Resource resource = new BaseResourceImpl(parentPath, name, type);
294
295 resource.setModel(type);
296
297 return resource;
298 }
299
300 protected Resource toResource(
301 WebDAVRequest webDavRequest, JournalStructure structure,
302 boolean appendPath) {
303
304 String parentPath = getRootPath() + webDavRequest.getPath();
305 String name = StringPool.BLANK;
306
307 if (appendPath) {
308 name = structure.getStructureId();
309 }
310
311 return new JournalStructureResourceImpl(structure, parentPath, name);
312 }
313
314 protected Resource toResource(
315 WebDAVRequest webDavRequest, JournalTemplate template,
316 boolean appendPath) {
317
318 String parentPath = getRootPath() + webDavRequest.getPath();
319 String name = StringPool.BLANK;
320
321 if (appendPath) {
322 name = template.getTemplateId();
323 }
324
325 return new JournalTemplateResourceImpl(template, parentPath, name);
326 }
327
328
330 private static final String _TYPE_STRUCTURES = "Structures";
331
332 private static final String _TYPE_TEMPLATES = "Templates";
333
334 }