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