1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.Status;
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.Iterator;
47  import java.util.List;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * <a href="JournalWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
59  
60      public Status addCollection(WebDAVRequest webDavReq)
61          throws WebDAVException {
62  
63          return new Status(HttpServletResponse.SC_FORBIDDEN);
64      }
65  
66      public int copyCollectionResource(
67              WebDAVRequest webDavReq, Resource resource, String destination,
68              boolean overwrite, long depth)
69          throws WebDAVException {
70  
71          return HttpServletResponse.SC_FORBIDDEN;
72      }
73  
74      public int copySimpleResource(
75              WebDAVRequest webDavReq, Resource resource, String destination,
76              boolean overwrite)
77          throws WebDAVException {
78  
79          return HttpServletResponse.SC_FORBIDDEN;
80      }
81  
82      public int deleteResource(WebDAVRequest webDavReq) throws WebDAVException {
83          try {
84              Resource resource = getResource(webDavReq);
85  
86              if (resource == null) {
87                  return HttpServletResponse.SC_NOT_FOUND;
88              }
89  
90              Object model = resource.getModel();
91  
92              if (model instanceof JournalStructure) {
93                  JournalStructure structure = (JournalStructure)model;
94  
95                  JournalStructureServiceUtil.deleteStructure(
96                      structure.getGroupId(), structure.getStructureId());
97  
98                  return HttpServletResponse.SC_NO_CONTENT;
99              }
100             else if (model instanceof JournalTemplate) {
101                 JournalTemplate template = (JournalTemplate)model;
102 
103                 JournalTemplateServiceUtil.deleteTemplate(
104                     template.getGroupId(), template.getTemplateId());
105 
106                 return HttpServletResponse.SC_NO_CONTENT;
107             }
108             else {
109                 return HttpServletResponse.SC_FORBIDDEN;
110             }
111         }
112         catch (PortalException pe) {
113             return HttpServletResponse.SC_FORBIDDEN;
114         }
115         catch (Exception e) {
116             throw new WebDAVException(e);
117         }
118     }
119 
120     public Resource getResource(WebDAVRequest webDavReq)
121         throws WebDAVException {
122 
123         try {
124             String[] pathArray = webDavReq.getPathArray();
125 
126             if (pathArray.length == 3) {
127                 //String companyId = pathArray[0];
128                 //String groupId = pathArray[1];
129                 String type = pathArray[2];
130 
131                 return toResource(webDavReq, type, false);
132             }
133             else if (pathArray.length == 4) {
134                 //String companyId = pathArray[0];
135                 //String groupId = pathArray[1];
136                 String type = pathArray[2];
137                 String journalTypeId = pathArray[3];
138 
139                 if (type.equals(_TYPE_STRUCTURES)) {
140                     try {
141                         JournalStructure journalStructure =
142                             JournalStructureLocalServiceUtil.getStructure(
143                                 webDavReq.getGroupId(), journalTypeId);
144 
145                         return toResource(webDavReq, journalStructure, false);
146                     }
147                     catch (NoSuchStructureException nsse) {
148                         return null;
149                     }
150                 }
151                 else if (type.equals(_TYPE_TEMPLATES)) {
152                     try {
153                         JournalTemplate journalTemplate =
154                             JournalTemplateLocalServiceUtil.getTemplate(
155                                 webDavReq.getGroupId(), journalTypeId);
156 
157                         return toResource(webDavReq, journalTemplate, false);
158                     }
159                     catch (NoSuchTemplateException nste) {
160                         return null;
161                     }
162                 }
163             }
164 
165             return null;
166         }
167         catch (Exception e) {
168             throw new WebDAVException(e);
169         }
170     }
171 
172     public List getResources(WebDAVRequest webDavReq)
173         throws WebDAVException {
174 
175         try {
176             String[] pathArray = webDavReq.getPathArray();
177 
178             if (pathArray.length == 2) {
179                 return getFolders(webDavReq);
180             }
181             else if (pathArray.length == 3) {
182                 String type = pathArray[2];
183 
184                 if (type.equals(_TYPE_STRUCTURES)) {
185                     return getStructures(webDavReq);
186                 }
187                 else if (type.equals(_TYPE_TEMPLATES)) {
188                     return getTemplates(webDavReq);
189                 }
190             }
191 
192             return new ArrayList();
193         }
194         catch (Exception e) {
195             throw new WebDAVException(e);
196         }
197     }
198 
199     public int moveCollectionResource(
200             WebDAVRequest webDavReq, Resource resource, String destination,
201             boolean overwrite)
202         throws WebDAVException {
203 
204         return HttpServletResponse.SC_FORBIDDEN;
205     }
206 
207     public int moveSimpleResource(
208             WebDAVRequest webDavReq, Resource resource, String destination,
209             boolean overwrite)
210         throws WebDAVException {
211 
212         return HttpServletResponse.SC_FORBIDDEN;
213     }
214 
215     public int putResource(WebDAVRequest webDavReq) throws WebDAVException {
216         try {
217             Resource resource = getResource(webDavReq);
218 
219             if (resource == null) {
220                 return HttpServletResponse.SC_NOT_FOUND;
221             }
222 
223             Object model = resource.getModel();
224 
225             if (model instanceof JournalStructure) {
226                 JournalStructure structure = (JournalStructure)model;
227 
228                 HttpServletRequest req = webDavReq.getHttpServletRequest();
229 
230                 String xsd = StringUtil.read(req.getInputStream());
231 
232                 JournalStructureServiceUtil.updateStructure(
233                     structure.getGroupId(), structure.getStructureId(),
234                     structure.getName(), structure.getDescription(), xsd);
235 
236                 return HttpServletResponse.SC_CREATED;
237             }
238             else if (model instanceof JournalTemplate) {
239                 JournalTemplate template = (JournalTemplate)model;
240 
241                 HttpServletRequest req = webDavReq.getHttpServletRequest();
242 
243                 String xsl = StringUtil.read(req.getInputStream());
244                 boolean formatXsl = true;
245                 File smallFile = null;
246 
247                 JournalTemplateServiceUtil.updateTemplate(
248                     template.getGroupId(), template.getTemplateId(),
249                     template.getStructureId(), template.getName(),
250                     template.getDescription(), xsl, formatXsl,
251                     template.getLangType(), template.isCacheable(),
252                     template.isSmallImage(), template.getSmallImageURL(),
253                     smallFile);
254 
255                 return HttpServletResponse.SC_CREATED;
256             }
257             else {
258                 return HttpServletResponse.SC_FORBIDDEN;
259             }
260         }
261         catch (PortalException pe) {
262             return HttpServletResponse.SC_FORBIDDEN;
263         }
264         catch (Exception e) {
265             throw new WebDAVException(e);
266         }
267     }
268 
269     protected List getFolders(WebDAVRequest webDavReq) throws Exception {
270         List folders = new ArrayList();
271 
272         //folders.add(toResource(webDavReq, _TYPE_ARTICLES, true));
273         folders.add(toResource(webDavReq, _TYPE_STRUCTURES, true));
274         folders.add(toResource(webDavReq, _TYPE_TEMPLATES, true));
275 
276         return folders;
277     }
278 
279     protected List getStructures(WebDAVRequest webDavReq) throws Exception {
280         List templates = new ArrayList();
281 
282         Iterator itr = JournalStructureLocalServiceUtil.getStructures(
283             webDavReq.getGroupId()).iterator();
284 
285         while (itr.hasNext()) {
286             JournalStructure structure = (JournalStructure)itr.next();
287 
288             Resource resource = toResource(webDavReq, structure, true);
289 
290             templates.add(resource);
291         }
292 
293         return templates;
294     }
295 
296     protected List getTemplates(WebDAVRequest webDavReq) throws Exception {
297         List templates = new ArrayList();
298 
299         Iterator itr = JournalTemplateLocalServiceUtil.getTemplates(
300             webDavReq.getGroupId()).iterator();
301 
302         while (itr.hasNext()) {
303             JournalTemplate template = (JournalTemplate)itr.next();
304 
305             Resource resource = toResource(webDavReq, template, true);
306 
307             templates.add(resource);
308         }
309 
310         return templates;
311     }
312 
313     protected Resource toResource(
314         WebDAVRequest webDavReq, String type, boolean appendPath) {
315 
316         String parentPath = getRootPath() + webDavReq.getPath();
317         String name = StringPool.BLANK;
318 
319         if (appendPath) {
320             name = type;
321         }
322 
323         Resource resource = new BaseResourceImpl(parentPath, name, type);
324 
325         resource.setModel(type);
326 
327         return resource;
328     }
329 
330     protected Resource toResource(
331         WebDAVRequest webDavReq, JournalStructure structure,
332         boolean appendPath) {
333 
334         String parentPath = getRootPath() + webDavReq.getPath();
335         String name = StringPool.BLANK;
336 
337         if (appendPath) {
338             name = structure.getStructureId();
339         }
340 
341         return new JournalStructureResourceImpl(structure, parentPath, name);
342     }
343 
344     protected Resource toResource(
345         WebDAVRequest webDavReq, JournalTemplate template, boolean appendPath) {
346 
347         String parentPath = getRootPath() + webDavReq.getPath();
348         String name = StringPool.BLANK;
349 
350         if (appendPath) {
351             name = template.getTemplateId();
352         }
353 
354         return new JournalTemplateResourceImpl(template, parentPath, name);
355     }
356 
357     //private static final String _TYPE_ARTICLES = "Articles";
358 
359     private static final String _TYPE_STRUCTURES = "Structures";
360 
361     private static final String _TYPE_TEMPLATES = "Templates";
362 
363 }