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    
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            public long getCompanyId() {
067                    return _companyId;
068            }
069    
070            public long getGroupId() {
071                    return _groupId;
072            }
073    
074            public HttpServletRequest getHttpServletRequest() {
075                    return _request;
076            }
077    
078            public HttpServletResponse getHttpServletResponse() {
079                    return _response;
080            }
081    
082            public String getLockUuid() {
083                    return _lockUuid;
084            }
085    
086            public String getPath() {
087                    return _path;
088            }
089    
090            public String[] getPathArray() {
091                    return WebDAVUtil.getPathArray(_path);
092            }
093    
094            public PermissionChecker getPermissionChecker() {
095                    return _permissionChecker;
096            }
097    
098            public String getRootPath() {
099                    return _storage.getRootPath();
100            }
101    
102            public long getUserId() {
103                    return _userId;
104            }
105    
106            public WebDAVStorage getWebDAVStorage() {
107                    return _storage;
108            }
109    
110            public boolean isAppleDoubleRequest() {
111                    String[] pathArray = getPathArray();
112    
113                    String name = WebDAVUtil.getResourceName(pathArray);
114    
115                    if (isMac() && name.startsWith(_APPLE_DOUBLE_PREFIX)) {
116                            return true;
117                    }
118                    else {
119                            return false;
120                    }
121            }
122    
123            public boolean isLitmus() {
124                    return _userAgent.contains("litmus");
125            }
126    
127            public boolean isMac() {
128                    return _userAgent.contains("WebDAVFS");
129            }
130    
131            public boolean isManualCheckInRequired() {
132                    return _manualCheckInRequired;
133            }
134    
135            public boolean isWindows() {
136                    return _userAgent.contains(
137                            "Microsoft Data Access Internet Publishing Provider");
138            }
139    
140            private static final String _APPLE_DOUBLE_PREFIX = "._";
141    
142            private long _companyId;
143            private long _groupId;
144            private String _lockUuid;
145            private boolean _manualCheckInRequired;
146            private String _path = StringPool.BLANK;
147            private PermissionChecker _permissionChecker;
148            private HttpServletRequest _request;
149            private HttpServletResponse _response;
150            private WebDAVStorage _storage;
151            private String _userAgent;
152            private long _userId;
153    
154    }