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