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