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.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  /**
51   * <a href="JournalWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
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.getName(), structure.getDescription(), xsd);
201 
202                 return HttpServletResponse.SC_CREATED;
203             }
204             else if (model instanceof JournalTemplate) {
205                 JournalTemplate template = (JournalTemplate)model;
206 
207                 HttpServletRequest request =
208                     webDavRequest.getHttpServletRequest();
209 
210                 String xsl = StringUtil.read(request.getInputStream());
211                 boolean formatXsl = true;
212                 File smallFile = null;
213 
214                 JournalTemplateServiceUtil.updateTemplate(
215                     template.getGroupId(), template.getTemplateId(),
216                     template.getStructureId(), template.getName(),
217                     template.getDescription(), xsl, formatXsl,
218                     template.getLangType(), template.isCacheable(),
219                     template.isSmallImage(), template.getSmallImageURL(),
220                     smallFile);
221 
222                 return HttpServletResponse.SC_CREATED;
223             }
224             else {
225                 return HttpServletResponse.SC_FORBIDDEN;
226             }
227         }
228         catch (PortalException pe) {
229             return HttpServletResponse.SC_FORBIDDEN;
230         }
231         catch (Exception e) {
232             throw new WebDAVException(e);
233         }
234     }
235 
236     protected List<Resource> getFolders(WebDAVRequest webDavRequest)
237         throws Exception {
238 
239         List<Resource> folders = new ArrayList<Resource>();
240 
241         //folders.add(toResource(webDavRequest, _TYPE_ARTICLES, true));
242         folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
243         folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
244 
245         return folders;
246     }
247 
248     protected List<Resource> getStructures(WebDAVRequest webDavRequest)
249         throws Exception {
250 
251         List<Resource> resources = new ArrayList<Resource>();
252 
253         long groupId = webDavRequest.getGroupId();
254 
255         List<JournalStructure> structures =
256             JournalStructureLocalServiceUtil.getStructures(groupId);
257 
258         for (JournalStructure structure : structures) {
259             Resource resource = toResource(webDavRequest, structure, true);
260 
261             resources.add(resource);
262         }
263 
264         return resources;
265     }
266 
267     protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
268         throws Exception {
269 
270         List<Resource> resources = new ArrayList<Resource>();
271 
272         long groupId = webDavRequest.getGroupId();
273 
274         List<JournalTemplate> templates =
275             JournalTemplateLocalServiceUtil.getTemplates(groupId);
276 
277         for (JournalTemplate template : templates) {
278             Resource resource = toResource(webDavRequest, template, true);
279 
280             resources.add(resource);
281         }
282 
283         return resources;
284     }
285 
286     protected Resource toResource(
287         WebDAVRequest webDavRequest, String type, boolean appendPath) {
288 
289         String parentPath = getRootPath() + webDavRequest.getPath();
290         String name = StringPool.BLANK;
291 
292         if (appendPath) {
293             name = type;
294         }
295 
296         Resource resource = new BaseResourceImpl(parentPath, name, type);
297 
298         resource.setModel(type);
299 
300         return resource;
301     }
302 
303     protected Resource toResource(
304         WebDAVRequest webDavRequest, JournalStructure structure,
305         boolean appendPath) {
306 
307         String parentPath = getRootPath() + webDavRequest.getPath();
308         String name = StringPool.BLANK;
309 
310         if (appendPath) {
311             name = structure.getStructureId();
312         }
313 
314         return new JournalStructureResourceImpl(structure, parentPath, name);
315     }
316 
317     protected Resource toResource(
318         WebDAVRequest webDavRequest, JournalTemplate template,
319         boolean appendPath) {
320 
321         String parentPath = getRootPath() + webDavRequest.getPath();
322         String name = StringPool.BLANK;
323 
324         if (appendPath) {
325             name = template.getTemplateId();
326         }
327 
328         return new JournalTemplateResourceImpl(template, parentPath, name);
329     }
330 
331     //private static final String _TYPE_ARTICLES = "Articles";
332 
333     private static final String _TYPE_STRUCTURES = "Structures";
334 
335     private static final String _TYPE_TEMPLATES = "Templates";
336 
337 }