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.kernel.servlet;
016    
017    import java.security.Principal;
018    
019    import javax.servlet.http.HttpServletRequest;
020    import javax.servlet.http.HttpServletRequestWrapper;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class ProtectedServletRequest extends HttpServletRequestWrapper {
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                    _remoteUser = remoteUser;
039    
040                    if (remoteUser != null) {
041                            _userPrincipal = new ProtectedPrincipal(remoteUser);
042                    }
043    
044                    _authType = authType;
045            }
046    
047            @Override
048            public String getAuthType() {
049                    if (_authType == null) {
050                            return super.getAuthType();
051                    }
052    
053                    if (_authType.equals(HttpServletRequest.BASIC_AUTH)) {
054                            return HttpServletRequest.BASIC_AUTH;
055                    }
056                    else if (_authType.equals(HttpServletRequest.CLIENT_CERT_AUTH)) {
057                            return HttpServletRequest.CLIENT_CERT_AUTH;
058                    }
059                    else if (_authType.equals(HttpServletRequest.DIGEST_AUTH)) {
060                            return HttpServletRequest.DIGEST_AUTH;
061                    }
062                    else if (_authType.equals(HttpServletRequest.FORM_AUTH)) {
063                            return HttpServletRequest.FORM_AUTH;
064                    }
065    
066                    return _authType;
067            }
068    
069            @Override
070            public String getRemoteUser() {
071                    if (_remoteUser != null) {
072                            return _remoteUser;
073                    }
074                    else {
075                            return super.getRemoteUser();
076                    }
077            }
078    
079            @Override
080            public Principal getUserPrincipal() {
081                    if (_userPrincipal != null) {
082                            return _userPrincipal;
083                    }
084                    else {
085                            return super.getUserPrincipal();
086                    }
087            }
088    
089            private String _authType;
090            private String _remoteUser;
091            private Principal _userPrincipal;
092    
093    }