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