View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.springframework.security.ui;
17  
18  import org.springframework.util.Assert;
19  import org.springframework.util.ReflectionUtils;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  
27  /**
28   * Implementation of {@link AuthenticationDetailsSource} which builds the details object from
29   * an <tt>HttpServletRequest</tt> object.
30   * <p>
31   * By default will create an instance of <code>WebAuthenticationDetails</code>. Any object that accepts a 
32   * <code>HttpServletRequest</code> as its sole constructor can be used instead of this default.
33   *
34   * @author Ben Alex
35   * @version $Id: WebAuthenticationDetailsSource.java 2727 2008-03-13 14:42:38Z luke_t $
36   */
37  public class WebAuthenticationDetailsSource implements AuthenticationDetailsSource {
38      //~ Instance fields ================================================================================================
39  
40      private Class clazz = WebAuthenticationDetails.class;
41  
42      //~ Methods ========================================================================================================
43  
44      /**
45       * @param context the <tt>HttpServletRequest</tt> object.
46       */
47      public Object buildDetails(Object context) {
48          Assert.isInstanceOf(HttpServletRequest.class, context);
49          try {
50              Constructor constructor = clazz.getConstructor(new Class[] {HttpServletRequest.class});
51  
52              return constructor.newInstance(new Object[] {context});
53          } catch (NoSuchMethodException ex) {
54              ReflectionUtils.handleReflectionException(ex);
55          } catch (InvocationTargetException ex) {
56              ReflectionUtils.handleReflectionException(ex);
57          } catch (InstantiationException ex) {
58              ReflectionUtils.handleReflectionException(ex);
59          } catch (IllegalAccessException ex) {
60              ReflectionUtils.handleReflectionException(ex);
61          }
62  
63          return null;
64      }
65  
66      public void setClazz(Class clazz) {
67          Assert.notNull(clazz, "Class required");
68          this.clazz = clazz;
69      }
70  }