cancel
Showing results for 
Search instead for 
Did you mean: 

Java backed webscript and Freemarker templates

amenel
Champ in-the-making
Champ in-the-making
Hi all,
I have spent some time trying to figure out how web scripts work in Alfresco 3.0 and also trying to set up a simple web script. For the first part, the information from the wiki pages got me started. For the second part, these same pages seem not enough to me.

My task is to write a web script that provides data access to the Alfresco repository for an application we are working on. For now, I am trying to write a script that returns a simple xml response: the names and values of the parameters it has been called with. I wrote the Java class that extends AbstractWebScript, and installed the jar file in webapps/alfresco/WEB-INF/lib. I also wrote the appropriate desc.xml file and added a bean in web-scripts-custom-context.xml referencing my script.

At first, the script was not found (class load exception). I had to change package names before it was loaded.

However, Alfresco returned an exception stating that my webscript had no Freemarker template… The wiki says that AbstractWebScript "allows the webscript to take full control over the request" whereas DeclarativeWebScript "allows a web script to mix Java, JavaScript and templates" (I understood "Freemarker templates"). My deduction was that AbstractWebScript is not constrained with the obligation of providing templates. Am I wrong ?

I provided a template anyway. But I here comes the problem: how can I include the streamed xml that my java class returns into the template so as to provide a full response ?
The FreeMarker Template Cookbook (http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook) didn't help.
The Java-backed examples page does not mention templates.
The section for response templates (http://wiki.alfresco.com/wiki/Web_Scripts#Creating_a_Response_Template) does not mention a possible inclusion of java returned results.

I haven't been able to find a reference that listed all variables available to the template under Alfresco. Does such a reference exist ?
14 REPLIES 14

rmfought
Champ in-the-making
Champ in-the-making
OK, it appears I have overcome the class hurdle, but now I am getting the following error from the Web Script Engine:

07210001 Web Script org/alfresco/java/simple.get already associated with the 'Presentation' container (classpath:alfresco/webscripts/org/alfresco/java/simple.get.desc.xml)

amenel
Champ in-the-making
Champ in-the-making
The jar file has to go into webapps/alfresco/WEB-INF/lib. Is it there ?
EDIT: I didn't see that you did put the file into webapps/surf/WEB-INF/lib. The only times I encountered the "can't load class" message were when I forgot to drop it into WEB-INF/lib. I really don't see what the problem can be.

rmfought
Champ in-the-making
Champ in-the-making
Thanks Amenel,

I'm sure it has something to do with the fact that I am using a bare Surf instance, and not a full-blown Alfresco install.  Maybe something wonky with the myriad of configuration files?

EDIT: Perhaps I should be using a Java Bean Component instead of a Java-backed Web Script?  Unfortunately, the documentation for this method is non-existent.

Rich

hyperation
Champ on-the-rise
Champ on-the-rise
Hi Everyone,

I am also having problem trying to get a simple java backed webscript running.

Here is what I did so far:
web-scripts-application-context.xml:
   
<bean id="webscript.test.simple.get" class="com.test.webscript.SimpleWebscript" parent="webscript" />
BTW, do I need some kind of <property> for the bean entry?

simple.get.json.ftl:

{
   "status" : "${status}",
   "x" : "${x}",
   "y" : "${y}"
}

simple.get.desc.xml:

<webscript>
  <shortname>Simple Webscript</shortname>
  <description>Returns JSON results</description>
  <url>/test/simple</url>
  <format default="json">extension</format>
  <authentication>none</authentication>
</webscript>

SimpleWebscript.java:

import java.util.HashMap;
import java.util.Map;
import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.Status;
import org.alfresco.web.scripts.WebScriptRequest;

public class SimpleWebscript extends DeclarativeWebScript {
   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest request, Status status) {

      String responseStatus = "200";
      String x = "xxx";
      String y = "yyy";
            
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("status", responseStatus);
      model.put("x", x);
      model.put("y", y);
      
      return model;
   }
}

Whenever I do a GET on the webscript I get the following error:

{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "Wrapped Exception (with status template): Error during processing of the template 'Expression x is undefined on line 3, column 20 in test\/simple.get.json.ftl.'. Please contact your system administrator.", 
  "exception" : "org.alfresco.web.scripts.WebScriptException - Wrapped Exception (with status template): Error during processing of the template 'Expression username is undefined on line 3, column 20 in test\/simple.get.json.ftl.'. Please contact your system administrator.",
….

It seems like that class isn't even being touched.  Any idea?

Thanks
Smiley Happy

hyperation
Champ on-the-rise
Champ on-the-rise
Okay…

I was kind of correct, the Java class wasn't being called when invoking webscript.  It seems like I placed the bean definition in the wrong file, I placed it in the none custom context.xml (web-scripts-application-context.xml).  Instead, I should have created the bean in my own custom context.xml and place it in the extension folder in Alfresco.

Thanks
Smiley Happy
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.