001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.webdav;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
021    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
022    import com.liferay.portal.kernel.webdav.Resource;
023    import com.liferay.portal.kernel.webdav.WebDAVException;
024    import com.liferay.portal.kernel.webdav.WebDAVRequest;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.journal.NoSuchStructureException;
027    import com.liferay.portlet.journal.NoSuchTemplateException;
028    import com.liferay.portlet.journal.model.JournalStructure;
029    import com.liferay.portlet.journal.model.JournalTemplate;
030    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
031    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
032    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
033    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
034    
035    import java.io.File;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Raymond Augé
046     */
047    public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
048    
049            public int deleteResource(WebDAVRequest webDavRequest)
050                    throws WebDAVException {
051    
052                    try {
053                            Resource resource = getResource(webDavRequest);
054    
055                            if (resource == null) {
056                                    return HttpServletResponse.SC_NOT_FOUND;
057                            }
058    
059                            Object model = resource.getModel();
060    
061                            if (model instanceof JournalStructure) {
062                                    JournalStructure structure = (JournalStructure)model;
063    
064                                    JournalStructureServiceUtil.deleteStructure(
065                                            structure.getGroupId(), structure.getStructureId());
066    
067                                    return HttpServletResponse.SC_NO_CONTENT;
068                            }
069                            else if (model instanceof JournalTemplate) {
070                                    JournalTemplate template = (JournalTemplate)model;
071    
072                                    JournalTemplateServiceUtil.deleteTemplate(
073                                            template.getGroupId(), template.getTemplateId());
074    
075                                    return HttpServletResponse.SC_NO_CONTENT;
076                            }
077                            else {
078                                    return HttpServletResponse.SC_FORBIDDEN;
079                            }
080                    }
081                    catch (PortalException pe) {
082                            return HttpServletResponse.SC_FORBIDDEN;
083                    }
084                    catch (Exception e) {
085                            throw new WebDAVException(e);
086                    }
087            }
088    
089            public Resource getResource(WebDAVRequest webDavRequest)
090                    throws WebDAVException {
091    
092                    try {
093                            String[] pathArray = webDavRequest.getPathArray();
094    
095                            if (pathArray.length == 2) {
096                                    String path = getRootPath() + webDavRequest.getPath();
097    
098                                    return new BaseResourceImpl(path, StringPool.BLANK, getToken());
099                            }
100                            else if (pathArray.length == 3) {
101                                    String type = pathArray[2];
102    
103                                    return toResource(webDavRequest, type, false);
104                            }
105                            else if (pathArray.length == 4) {
106                                    String type = pathArray[2];
107                                    String journalTypeId = pathArray[3];
108    
109                                    if (type.equals(_TYPE_STRUCTURES)) {
110                                            try {
111                                                    JournalStructure journalStructure =
112                                                            JournalStructureLocalServiceUtil.getStructure(
113                                                                    webDavRequest.getGroupId(), journalTypeId);
114    
115                                                    return toResource(
116                                                            webDavRequest, journalStructure, false);
117                                            }
118                                            catch (NoSuchStructureException nsse) {
119                                                    return null;
120                                            }
121                                    }
122                                    else if (type.equals(_TYPE_TEMPLATES)) {
123                                            try {
124                                                    JournalTemplate journalTemplate =
125                                                            JournalTemplateLocalServiceUtil.getTemplate(
126                                                                    webDavRequest.getGroupId(), journalTypeId);
127    
128                                                    return toResource(
129                                                            webDavRequest, journalTemplate, false);
130                                            }
131                                            catch (NoSuchTemplateException nste) {
132                                                    return null;
133                                            }
134                                    }
135                            }
136    
137                            return null;
138                    }
139                    catch (Exception e) {
140                            throw new WebDAVException(e);
141                    }
142            }
143    
144            public List<Resource> getResources(WebDAVRequest webDavRequest)
145                    throws WebDAVException {
146    
147                    try {
148                            String[] pathArray = webDavRequest.getPathArray();
149    
150                            if (pathArray.length == 2) {
151                                    return getFolders(webDavRequest);
152                            }
153                            else if (pathArray.length == 3) {
154                                    String type = pathArray[2];
155    
156                                    if (type.equals(_TYPE_STRUCTURES)) {
157                                            return getStructures(webDavRequest);
158                                    }
159                                    else if (type.equals(_TYPE_TEMPLATES)) {
160                                            return getTemplates(webDavRequest);
161                                    }
162                            }
163    
164                            return new ArrayList<Resource>();
165                    }
166                    catch (Exception e) {
167                            throw new WebDAVException(e);
168                    }
169            }
170    
171            public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
172                    try {
173                            Resource resource = getResource(webDavRequest);
174    
175                            if (resource == null) {
176                                    return HttpServletResponse.SC_NOT_FOUND;
177                            }
178    
179                            ServiceContext serviceContext = new ServiceContext();
180    
181                            Object model = resource.getModel();
182    
183                            if (model instanceof JournalStructure) {
184                                    JournalStructure structure = (JournalStructure)model;
185    
186                                    HttpServletRequest request =
187                                            webDavRequest.getHttpServletRequest();
188    
189                                    String xsd = StringUtil.read(request.getInputStream());
190    
191                                    JournalStructureServiceUtil.updateStructure(
192                                            structure.getGroupId(), structure.getStructureId(),
193                                            structure.getParentStructureId(), structure.getName(),
194                                            structure.getDescription(), xsd, serviceContext);
195    
196                                    return HttpServletResponse.SC_CREATED;
197                            }
198                            else if (model instanceof JournalTemplate) {
199                                    JournalTemplate template = (JournalTemplate)model;
200    
201                                    HttpServletRequest request =
202                                            webDavRequest.getHttpServletRequest();
203    
204                                    String xsl = StringUtil.read(request.getInputStream());
205                                    boolean formatXsl = true;
206                                    File smallFile = null;
207    
208                                    JournalTemplateServiceUtil.updateTemplate(
209                                            template.getGroupId(), template.getTemplateId(),
210                                            template.getStructureId(), template.getName(),
211                                            template.getDescription(), xsl, formatXsl,
212                                            template.getLangType(), template.isCacheable(),
213                                            template.isSmallImage(), template.getSmallImageURL(),
214                                            smallFile, serviceContext);
215    
216                                    return HttpServletResponse.SC_CREATED;
217                            }
218                            else {
219                                    return HttpServletResponse.SC_FORBIDDEN;
220                            }
221                    }
222                    catch (PortalException pe) {
223                            return HttpServletResponse.SC_FORBIDDEN;
224                    }
225                    catch (Exception e) {
226                            throw new WebDAVException(e);
227                    }
228            }
229    
230            protected List<Resource> getFolders(WebDAVRequest webDavRequest)
231                    throws Exception {
232    
233                    List<Resource> folders = new ArrayList<Resource>();
234    
235                    //folders.add(toResource(webDavRequest, _TYPE_ARTICLES, true));
236                    folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
237                    folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
238    
239                    return folders;
240            }
241    
242            protected List<Resource> getStructures(WebDAVRequest webDavRequest)
243                    throws Exception {
244    
245                    List<Resource> resources = new ArrayList<Resource>();
246    
247                    long groupId = webDavRequest.getGroupId();
248    
249                    List<JournalStructure> structures =
250                            JournalStructureLocalServiceUtil.getStructures(groupId);
251    
252                    for (JournalStructure structure : structures) {
253                            Resource resource = toResource(webDavRequest, structure, true);
254    
255                            resources.add(resource);
256                    }
257    
258                    return resources;
259            }
260    
261            protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
262                    throws Exception {
263    
264                    List<Resource> resources = new ArrayList<Resource>();
265    
266                    long groupId = webDavRequest.getGroupId();
267    
268                    List<JournalTemplate> templates =
269                            JournalTemplateLocalServiceUtil.getTemplates(groupId);
270    
271                    for (JournalTemplate template : templates) {
272                            Resource resource = toResource(webDavRequest, template, true);
273    
274                            resources.add(resource);
275                    }
276    
277                    return resources;
278            }
279    
280            protected Resource toResource(
281                    WebDAVRequest webDavRequest, String type, boolean appendPath) {
282    
283                    String parentPath = getRootPath() + webDavRequest.getPath();
284                    String name = StringPool.BLANK;
285    
286                    if (appendPath) {
287                            name = type;
288                    }
289    
290                    Resource resource = new BaseResourceImpl(parentPath, name, type);
291    
292                    resource.setModel(type);
293    
294                    return resource;
295            }
296    
297            protected Resource toResource(
298                    WebDAVRequest webDavRequest, JournalStructure structure,
299                    boolean appendPath) {
300    
301                    String parentPath = getRootPath() + webDavRequest.getPath();
302                    String name = StringPool.BLANK;
303    
304                    if (appendPath) {
305                            name = structure.getStructureId();
306                    }
307    
308                    return new JournalStructureResourceImpl(structure, parentPath, name);
309            }
310    
311            protected Resource toResource(
312                    WebDAVRequest webDavRequest, JournalTemplate template,
313                    boolean appendPath) {
314    
315                    String parentPath = getRootPath() + webDavRequest.getPath();
316                    String name = StringPool.BLANK;
317    
318                    if (appendPath) {
319                            name = template.getTemplateId();
320                    }
321    
322                    return new JournalTemplateResourceImpl(template, parentPath, name);
323            }
324    
325            //private static final String _TYPE_ARTICLES = "Articles";
326    
327            private static final String _TYPE_STRUCTURES = "Structures";
328    
329            private static final String _TYPE_TEMPLATES = "Templates";
330    
331    }