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
Take a look at the log file of Alfresco.

sihnu
Champ in-the-making
Champ in-the-making
Found multiple entries of this error:

SEVERE: Exception sending context initialized event to listener instance of class org.alfresco.web.app.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'LuceneFullTextSearchIndexer' defined in class path resource [alfresco/core-services-context.xml]: Cannot resolve reference to bean 'luceneFullTextSearchIndexer' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'luceneFullTextSearchIndexer' defined in class path resource [alfresco/core-services-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.alfresco.module.demoscripts.SimpleWebScript] for bean with name 'webscript.org.alfresco.demo.simple.get' defined in file [/home/<name>/alfresco

Darn… The class is not found… Again… I'm sure it did work yesterday… =/

openpj
Elite Collaborator
Elite Collaborator
No, I think that below in this log you will find the real cause of the issue, try to search in the log the "Caused by" clause  :wink:

It seems a problem of the Alfresco configuration…

sihnu
Champ in-the-making
Champ in-the-making
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.alfresco.module.demoscripts.SimpleWebScript] for bean with name 'webscript.org.alfresco.demo.simple.get' defined in file [/home/henril/alfresco-3.4.d/tomcat/webapps/alfresco/WEB-INF/classes/org/springframework/extensions/webscripts/web-scripts-application-context.xml]; nested exception is java.lang.ClassNotFoundException: org.alfresco.module.demoscripts.SimpleWebScript
   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1208)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:568)
   at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:302)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:381)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:375)
   at org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexerImpl.setBeanFactory(FullTextSearchIndexerImpl.java:296)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1422)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1389)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
   … 46 more
Caused by: java.lang.ClassNotFoundException: org.alfresco.module.demoscripts.SimpleWebScript
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
   at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
   at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1229)
   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1200)
   … 55 more

openpj
Elite Collaborator
Elite Collaborator
Why are you using org.alfresco package?
That package is reserved for Alfresco application.

sihnu
Champ in-the-making
Champ in-the-making
It was in the exmaple…  Smiley Indifferent

openpj
Elite Collaborator
Elite Collaborator
Ok, but I suggest you to not use the org.alfresco package.