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