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.service.ServiceContext;
26 import com.liferay.portal.webdav.BaseResourceImpl;
27 import com.liferay.portal.webdav.BaseWebDAVStorageImpl;
28 import com.liferay.portal.webdav.Resource;
29 import com.liferay.portal.webdav.WebDAVException;
30 import com.liferay.portal.webdav.WebDAVRequest;
31 import com.liferay.portlet.journal.NoSuchStructureException;
32 import com.liferay.portlet.journal.NoSuchTemplateException;
33 import com.liferay.portlet.journal.model.JournalStructure;
34 import com.liferay.portlet.journal.model.JournalTemplate;
35 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
36 import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
37 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
38 import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
39
40 import java.io.File;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48
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 ServiceContext serviceContext = new ServiceContext();
188
189 Object model = resource.getModel();
190
191 if (model instanceof JournalStructure) {
192 JournalStructure structure = (JournalStructure)model;
193
194 HttpServletRequest request =
195 webDavRequest.getHttpServletRequest();
196
197 String xsd = StringUtil.read(request.getInputStream());
198
199 JournalStructureServiceUtil.updateStructure(
200 structure.getGroupId(), structure.getStructureId(),
201 structure.getParentStructureId(), structure.getName(),
202 structure.getDescription(), xsd, serviceContext);
203
204 return HttpServletResponse.SC_CREATED;
205 }
206 else if (model instanceof JournalTemplate) {
207 JournalTemplate template = (JournalTemplate)model;
208
209 HttpServletRequest request =
210 webDavRequest.getHttpServletRequest();
211
212 String xsl = StringUtil.read(request.getInputStream());
213 boolean formatXsl = true;
214 File smallFile = null;
215
216 JournalTemplateServiceUtil.updateTemplate(
217 template.getGroupId(), template.getTemplateId(),
218 template.getStructureId(), template.getName(),
219 template.getDescription(), xsl, formatXsl,
220 template.getLangType(), template.isCacheable(),
221 template.isSmallImage(), template.getSmallImageURL(),
222 smallFile, serviceContext);
223
224 return HttpServletResponse.SC_CREATED;
225 }
226 else {
227 return HttpServletResponse.SC_FORBIDDEN;
228 }
229 }
230 catch (PortalException pe) {
231 return HttpServletResponse.SC_FORBIDDEN;
232 }
233 catch (Exception e) {
234 throw new WebDAVException(e);
235 }
236 }
237
238 protected List<Resource> getFolders(WebDAVRequest webDavRequest)
239 throws Exception {
240
241 List<Resource> folders = new ArrayList<Resource>();
242
243 folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
245 folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
246
247 return folders;
248 }
249
250 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
251 throws Exception {
252
253 List<Resource> resources = new ArrayList<Resource>();
254
255 long groupId = webDavRequest.getGroupId();
256
257 List<JournalStructure> structures =
258 JournalStructureLocalServiceUtil.getStructures(groupId);
259
260 for (JournalStructure structure : structures) {
261 Resource resource = toResource(webDavRequest, structure, true);
262
263 resources.add(resource);
264 }
265
266 return resources;
267 }
268
269 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
270 throws Exception {
271
272 List<Resource> resources = new ArrayList<Resource>();
273
274 long groupId = webDavRequest.getGroupId();
275
276 List<JournalTemplate> templates =
277 JournalTemplateLocalServiceUtil.getTemplates(groupId);
278
279 for (JournalTemplate template : templates) {
280 Resource resource = toResource(webDavRequest, template, true);
281
282 resources.add(resource);
283 }
284
285 return resources;
286 }
287
288 protected Resource toResource(
289 WebDAVRequest webDavRequest, String type, boolean appendPath) {
290
291 String parentPath = getRootPath() + webDavRequest.getPath();
292 String name = StringPool.BLANK;
293
294 if (appendPath) {
295 name = type;
296 }
297
298 Resource resource = new BaseResourceImpl(parentPath, name, type);
299
300 resource.setModel(type);
301
302 return resource;
303 }
304
305 protected Resource toResource(
306 WebDAVRequest webDavRequest, JournalStructure structure,
307 boolean appendPath) {
308
309 String parentPath = getRootPath() + webDavRequest.getPath();
310 String name = StringPool.BLANK;
311
312 if (appendPath) {
313 name = structure.getStructureId();
314 }
315
316 return new JournalStructureResourceImpl(structure, parentPath, name);
317 }
318
319 protected Resource toResource(
320 WebDAVRequest webDavRequest, JournalTemplate template,
321 boolean appendPath) {
322
323 String parentPath = getRootPath() + webDavRequest.getPath();
324 String name = StringPool.BLANK;
325
326 if (appendPath) {
327 name = template.getTemplateId();
328 }
329
330 return new JournalTemplateResourceImpl(template, parentPath, name);
331 }
332
333
335 private static final String _TYPE_STRUCTURES = "Structures";
336
337 private static final String _TYPE_TEMPLATES = "Templates";
338
339 }