001    /**
002     * Copyright (c) 2000-2012 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.webdav.BaseResourceImpl;
020    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
021    import com.liferay.portal.kernel.webdav.Resource;
022    import com.liferay.portal.kernel.webdav.WebDAVException;
023    import com.liferay.portal.kernel.webdav.WebDAVRequest;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Raymond Augé
033     */
034    public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
035    
036            @Override
037            public int deleteResource(WebDAVRequest webDavRequest)
038                    throws WebDAVException {
039    
040                    try {
041                            Resource resource = getResource(webDavRequest);
042    
043                            if (resource == null) {
044                                    return HttpServletResponse.SC_NOT_FOUND;
045                            }
046    
047                            return HttpServletResponse.SC_FORBIDDEN;
048                    }
049                    catch (PortalException pe) {
050                            return HttpServletResponse.SC_FORBIDDEN;
051                    }
052                    catch (Exception e) {
053                            throw new WebDAVException(e);
054                    }
055            }
056    
057            public Resource getResource(WebDAVRequest webDavRequest)
058                    throws WebDAVException {
059    
060                    try {
061                            String[] pathArray = webDavRequest.getPathArray();
062    
063                            if (pathArray.length == 2) {
064                                    String path = getRootPath() + webDavRequest.getPath();
065    
066                                    return new BaseResourceImpl(path, StringPool.BLANK, getToken());
067                            }
068                            else if (pathArray.length == 3) {
069                                    String type = pathArray[2];
070    
071                                    return toResource(webDavRequest, type, false);
072                            }
073    
074                            return null;
075                    }
076                    catch (Exception e) {
077                            throw new WebDAVException(e);
078                    }
079            }
080    
081            public List<Resource> getResources(WebDAVRequest webDavRequest) {
082                    return Collections.emptyList();
083            }
084    
085            @Override
086            public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
087                    try {
088                            Resource resource = getResource(webDavRequest);
089    
090                            if (resource == null) {
091                                    return HttpServletResponse.SC_NOT_FOUND;
092                            }
093    
094                            return HttpServletResponse.SC_FORBIDDEN;
095                    }
096                    catch (PortalException pe) {
097                            return HttpServletResponse.SC_FORBIDDEN;
098                    }
099                    catch (Exception e) {
100                            throw new WebDAVException(e);
101                    }
102            }
103    
104            protected Resource toResource(
105                    WebDAVRequest webDavRequest, String type, boolean appendPath) {
106    
107                    String parentPath = getRootPath() + webDavRequest.getPath();
108    
109                    String name = StringPool.BLANK;
110    
111                    if (appendPath) {
112                            name = type;
113                    }
114    
115                    Resource resource = new BaseResourceImpl(parentPath, name, type);
116    
117                    resource.setModel(type);
118    
119                    return resource;
120            }
121    
122    }