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