View Javadoc

1   package net.sf.jguard.jsf.authentication.callbacks;
2   
3   import java.io.IOException;
4   import java.net.UnknownHostException;
5   import java.util.List;
6   
7   import java.util.Locale;
8   import javax.faces.context.ExternalContext;
9   import javax.faces.context.FacesContext;
10  import javax.portlet.PortletRequest;
11  import javax.security.auth.callback.Callback;
12  import javax.security.auth.callback.LanguageCallback;
13  import javax.security.auth.callback.UnsupportedCallbackException;
14  
15  import javax.servlet.http.HttpServletRequest;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  import net.sf.jguard.core.authentication.AccessContext;
21  import net.sf.jguard.core.authentication.bindings.AuthenticationBindings;
22  import net.sf.jguard.core.authentication.callbacks.InetAddressCallback;
23  import net.sf.jguard.core.authentication.schemes.AuthenticationSchemeHandler;
24  import net.sf.jguard.jee.authentication.callbacks.HttpServletCallbackHandler;
25  import net.sf.jguard.jsf.AccessListener;
26  
27  public class JSFCallbackHandler extends HttpServletCallbackHandler {
28  
29  	private static final Logger logger = LoggerFactory.getLogger(JSFCallbackHandler.class);
30  	
31  	public JSFCallbackHandler(AuthenticationBindings authBindings,AccessContext context,List<AuthenticationSchemeHandler> authenticationSchemeHandlers){
32          super(authBindings,context,authenticationSchemeHandlers);   
33      }
34  
35  	
36  	/**
37       * we fill callbacks not directly related to an AuthenticationSchemeHandler like
38       * {@link InetAddressCallback} and {@link LanguageCallback}.
39       * @param cbks
40       * @throws javax.security.auth.callback.UnsupportedCallbackException
41       */
42      @Override
43      protected void handleNonSchemeCallbacks(List<Callback> cbks) throws UnsupportedCallbackException {
44          FacesContext faceContext = (FacesContext)context.getAttribute(AccessListener.FACES_CONTEXT);
45          ExternalContext extContext = faceContext.getExternalContext();
46          Object request = extContext.getRequest();
47          HttpServletRequest httpRequest = null;
48          PortletRequest portletRequest = null;
49          if (HttpServletRequest.class.isAssignableFrom(request.getClass())){
50              httpRequest = (HttpServletRequest)request;
51          }else{
52              portletRequest = (PortletRequest)request;
53          }
54             
55          for (Callback cb : cbks) {
56              if (cb instanceof InetAddressCallback) {
57                  if(httpRequest==null){
58                      continue;
59                  }
60                  String remoteAddress = httpRequest.getRemoteAddr();
61                  String remoteHost = httpRequest.getRemoteHost();
62                  InetAddressCallback inetAddressCallback = (InetAddressCallback) cb;
63                  inetAddressCallback.setHostAdress(remoteAddress);
64  
65                  //the server is not configured to return the hostName.
66                  if (remoteAddress.equals(remoteHost)) {
67                      String resolvedHostName = remoteAddress;
68                      try {
69                          resolvedHostName = HttpServletCallbackHandler.reverseDns(remoteAddress);
70                      } catch (UnknownHostException uhe) {
71                          logger.warn(" host bound to address " + remoteAddress + "cannot be resolved", uhe);
72                          throw new UnsupportedCallbackException(cb, uhe.getMessage());
73                      } catch (IOException ex) {
74                          logger.equals(ex.getMessage());
75                          throw new UnsupportedCallbackException(cb, ex.getMessage());
76                      }
77                      inetAddressCallback.setHostName(resolvedHostName);
78                  }
79              } else if (cb instanceof LanguageCallback) {
80                  LanguageCallback languageCallback = (LanguageCallback) cb;
81                  Locale locale = extContext.getRequestLocale();
82                  languageCallback.setLocale(locale);
83              }
84          }
85      }
86  }