FilteredRequest.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  *
  14.  */

  15. package org.esigate.servlet.user;

  16. import java.security.Principal;

  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletRequestWrapper;

  19. /**
  20.  * Wrapper to the response used inside the servlet filter to override some of the methods of the request.
  21.  *
  22.  * @author Francois-Xavier Bonnet
  23.  */
  24. public class FilteredRequest extends HttpServletRequestWrapper {
  25.     public FilteredRequest(HttpServletRequest request) {
  26.         super(request);
  27.     }

  28.     /**
  29.      * Returns the user defined as parameter "user" if present.
  30.      */
  31.     @Override
  32.     public String getRemoteUser() {
  33.         String user = getHeader("X_REMOTE_USER");
  34.         if (user != null) {
  35.             return user;
  36.         } else {
  37.             return super.getRemoteUser();
  38.         }
  39.     }

  40.     @Override
  41.     public Principal getUserPrincipal() {
  42.         String user = getRemoteUser();
  43.         if (user != null) {
  44.             return new Principal() {
  45.                 @Override
  46.                 public String getName() {
  47.                     return getRemoteUser();
  48.                 }
  49.             };
  50.         } else {
  51.             return null;
  52.         }
  53.     }
  54. }