cancel
Showing results for 
Search instead for 
Did you mean: 

Java-Backed web script

sihnu
Champ in-the-making
Champ in-the-making
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.
16 REPLIES 16

openpj
Elite Collaborator
Elite Collaborator
Try to change your Spring bean definition in this way:

<bean id="webscript.demo.simple.simple.get"
      class="org.alfresco.module.demoscripts.SimpleWebScript"
      parent="webscript">
    </bean>
Hope this helps  :wink:

sihnu
Champ in-the-making
Champ in-the-making
It didn't help.. =( I'm Getting same "error":

An error has occured in the Share component: /share/service/components/dashlets/user-calendar.
It responded with a status of 500 - Internal Error.

I'm getting the same error… Why "simple" two times? I got only one folder called "simple" and why to change class to "org.alfresco.module.demoscripts.SimpleWebScript"? my class file is at: shared/classes/alfresco/lib/

Little explanation would be nice 😉

I read from the wiki:

Locations of the files

On Alfresco Share Standalone (3.3 or greater)

.class files in <Alfresco>/tomcat/webapps/alfresco/WEB-INF/classes/<class folder structure>

But should it now work at shared/classes/alfresco also? Isn't it same thing if I pleace my filese at webapps/alfreso/WEB-INF/classes/ or shared/classes/?

mrogers
Star Contributor
Star Contributor
a) I don't think it should have two "simple"
b) Never stick your stuff in WEB-INF
c) I don't think you need the "lib" in your path.   That does not match your package name.

sihnu
Champ in-the-making
Champ in-the-making
Thanks for the answers but still no luck… So I assume now that the correct id for the bean is: webscript.demo.simple.get and what comes to the class definition… I've tried now:

class="org.alfresco.module.demoscripts.SimpleWebScript"

and I placed the SimepleWebScript.class file to shared/classes/alfresco/ so that there is no lib folder anymore…

That didn't work. So I tried this:

class="alfresco.SimpleWebScript" as my class file is now at the root of the shared/classes/alfresco folder. That didn't work either… I also tried to move the class file to SimpleWebScript folder in alfresco folder as I thought the name in the class definition is actually refering to folder but that didn't work either…

I also tried by creating structure org/alfresco/module/demoscripts/SimpleWebScript/SimpleWebScript.class and then org/alfresco/module/demoscripts/SimpleWebScript.class with definition: org.alfresco.module.demoscripts.SimpleWebScrip but neither of these worked…

I don't know where to place that class file and what the defition should be…  :cry: I'm getting the same error all the time…

openpj
Elite Collaborator
Elite Collaborator
The class file can be in the classpath, this means that you can package all your Java classes in an unique JAR file and then you can put it in alfresco/WEB-INF/lib or in tomcat/shared/lib. This depends on the classloader that you want to use for these classes.

Hope this helps.

sihnu
Champ in-the-making
Champ in-the-making
I tried this:

I packed the .class file to simpleWebScript.jar and placed it at shared/classes/alfresco/lib. Then I defined the bean as:

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

I'm still getting the same error. Please help.

openpj
Elite Collaborator
Elite Collaborator
The descriptor file simple.get.desc.xml must be dropped in this folder:
/tomcat/shared/classes/alfresco/extension/templates/webscripts/demo
An I think that it should work now  :wink:

sihnu
Champ in-the-making
Champ in-the-making
I got it working. Thanks for the help a lot. Though, couldn't get straight answer it sill led me to conclusion. The webscript didn't work in shared/classes/… but it did work when I followed this from word to word:

.jar file in <tomcat>/webapps/share/WEB-INF/lib or .class files in <Alfresco>/tomcat/webapps/alfresco/WEB-INF/classes/<class folder structure>
web-scripts-application-context.xml - <tomcat>/webapps/share/WEB-INF/classes/org/springframework/extensions/webscripts
simple.get.desc.xml  - <tomcat>/webapps/share/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/demo

Gotta make some more experiments why I couldn't get it working in shared/classes/… class structure. Anyway, it works now… finally Smiley Happy

P.S. Is there a button to mark this topic as solved or do I have to edit the topic name?

sihnu
Champ in-the-making
Champ in-the-making
Weird… yesterday my web script was working just fine but today I get this error all the time:

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

Message:   java.lang.NullPointerException

It doesn't matter if I change the java code. I've tried also a code that does nothing. It always gives the same error. Could anyone help?