AnsweredAssumed Answered

DeclarativeWebScript not working

Question asked by kasperba on Jun 21, 2007
Latest reply on Feb 12, 2008 by robertito
I have written a REST-signin call based on the Alfresco 2.1dev REST-login but when I call it from a browser then I get an exception which says the following:

freemarker.core.InvalidReferenceException: Expression ticket is undefined on line 2, column 11 in com/wannakey/user_signin.get.xml.ftl.

After some hardwork I found out that the setAuthenticationService in the Signin-class is called when Alfresco starts, but the executeImpl is never triggered. Not even when I GET the REST-call.

Alfresco's REST-login works, mine doesn't. What did I miss?


I use the following url:

http://localhost:8080/alfresco/service/user/signin?email=admin&password=admin

Below are the files that are used and they are in an AMP.

signin.java
package com.wannakey.user;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;

import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptStatus;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.web.scripts.WebScriptException;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class SignIn extends DeclarativeWebScript {

   //    Logger
    private static final Log log = LogFactory.getLog(SignIn.class);

    // dependencies
    private AuthenticationService authenticationService;
   
    /**
     * @param authenticationService
     */
    public void setAuthenticationService(AuthenticationService authenticationService)
    {
       this.authenticationService = authenticationService;
    }
   
   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status) {
      
       if (authenticationService == null) {
         log.debug("Signin.executeImpl authenticationService is null");
      } else {
         log.debug("Signin.executeImpl authenticationService is NOT null");
      }
      
      log.debug("Signin.executeImpl called CurrentUser=" + authenticationService.getCurrentUserName());
      
      String email = req.getParameter("email");
      String password = req.getParameter("password");
      
      if ((email == null) || (email.length() == 0)) {
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "email not specified");
      }
      
      if ((password == null) || (password.length() == 0)) {
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Password not specified");
      }
      
      try
        {
            // get ticket
            authenticationService.authenticate(email, password.toCharArray());

            // add ticket to model for javascript and template access
            Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
            model.put("ticket",  authenticationService.getCurrentTicket());
            return model;
        }
        catch(AuthenticationException e)
        {
            throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Signin failed");
        }
        finally
        {
            authenticationService.clearCurrentSecurityContext();
        }
   }
}

signin.get.desc.xml
<webscript>
  <shortname>signin</shortname>
  <description>Sign in with your wannakey-account</description>
  <url>/user/signin?email={email}&amp;password={password}</url>
  <format default="xml"/>
  <authentication>none</authentication>
  <transaction>required</transaction>
</webscript>

signin.get.xml.ftl
<?xml version="1.0" encoding="UTF-8"?>
<ticket>${ticket}</ticket>

module-context.xml
<bean id="webscript.com.wannakey.signin.get"
             class="com.wannakey.user.SignIn" parent="webscript"
             abstract="false" singleton="true" lazy-init="default"
             autowire="default" dependency-check="default">
      <property name="authenticationService" ref="authenticationService" />
   </bean>

Outcomes