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