This project has retired. For details please refer to its Attic page.
HttpBasicAuthenticationFilter xref
View Javadoc

1   package org.apache.archiva.redback.integration.filter.authentication.basic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.authentication.AuthenticationException;
23  import org.apache.archiva.redback.integration.filter.authentication.AbstractHttpAuthenticationFilter;
24  import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
25  
26  import javax.servlet.FilterChain;
27  import javax.servlet.FilterConfig;
28  import javax.servlet.ServletException;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletResponse;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.io.IOException;
34  
35  /**
36   * HttpBasicAuthenticationFilter
37   *
38   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
39   *
40   */
41  public class HttpBasicAuthenticationFilter
42      extends AbstractHttpAuthenticationFilter
43  {
44      private HttpAuthenticator httpAuthentication;
45  
46      @Override
47      public void init( FilterConfig filterConfig )
48          throws ServletException
49      {
50          super.init( filterConfig );
51  
52          httpAuthentication = getApplicationContext().getBean( "httpAuthenticator#basic", HttpAuthenticator.class );
53      }
54  
55      public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
56          throws IOException, ServletException
57      {
58          if ( !( request instanceof HttpServletRequest ) )
59          {
60              throw new ServletException( "Can only process HttpServletRequest" );
61          }
62  
63          if ( !( response instanceof HttpServletResponse ) )
64          {
65              throw new ServletException( "Can only process HttpServletResponse" );
66          }
67  
68          HttpServletRequest httpRequest = (HttpServletRequest) request;
69          HttpServletResponse httpResponse = (HttpServletResponse) response;
70  
71          try
72          {
73              httpAuthentication.authenticate( httpRequest, httpResponse );
74          }
75          catch ( AuthenticationException e )
76          {
77              HttpAuthenticator httpauthn = new HttpBasicAuthentication();
78              httpauthn.challenge( httpRequest, httpResponse, getRealmName(), e );
79              return;
80          }
81  
82          chain.doFilter( request, response );
83      }
84  }