001    /**
002     * Copyright (c) 2000-2013 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.kernel.servlet;
016    
017    import java.security.Principal;
018    
019    import javax.servlet.http.HttpServletRequest;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class ProtectedServletRequest
025            extends PersistentHttpServletRequestWrapper {
026    
027            public ProtectedServletRequest(
028                    HttpServletRequest request, String remoteUser) {
029    
030                    this(request, remoteUser, null);
031            }
032    
033            public ProtectedServletRequest(
034                    HttpServletRequest request, String remoteUser, String authType) {
035    
036                    super(request);
037    
038                    if (request instanceof ProtectedServletRequest) {
039                            ProtectedServletRequest parentRequest =
040                                    (ProtectedServletRequest)request;
041    
042                            setRequest(parentRequest.getRequest());
043                    }
044    
045                    _remoteUser = remoteUser;
046    
047                    if (remoteUser != null) {
048                            _userPrincipal = new ProtectedPrincipal(remoteUser);
049                    }
050    
051                    _authType = authType;
052            }
053    
054            @Override
055            public String getAuthType() {
056                    if (_authType == null) {
057                            return super.getAuthType();
058                    }
059    
060                    if (_authType.equals(HttpServletRequest.BASIC_AUTH)) {
061                            return HttpServletRequest.BASIC_AUTH;
062                    }
063                    else if (_authType.equals(HttpServletRequest.CLIENT_CERT_AUTH)) {
064                            return HttpServletRequest.CLIENT_CERT_AUTH;
065                    }
066                    else if (_authType.equals(HttpServletRequest.DIGEST_AUTH)) {
067                            return HttpServletRequest.DIGEST_AUTH;
068                    }
069                    else if (_authType.equals(HttpServletRequest.FORM_AUTH)) {
070                            return HttpServletRequest.FORM_AUTH;
071                    }
072    
073                    return _authType;
074            }
075    
076            @Override
077            public String getRemoteUser() {
078                    if (_remoteUser != null) {
079                            return _remoteUser;
080                    }
081                    else {
082                            return super.getRemoteUser();
083                    }
084            }
085    
086            @Override
087            public Principal getUserPrincipal() {
088                    if (_userPrincipal != null) {
089                            return _userPrincipal;
090                    }
091                    else {
092                            return super.getUserPrincipal();
093                    }
094            }
095    
096            private final String _authType;
097            private String _remoteUser;
098            private Principal _userPrincipal;
099    
100    }