View Javadoc
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  
16  package org.esigate.servlet.user;
17  
18  import java.security.Principal;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletRequestWrapper;
22  
23  /**
24   * Wrapper to the response used inside the servlet filter to override some of the methods of the request.
25   * 
26   * @author Francois-Xavier Bonnet
27   */
28  public class FilteredRequest extends HttpServletRequestWrapper {
29      public FilteredRequest(HttpServletRequest request) {
30          super(request);
31      }
32  
33      /**
34       * Returns the user defined as parameter "user" if present.
35       */
36      @Override
37      public String getRemoteUser() {
38          String user = getHeader("X_REMOTE_USER");
39          if (user != null) {
40              return user;
41          } else {
42              return super.getRemoteUser();
43          }
44      }
45  
46      @Override
47      public Principal getUserPrincipal() {
48          String user = getRemoteUser();
49          if (user != null) {
50              return new Principal() {
51                  @Override
52                  public String getName() {
53                      return getRemoteUser();
54                  }
55              };
56          } else {
57              return null;
58          }
59      }
60  }