001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public Resource getResource(WebDAVRequest webDavRequest)
035                    throws WebDAVException {
036    
037                    verifyGroup(webDavRequest);
038    
039                    String path = getRootPath() + webDavRequest.getPath();
040    
041                    return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
042            }
043    
044            public List<Resource> getResources(WebDAVRequest webDavRequest)
045                    throws WebDAVException {
046    
047                    verifyGroup(webDavRequest);
048    
049                    List<Resource> resources = new ArrayList<Resource>();
050    
051                    String path = getRootPath() + webDavRequest.getPath();
052    
053                    for (String token : WebDAVUtil.getStorageTokens()) {
054                            resources.add(new BaseResourceImpl(path, token, token));
055                    }
056    
057                    return resources;
058            }
059    
060            protected void verifyGroup(WebDAVRequest webDavRequest)
061                    throws WebDAVException {
062    
063                    String path = webDavRequest.getPath();
064    
065                    try {
066                            long userId = webDavRequest.getUserId();
067    
068                            List<Group> groups = WebDAVUtil.getGroups(userId);
069    
070                            for (Group group : groups) {
071                                    if (path.equals(group.getFriendlyURL())) {
072                                            return;
073                                    }
074                            }
075                    }
076                    catch (Exception e) {
077                    }
078    
079                    throw new WebDAVException(
080                            "Invalid group for given credentials " +
081                                    webDavRequest.getRootPath() + path);
082            }
083    
084    }