1package org.apache.archiva.redback.integration.filter.authentication.basic;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.redback.authentication.AuthenticationException;
23import org.apache.archiva.redback.integration.filter.authentication.AbstractHttpAuthenticationFilter;
24import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
2526import javax.servlet.FilterChain;
27import javax.servlet.FilterConfig;
28import javax.servlet.ServletException;
29import javax.servlet.ServletRequest;
30import javax.servlet.ServletResponse;
31import javax.servlet.http.HttpServletRequest;
32import javax.servlet.http.HttpServletResponse;
33import java.io.IOException;
3435/**36 * HttpBasicAuthenticationFilter37 *38 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>39 *40 */41publicclassHttpBasicAuthenticationFilter42extendsAbstractHttpAuthenticationFilter43 {
44privateHttpAuthenticator httpAuthentication;
4546 @Override
47publicvoid init( FilterConfig filterConfig )
48throws ServletException
49 {
50super.init( filterConfig );
5152 httpAuthentication = getApplicationContext().getBean( "httpAuthenticator#basic", HttpAuthenticator.class );
53 }
5455publicvoid doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
56throws IOException, ServletException
57 {
58if ( !( request instanceof HttpServletRequest ) )
59 {
60thrownew ServletException( "Can only process HttpServletRequest" );
61 }
6263if ( !( response instanceof HttpServletResponse ) )
64 {
65thrownew ServletException( "Can only process HttpServletResponse" );
66 }
6768 HttpServletRequest httpRequest = (HttpServletRequest) request;
69 HttpServletResponse httpResponse = (HttpServletResponse) response;
7071try72 {
73 httpAuthentication.authenticate( httpRequest, httpResponse );
74 }
75catch ( AuthenticationException e )
76 {
77HttpAuthenticator httpauthn = newHttpBasicAuthentication();
78 httpauthn.challenge( httpRequest, httpResponse, getRealmName(), e );
79return;
80 }
8182 chain.doFilter( request, response );
83 }
84 }