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