cancel
Showing results for 
Search instead for 
Did you mean: 

servlet auto-login

nyronian
Champ in-the-making
Champ in-the-making
I have created a new servlet (within alfresco) that is designed to "auto-login" the user under a specific name (not guest).  I have not figured out how to do this.  The problem I am trying to solve is there are nodes that I need to be able to access when the servlet is called that is NOT accessable by Guest.  So I am going to "auto-login" a user in order to access these nodes.  How do I do this?

Right now I have the following code that doesn't work: Smiley Happy

NOTE:  The servlet extends Alfresco's BaseServlet.


   protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
   {
      String ticket = login(request);
      
      logger.debug("login ticket = "+ticket);
      AuthenticationStatus status =  AuthenticationHelper.authenticate(getServletContext(), request, response, ticket);
      if (status == AuthenticationStatus.Failure)
      {
         return;
      }
      
      processRequest(request, response, false);

   }
   public String login(HttpServletRequest httpRequest)
   {
      String ticket = null;
      
        HttpSession session = httpRequest.getSession();
        ticket = (String)session.getAttribute(SESSION_USER_TICKET);
      
      FacesContext fc = FacesContext.getCurrentInstance();
      ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
      AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
      try
      {

         authenticationService.authenticate("admin", "admin".toCharArray());
         
         ticket = authenticationService.getCurrentTicket();
         session.setAttribute(SESSION_USER_TICKET, ticket);

      }
      catch (AuthenticationException aerr)
      {
         Utils.addErrorMessage(Application.getMessage(fc, MSG_ERROR_UNKNOWN_USER));
      }
      catch (InvalidNodeRefException refErr)
      {
         Utils.addErrorMessage(MessageFormat.format(Application.getMessage(fc,
               Repository.ERROR_NOHOME), refErr.getNodeRef().getId()));
      }

      return ticket;
   }

I get the following Exception when I try to access a Node:


java.lang.NullPointerException
        at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContex
t(FacesContextUtils.java:47)
        at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicati
onContext(FacesContextUtils.java:77)
        at org.alfresco.web.bean.repository.Repository.getServiceRegistry(Reposi
tory.java:410)
        at org.alfresco.web.bean.repository.Node.getServiceRegistry(Node.java:50
0)
        at org.alfresco.web.bean.repository.Node.<init>(Node.java:95)
        at com.tlhc.wc.servlet.PageProcessor.processTemplateRequest(PageProcesso
r.java:146)
        at com.tlhc.wc.servlet.PageProcessor.service(PageProcessor.java:99)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

If I log into alfresco and then access the servlet (under the same session) everything works perfectly Smiley Happy

Thanks in advance.
4 REPLIES 4

kevinr
Star Contributor
Star Contributor
When your servlet executes you are not able to use the FacesContext object - as the /faces application has not yet been hit. Servlets are executed first, so if you remove all references to the FacesContext and associated objects it will work. For error handling you will need to rely on standard servlet techniques i.e. throw an exception and it will be displayed by our standard error pages.

Thanks,

Kevin

nyronian
Champ in-the-making
Champ in-the-making
Thanks for the response Kevin.  I took out references to FacesContext….but the error occures when I execute:

Node myNode = new Node(myNodeRef);


This tells me the Node Contstructor is calling FacesContext and this is after I have completed a search on the repository, found a node I was interested in, got the nodeRef and then I try to instantiate it as a Node. 

Am I missing something here?  Is there a different way I should be instantiating the Node from the repository?  I know your da man because your name is all over the servlet code Smiley Happy

kevinr
Star Contributor
Star Contributor
I didn't see any code in your example accessing Nodes. Most of the web-client helper classes use the JSF framework and need it to be available - the Node class is one of them.

In servlets the JSF framework is not available, so you need to make direct repository API calls - mostly using the NodeService. If you take a look at the other servlet classes (yes mostly my fault Smiley Happy), such as DownloadContentServlet or TemplateContentServlet you will see how to access nodes and properties.

Thanks,

Kevin

nyronian
Champ in-the-making
Champ in-the-making
WAAAAHOOOOO!!!

It works perfectly now!  I was simply getting the Node to get the Path of the Node, which of course the nodeService will do.  I fixed that and now it works perfectly!!!

Thanks again for your help…when writing up the issue your not always sure what code is relevent and what is not….guess accessing the Node class was quite relevent!  Smiley Very Happy