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.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    
047                    String pathInfo = HttpUtil.fixPath(_request.getPathInfo(), false, true);
048    
049                    String strippedPathInfo = WebDAVUtil.stripManualCheckInRequiredPath(
050                            pathInfo);
051    
052                    if (strippedPathInfo.length() != pathInfo.length()) {
053                            pathInfo = strippedPathInfo;
054    
055                            _manualCheckInRequired = true;
056                    }
057    
058                    _path = WebDAVUtil.stripOfficeExtension(pathInfo);
059    
060                    _companyId = PortalUtil.getCompanyId(request);
061                    _groupId = WebDAVUtil.getGroupId(_companyId, _path);
062                    _userId = GetterUtil.getLong(_request.getRemoteUser());
063                    _permissionChecker = permissionChecker;
064            }
065    
066            @Override
067            public long getCompanyId() {
068                    return _companyId;
069            }
070    
071            @Override
072            public long getGroupId() {
073                    return _groupId;
074            }
075    
076            @Override
077            public HttpServletRequest getHttpServletRequest() {
078                    return _request;
079            }
080    
081            @Override
082            public HttpServletResponse getHttpServletResponse() {
083                    return _response;
084            }
085    
086            @Override
087            public String getLockUuid() {
088                    return _lockUuid;
089            }
090    
091            @Override
092            public String getPath() {
093                    return _path;
094            }
095    
096            @Override
097            public String[] getPathArray() {
098                    return WebDAVUtil.getPathArray(_path);
099            }
100    
101            @Override
102            public PermissionChecker getPermissionChecker() {
103                    return _permissionChecker;
104            }
105    
106            @Override
107            public String getRootPath() {
108                    return _storage.getRootPath();
109            }
110    
111            @Override
112            public long getUserId() {
113                    return _userId;
114            }
115    
116            @Override
117            public WebDAVStorage getWebDAVStorage() {
118                    return _storage;
119            }
120    
121            @Override
122            public boolean isAppleDoubleRequest() {
123                    String[] pathArray = getPathArray();
124    
125                    String name = WebDAVUtil.getResourceName(pathArray);
126    
127                    if (isMac() && name.startsWith(_APPLE_DOUBLE_PREFIX)) {
128                            return true;
129                    }
130                    else {
131                            return false;
132                    }
133            }
134    
135            @Override
136            public boolean isLitmus() {
137                    return _userAgent.contains("litmus");
138            }
139    
140            @Override
141            public boolean isMac() {
142                    return _userAgent.contains("WebDAVFS");
143            }
144    
145            @Override
146            public boolean isManualCheckInRequired() {
147                    return _manualCheckInRequired;
148            }
149    
150            @Override
151            public boolean isWindows() {
152                    return _userAgent.contains(
153                            "Microsoft Data Access Internet Publishing Provider");
154            }
155    
156            private static final String _APPLE_DOUBLE_PREFIX = "._";
157    
158            private long _companyId;
159            private long _groupId;
160            private String _lockUuid;
161            private boolean _manualCheckInRequired;
162            private String _path = StringPool.BLANK;
163            private PermissionChecker _permissionChecker;
164            private HttpServletRequest _request;
165            private HttpServletResponse _response;
166            private WebDAVStorage _storage;
167            private String _userAgent;
168            private long _userId;
169    
170    }