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.portal.webdav;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.webdav.WebDAVException;
021    import com.liferay.portal.kernel.webdav.WebDAVRequest;
022    import com.liferay.portal.kernel.webdav.WebDAVStorage;
023    import com.liferay.portal.kernel.webdav.WebDAVUtil;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class WebDAVRequestImpl implements WebDAVRequest {
034    
035            public WebDAVRequestImpl(
036                            WebDAVStorage storage, HttpServletRequest request,
037                            HttpServletResponse response, String userAgent,
038                            PermissionChecker permissionChecker)
039                    throws WebDAVException {
040    
041                    _storage = storage;
042                    _request = request;
043                    _response = response;
044                    _userAgent = userAgent;
045                    _lockUuid = WebDAVUtil.getLockUuid(request);
046                    _path = HttpUtil.fixPath(_request.getPathInfo(), false, true);
047                    _companyId = PortalUtil.getCompanyId(request);
048                    _groupId = WebDAVUtil.getGroupId(_companyId, _path);
049                    _userId = GetterUtil.getLong(_request.getRemoteUser());
050                    _permissionChecker = permissionChecker;
051            }
052    
053            public long getCompanyId() {
054                    return _companyId;
055            }
056    
057            public long getGroupId() {
058                    return _groupId;
059            }
060    
061            public HttpServletRequest getHttpServletRequest() {
062                    return _request;
063            }
064    
065            public HttpServletResponse getHttpServletResponse() {
066                    return _response;
067            }
068    
069            public String getLockUuid() {
070                    return _lockUuid;
071            }
072    
073            public String getPath() {
074                    return _path;
075            }
076    
077            public String[] getPathArray() {
078                    return WebDAVUtil.getPathArray(_path);
079            }
080    
081            public PermissionChecker getPermissionChecker() {
082                    return _permissionChecker;
083            }
084    
085            public String getRootPath() {
086                    return _storage.getRootPath();
087            }
088    
089            public long getUserId() {
090                    return _userId;
091            }
092    
093            public WebDAVStorage getWebDAVStorage() {
094                    return _storage;
095            }
096    
097            public boolean isAppleDoubleRequest() {
098                    String[] pathArray = getPathArray();
099    
100                    String name = WebDAVUtil.getResourceName(pathArray);
101    
102                    if (isMac() && name.startsWith(_APPLE_DOUBLE_PREFIX)) {
103                            return true;
104                    }
105                    else {
106                            return false;
107                    }
108            }
109    
110            public boolean isLitmus() {
111                    return _userAgent.contains("litmus");
112            }
113    
114            public boolean isMac() {
115                    return _userAgent.contains("WebDAVFS");
116            }
117    
118            public boolean isWindows() {
119                    return _userAgent.contains(
120                            "Microsoft Data Access Internet Publishing Provider");
121            }
122    
123            private static final String _APPLE_DOUBLE_PREFIX = "._";
124    
125            private long _companyId;
126            private long _groupId;
127            private String _lockUuid;
128            private String _path = StringPool.BLANK;
129            private PermissionChecker _permissionChecker;
130            private HttpServletRequest _request;
131            private HttpServletResponse _response;
132            private WebDAVStorage _storage;
133            private String _userAgent;
134            private long _userId;
135    
136    }