001    /**
002     * Copyright (c) 2000-2013 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.portal.webdav;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
019    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
020    import com.liferay.portal.kernel.webdav.Resource;
021    import com.liferay.portal.kernel.webdav.WebDAVException;
022    import com.liferay.portal.kernel.webdav.WebDAVRequest;
023    import com.liferay.portal.kernel.webdav.WebDAVUtil;
024    import com.liferay.portal.model.Group;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class GroupWebDAVStorageImpl extends BaseWebDAVStorageImpl {
033    
034            @Override
035            public Resource getResource(WebDAVRequest webDAVRequest)
036                    throws WebDAVException {
037    
038                    verifyGroup(webDAVRequest);
039    
040                    String path = getRootPath() + webDAVRequest.getPath();
041    
042                    return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
043            }
044    
045            @Override
046            public List<Resource> getResources(WebDAVRequest webDAVRequest)
047                    throws WebDAVException {
048    
049                    verifyGroup(webDAVRequest);
050    
051                    List<Resource> resources = new ArrayList<Resource>();
052    
053                    String path = getRootPath() + webDAVRequest.getPath();
054    
055                    for (String token : WebDAVUtil.getStorageTokens()) {
056                            resources.add(new BaseResourceImpl(path, token, token));
057                    }
058    
059                    return resources;
060            }
061    
062            protected void verifyGroup(WebDAVRequest webDAVRequest)
063                    throws WebDAVException {
064    
065                    String path = webDAVRequest.getPath();
066    
067                    try {
068                            long userId = webDAVRequest.getUserId();
069    
070                            List<Group> groups = WebDAVUtil.getGroups(userId);
071    
072                            for (Group group : groups) {
073                                    if (path.equals(group.getFriendlyURL())) {
074                                            return;
075                                    }
076                            }
077                    }
078                    catch (Exception e) {
079                    }
080    
081                    throw new WebDAVException(
082                            "Invalid group for given credentials " +
083                                    webDAVRequest.getRootPath() + path);
084            }
085    
086    }