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

openpj
Elite Collaborator
Elite Collaborator
Where do you have deployed all resources needed for your feature?
Can you provide more details?

amenel
Champ in-the-making
Champ in-the-making
Hi,
here is the list of the 4 things I deployed, base directory is "alfresco\tomcat'.
- wscript.get.desc.xml, in webapps\alfresco\WEB-INF\classes\alfresco\webscripts\extension\com\bluexml\xforms:

<webscript>
  <shortname>XForms for Alfresco 3.0</shortname>
  <description>BlueXML XForms data service for Alfresco 3.0</description>
  <url>/wscript</url>
  <format default="xml"/>any</format>
  <authentication>none</authentication>
  <transaction>none</transaction>
  <family>bluexml</family>
  <family>xforms</family>
</webscript>
- the template file wscript.get.xml.ftl, which contains the code below (I don't know whether "response" is a valid variable). I didn't try making it empty.

<?xml version="1.0" encoding="UTF-8"?>
<result>
   ${response}
</result>
- the jar file, in webapps\alfresco\WEB-INF\lib
- web-scripts-custom-context, in shared\classes\alfresco\extension:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
   <!– BlueXML XForms Data Controller –>
   <bean id="webscript.extension.com.bluexml.xforms.wscript.get"
            class="com.bluexml.xforms.controller.alfresco3x0.XFormsWebScript"
            parent="webscript">
         <property name="dictionaryService">
            <ref bean="dictionaryService" />
         </property>
   </bean>
</beans>

amenel
Champ in-the-making
Champ in-the-making
Well, I just succeeded running my webscript…
The frustrating thing is that I don't know why I had problems previously. It seems like the only thing I did was to change the format from <format default="xml">extension</format> to <format default="xml">any</format>.
Since the script runs as wanted, I deleted the freemarker file, which is no longer asked for by Alfresco.

loic
Champ in-the-making
Champ in-the-making
Hi,

I'm pretty new to Alfresco's WebScripts and I'm currentely trying to get the The World's Simplest Webscript working.

Here is my issue :

- The jar file is in webapps\alfresco\WEB-INF\lib

- I added these following lines to \webapps\alfresco\WEB-INF\classes\alfresco\web-scripts-application-context.xml


<!– SimpleWebScript –>
<bean id="webscript.org.alfresco.demo.simple.get" class="org.alfresco.module.demoscripts.SimpleWebScript" parent="webscript"><property name="dictionaryService" ref="DictionaryService"/>
</bean>

- I uploaded the simple.get.desc.xml file to the Company Home/Data Dictionnary/Web Scripts space.

- I also refreshed all the web scripts by clicking the button at the following adress http://localhost:8080/alfresco/service/index.

The problem is when I call the web script at the following address > http://localhost:8080/alfresco/service/demo/simple I have this message :

The Web Script /alfresco/service/demo/simple has responded with a status of 500 - Internal Error.

500 Description:   An error inside the HTTP server which prevented it from fulfilling the request.

Message:   Web Script format '' is not registered

…blah blah blah

If I explicitly ask for a format (http://localhost:8080/alfresco/service/demo/simple?format=json), the server responds me that I need a freemarker template…

How can I do ? Do I need a FreeMarker template (previous answers seems to say that i don't) ? If I really need one, how can I get the content filled in my java class (${var} ?  ${response} ? I tried these with no luck Smiley Sad)

Thanks.

openpj
Elite Collaborator
Elite Collaborator
If you want to implement a Java Backed Webscript with the execution of a JavaScript and/or a template FreeMarker for your presentation layout, you need to extend the following Java Helper class for your custom WebScript:
org.alfresco.web.scripts.DeclarativeWebScript
In this way WebScript Dispatcher will execute your custom webscripts files in the following order:
    Java class (extends DeclarativeWebScript)
    JavaScript (optional)
    FreeMarker Template (required)
And you must define your own custom FreeMarker template to create the corrected output format.

If you need only to execute an action and take control of the request you only need to extend the following Java Helper class:
org.alfresco.web.scripts.AbstractWebScript
To see more information you can see the WebScript wiki page.

Hope this helps.

juilee
Champ in-the-making
Champ in-the-making
Hello, I followed this tutorial to create a send email custom action using Java backed Webscript:
http://ecmstuff.blogspot.com/2012/04/adding-document-library-actions-in.html?showComment=14032798457...

As has been mentioned above, there is an AbstractWebScript class defined just to execute the action without using a freemaker template, but I get this error:
Cannot locate template processor for template sendDocInEmail.get.html

I have placed the services-context.xml file in the folder \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\module

Please help!

Thanks,
Juilee

rmfought
Champ in-the-making
Champ in-the-making
I think the point is that the example implements an AbstractWebScript, yet the engine is still looking for a Freemarker template.

I am having the same issues with the The World's Simplest WebScript.  When following the example, I get the following error:

org.alfresco.web.scripts.WebScriptException: 07200000 Failed to process template org/alfresco/simple/simple.get.html.ftl

Is the bean not properly registered? In my case, I'm using a bare-bones Surf instance (not Alfresco) and I don't have a web-scripts-application-context.xml file in \WEB-INF\classes\alfresco\ - I tried creating one to register the bean in, but am getting the same error.

rmfought
Champ in-the-making
Champ in-the-making
So I figured out that the bean wasn't even being recognized, because the web-scripts-application-context.xml file isn't being referenced in some other configuration file.  So I moved the bean declaration to webapps\surf\WEB-INF\classes\alfresco\webscript-framework-application-context.xml

<bean id="webscript.org.alfresco.java.simple.get" class="test.webscript.SimpleWebScript" parent="webscript" />

and now Surf recognizes the bean, but can't load the class:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [test.webscript.SimpleWebScript] for bean with name 'webscript.org.alfresco.java.simple.get' defined in class path resource [alfresco/webscript-framework-application-context.xml]; nested exception is java.lang.ClassNotFoundException: test.webscript.SimpleWebScript

I compiled the java into a class, and placed it in a jar file in webapps\surf\WEB-INF\lib

package test.webscript;

import java.io.IOException;

import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class SimpleWebScript extends AbstractWebScript
{
    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
       try
       {
          // build a json object
          JSONObject obj = new JSONObject();
          
          // put some data on it
          obj.put("field1", "data1");
          
          // build a JSON string and send it back
          String jsonString = obj.toString();
          res.getWriter().write(jsonString);
       }
       catch(JSONException e)
       {
          throw new WebScriptException("Unable to serialize JSON");
       }
    }   
}

Here is my webapps\surf\WEB-INF\classes\alfresco\webscripts\org\alfresco\java\simple.get.desc.xml :

<webscript>
  <shortname>The World's Simplest Webscript</shortname>
  <description>Hands back a little bit of JSON</description>
  <url>/java/simple</url>
  <authentication>none</authentication>
  <format default="">argument</format>
</webscript>

So why is the class not being found?

Thanks

amenel
Champ in-the-making
Champ in-the-making
It may be because the name of the jar file is not explicit ? Maybe that there's a naming convention. Have you tried naming the file with the Java package ? That's how I have named my webscript and it caused no problem.

Other thing: whatever file whose name ends with "-context.xml" is read by Alfresco when loading… provided the file is in WEB-INF/classes/alfresco or in shared/classes/alfresco/extension (EDIT: it seems like user files that are placed into WEB-INF/classes/alfresco are not loaded); there's no need for such a file to be referenced somewhere else cause it is already implicitly referenced, by convention. So I guess your initial file wasn't in the appropriate directory. I put mine in tomcat/shared/classes/alfresco/extension and no problem either.
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.