FileDocCategorySizeDatePackage
AuthenticationHandler.javaAPI DocApache Lucene 2.1.05568Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.servlet.handler

AuthenticationHandler

public class AuthenticationHandler extends Object implements GDataRequestHandler
author
Simon Willnauer

Fields Summary
private static final Log
LOG
private final org.apache.lucene.gdata.server.authentication.AuthenticationController
controller
private static final String
errorKey
private static final char
seperatory
private final org.apache.lucene.gdata.server.ServiceFactory
serviceFactory
private final org.apache.lucene.gdata.server.registry.GDataServerRegistry
registry
Constructors Summary
public AuthenticationHandler()

          
      
        this.registry = GDataServerRegistry.getRegistry();
        this.controller = this.registry.lookup(AuthenticationController.class, ComponentType.AUTHENTICATIONCONTROLLER);
        this.serviceFactory = this.registry.lookup(ServiceFactory.class, ComponentType.SERVICEFACTORY);
    
Methods Summary
private org.apache.lucene.gdata.data.GDataAccountgetAccount(java.lang.String accountName)

       
       AdminService service = this.serviceFactory.getAdminService();
       try{
       return service.getAccount(accountName);
       }finally{
            service.close();
       }
            
        
    
public voidprocessRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

see
org.apache.lucene.gdata.servlet.handler.GDataRequestHandler#processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

        
        try {
        String serviceName = request.getParameter(AuthenticationController.SERVICE_PARAMETER);
        if(LOG.isInfoEnabled()){
            String application = request.getParameter(AuthenticationController.APPLICATION_PARAMETER);
            LOG.info("Authentication request for service: "+serviceName+"; Application name: "+application);
        }
        
        if(!this.registry.isServiceRegistered(serviceName))
            throw new AuthenticationException("requested Service "+serviceName+"is not registered");
        String password = request.getParameter(AuthenticationController.PASSWORD_PARAMETER);
        String accountName = request.getParameter(AuthenticationController.ACCOUNT_PARAMETER);
        String clientIp = request.getRemoteHost();
        
       
        
        GDataAccount  account = getAccount(accountName);
        if(account == null || !account.getPassword().equals(password))
            throw new AuthenticationException("Account is null or password does not match");
        
        String token = this.controller.authenticatAccount(account,clientIp);
        sendToken(response,token);
        if(LOG.isInfoEnabled()){
            
            LOG.info("Account authenticated -- "+account);
        }
        } catch (AuthenticationException e){
            LOG.error("BadAuthentication -- "+e.getMessage(),e);
            sendError(response, GDataResponse.FORBIDDEN,"BadAuthentication");
        }catch (Exception e) {
            LOG.error("Unexpected Exception -- SERVERERROR -- "+e.getMessage(),e);
            sendError(response,GDataResponse.SERVER_ERROR, "Service not available");
        }
    
private voidsendError(javax.servlet.http.HttpServletResponse response, int code, java.lang.String message)

       Writer writer = response.getWriter();
       writer.write(errorKey);
       writer.write(seperatory);
       writer.write(message);
       response.sendError(code);
   
private voidsendToken(javax.servlet.http.HttpServletResponse response, java.lang.String token)

       Writer responseWriter = response.getWriter();
       Cookie cookie = new Cookie(AuthenticationController.TOKEN_KEY,token);
       response.addCookie(cookie);
       responseWriter.write(AuthenticationController.TOKEN_KEY);
       responseWriter.write(seperatory);
       responseWriter.write(token);
       responseWriter.close();