cancel
Showing results for 
Search instead for 
Did you mean: 

Java Backed Webscript

samuel_penn
Champ in-the-making
Champ in-the-making
I'm trying to write a Java backed web script, and I'm running into issues trying to get the Java code called. As far as I can tell, the Java class is being linked to the webscript, and the class is being initialised, but the class isn't called when the webscript is invoked. This is all in Alfresco 2.2.1, running on MS Windows.

In Data Dictionary/Web Scripts/webtools:

unlock.get.html.ftl

<h1>${title!"No Title"}</h1>

unlock.get.desc.xml

<webscript>
  <shortname>Unlock Website</shortname>
  <description>Unlocks all files in website.</description>
  <url>/webtools/unlock/{path}</url>
  <authentication>guest</authentication>
</webscript>

In tomcat\shared\classes\alfresco\extension I have placed a centrom-scripts-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
    <bean id="webscript.webtools.unlock.get" class="com.centrom.webscripts.Unlock" parent="webscript">
    </bean>
</beans>

And in tomcat\webapps\alfresco\WEB-INF\classes\com\centrom\webscripts I haved placed Unlock.class, the source to which is:


package com.centrom.webscripts;

import java.io.*;
import java.util.*;
import org.alfresco.web.scripts.*;
import org.apache.log4j.Logger;

public class Unlock extends DeclarativeWebScript {
   static Logger logger = Logger.getLogger(Unlock.class);
   static {
      if (logger != null) {
         logger.error("Initiating webscript");
      }
   }
    protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptResponse res) throws IOException {
       logger.error("Called executeImpl web script Unlock");
       Map<String,Object>   model = new HashMap<String, Object>();
       
       model.put("title", "Hello World");
       model.put("count", 1);
       model.put("response", "Success");

       return model;
    }
}

When the server starts up, I can see the debug for the static block in the class being called, so the class is being loaded. After refreshing the webscripts, I can pull back information on this web script which suggests that it has bound to the Java class.

http://localhost/alfresco/wcservice/script/webtools/unlock.get:

Web Script: webtools/unlock.get
Alfresco Enterprise Network v2.2.1 (233)
Generated from /alfresco/wcservice/script/webtools/unlock.get on 20-Feb-2009 14:38:43

Script Properties
Id:   webtools/unlock.get
Short Name:   Unlock Website
Description:   Unlocks all files in website.
Authentication:   guest
Transaction:   required
Method:   GET
URL Template:   /webtools/unlock/{path}
Format Style:   any
Default Format:   html
Implementation:   class com.centrom.webscripts.Unlock

Store: workspace://SpacesStore/app:company_home/app:dictionary/cm:extensionwebscripts

[No implementation files]

Store: workspace://SpacesStore/app:company_home/app:dictionary/cm:webscripts

File: webtools/unlock.get.desc.xml

<webscript>
  <shortname>Unlock Website</shortname>
  <description>Unlocks all files in website.</description>
  <url>/webtools/unlock/{path}</url>
  <authentication>guest</authentication>
</webscript>

File: webtools/unlock.get.html.ftl

<h1>${title!"No Title"}</h1>

Store: classpath:/alfresco/templates/webscripts

[No implementation files]

The Implementation:   class com.centrom.webscripts.Unlock line to me suggests that the class has been picked up and linked to the webscript correctly.

However, when the webscript is called, I get "No Title" as the response, so the model variable 'title' hasn't been set. I also see no debug in the logs, which suggests that the class isn't actually being called. My understanding is that the variables title, count and response should be set and available to the freemarker template.

I've gone through the information on the wiki, and in the Alfresco Developers Guide book several times, but haven't been able to figure out what it is I'm missing. I assume it's something simple, but I'm stumped.

Any ideas greatly appreciated,
thanks.

Sam.
4 REPLIES 4

openpj
Elite Collaborator
Elite Collaborator
Maybe there is an error on the path of the package where you are deploying your webscript class.
I think you must deploy all the webscript classes starting from this path for your package:

alfresco.war/WEB-INF/classes/alfresco/templates/webscripts/com/centrom/webscripts/

In this package you can store your webscript class  :wink:

Hope this helps.

samuel_penn
Champ in-the-making
Champ in-the-making
I get a class not found exception when Alfresco starts if I move the class to there. Back in its old location, it is definitely being picked up by the bean configuration, since I don't get any errors plus the debug from the static {} block is generated.

Sam.

openpj
Elite Collaborator
Elite Collaborator
Have you tried to move all your webscript descriptor files (xml, ftl) in the Web Script Extensions Alfresco space?

samuel_penn
Champ in-the-making
Champ in-the-making
I've tried variations on placing the xml and ftl files in the standard Web Script space, the Web Script Extensions and also on the file system in the templates/webscripts area. In all cases, I get the same result as at the start of the thread - the web script runs but the variables set in the class aren't exported to the template, and I don't see the debug I'd expect if the class was actually being called.

It must be some stupid little thing that I've missed out, but so far it eludes me.

I've managed to track down the new location of the samples from the wiki page[1], and I'm currently trying to build them (jar dependency hell). If I can get those working, then possibly I can just try modifying these (or figure out what the difference is).

Sam.

[1] http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples