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