AnsweredAssumed Answered

Java-Backed web script

Question asked by sihnu on Apr 1, 2011
Latest reply on Apr 6, 2011 by openpj
Hello, I asked in another topic how to refer to a certain node and it seems to be impossible with js. But with java there is a method for it.

I've been now practicing implementation of Java-Backed web scripts.

I've got java class file:

name: SimpleWebScript.class
location: shared/classes/alfresco/lib/
content:

package org.alfresco.module.demoscripts;

import java.io.IOException;

import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.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");
       }
    }   
}

and description file:

name: simple.get.desc.xml
location: shared/classes/alfresco/extension/templates/webscripts/demo/simple/

and content:

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

And my definition is at file:

name: test-model-context.xml
location: shared/classes/alfresco/extension/

and the part of the content to define the web script:

<bean id="webscript.demo.simple.get" 
      class="alfresco.lib"
      parent="webscript">
    </bean>

The wiki tells of the definition bean ID:

Note that the bean id is made up of:

    * prefix = webscript (must always start with this value)
    * packageId = org.alfresco.sample
    * serviceId = helloworld
    * httpMethod = get

From this we can determine that the descriptor for this Web Script is called helloworld.get.desc.xml and is located in the directory org/alfresco/sample (either in the repository or in the classpath, as described in Deciding Where to place Web Script Implementation).

and the places are described:

These files need to be stored in a folder somewhere. They can live in either the Alfresco Repository or the Java ClassPath. The following folders are listed in the sequence in which Alfresco searches for Web Script implementation files:

   1. repository folder: /Company Home/Data Dictionary/Web Scripts Extensions
   2. repository folder: /Company Home/Data Dictionary/Web Scripts
   3. class path folder: /alfresco/extension/templates/webscripts
   4. class path folder: /alfresco/templates/webscripts

Anhow, my bean definition is causing error. Is the definition in a wrong file? Can't I define the bean in my own custom model? Or is there something wrong in the id or in the class definition of the bean? I really would like to get this working. Please help, I think I'm so close.

Thanks in advance.

Outcomes