cancel
Showing results for 
Search instead for 
Did you mean: 

Create Java back webscript in share extesion project

djaweid_
Champ in-the-making
Champ in-the-making
Hi All,

I am new with alfresco. I have created a share extension where I want to create a Java backed webscript.

I used https://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples to create Java backed webscript.


Add the following bean in custom-slingshot-application-context.xml

<bean id="webframework.store.webscripts.custom" class="org.alfresco.web.scripts.ClassPathStore">

      <property name="mustExist"><value>true</value></property>

      <property name="classPath"><value>config/alfresco/web-extension/site-webscripts</value></property>

</bean>


http://127.0.0.1:8081/share/service/com/retreive-data/simple


I get the following error:

retreive-data-share/target/amp-war/WEB-INF/lib/retreive-data-share-1.0-SNAPSHOT.jar!/alfresco/web-extension/custom-slingshot-application-context.xml]; nested exception is java.lang.ClassNotFoundException: org.alfresco.web.scripts.ClassPathStore

Caused by: java.lang.ClassNotFoundException: org.alfresco.web.scripts.ClassPathStore

/src/main/amp/config/alfresco/web-extension/site-webscripts/com/retreive-data/simple.get.desc.xml

<webscript>
  <shortname>The World's Simplest Webscript</shortname>
  <description>Hands back a little bit of JSON</description>
  <url>/com/tenthline/retreive-data/simple</url>
  <authentication>none</authentication>
  <format default="">argument</format>
  <family>Alfresco Java-Backed WebScripts Demo</family>
</webscript>


/src/main/amp/config/alfresco/web-extension/site-webscripts/com/retreive-data/simple.get.json.ftl

<#escape x as jsonUtils.encodeJSONString(x)>

{

   "field1": "${field1}"

}

</#escape>



And also the there are xml files, I dont know where could I use them for Java Backed webscript

/src/main/amp/config/alfresco/web-extension/monitor-websites-share-slingshot-application-context.xml

/src/main/resources/alfresco/web-extension/custom-slingshot-application-context.xml

/src/main/resources/alfresco/web-extension/share-config-custom.xml.sample

/src/main/resources/META-INF/share-config-custom.xml.sample

Thanks advance for your help

dj

1 ACCEPTED ANSWER

jpotts
World-Class Innovator
World-Class Innovator

Your class looks fine, so it must be the Spring context file that is not correct.

I suspect it is your bean ID. As it says in my tutorial, the syntax for the bean ID is:

webscript.package.service-id.method

But yours is "webscript.sudo". Alfresco has no way of knowing which web script you intend to use that class with unless that ID follows the convention. You need to change it to:

webscript.sudo.sudorename.get

Be sure to restart Alfresco after that change.

View answer in original post

10 REPLIES 10

jpotts
World-Class Innovator
World-Class Innovator
You might want to have a look at <a href="http://ecmarchitect.com/alfresco-developer-series-tutorials/webscripts/tutorial/tutorial.html">this tutorial</a>. It describes how to write a Java-backed web script and the config you need.

The tutorial covers repo tier web scripts but it should be the same for Share tier web scripts.

Jeff

darkredd
Star Contributor
Star Contributor

Hi Jeff Potts,

I followed the principle and structure of your tutorial and that of the official Alfresco document tutorial, but still get the following error.

<blockcode>

2016-10-05 16:34:25,500  ERROR [freemarker.runtime] [http-apr-8080-exec-2] Error executing FreeMarker template

FreeMarker template error:

The following has evaluated to null or missing:

==> data  [in template "sudo/sudorename.get.html.ftl" at line 6, column 22]

Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??

The failing instruction (FTL stack trace):

----------

==> ${data}  [in template "sudo/sudorename.get.html.ftl" at line 6, column 20]

----------

</blockcode>

The webscript is fully registered and only when I invoke it do I get that error message. The Java class is a simple class to return a text (business logic to be done later).

My files:

tomcat\webapps\alfresco\WEB-INF\classes\za\gov\parliament\sudowebscripts\SudoChildNode.class

<blockcode>

public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)

  {

  Map<String, Object> model = new HashMap<String, Object>();

  model.put("data", "Data retrieved");

  logger.debug("Sudo webscript execution complete");

  return model;

  }

</blockcode>

tomcat\shared\classes\alfresco\extension\templates\webscripts\sudo\sudorename.get.desc.xml

tomcat\shared\classes\alfresco\extension\templates\webscripts\sudo\sudorename.get.html.xml

tomcat\shared\classes\alfresco\extension\sudorename-context.xml

<blockcode>

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN 2.0//EN' 'http://www.springframework.org/dtd/spring-beans-2.0.dtd'>

<beans>

  <bean id="webscript.sudo" class="za.gov.parliament.sudowebscripts.SudoChildNode" parent="webscript" />

</beans>

</blockcode>

In all the above is there something that I am missing?

Using Alfresco 5.2 201609 Nightly build

Much thanks.

~Vusani

jpotts
World-Class Innovator
World-Class Innovator

Can you provide the full class signature of your Java class so I can see what it extends/implements?

darkredd
Star Contributor
Star Contributor

Hi @Jeff

Here is the full class:

package za.gov.parliament.sudowebscripts;

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.extensions.webscripts.Cache;

import org.springframework.extensions.webscripts.DeclarativeWebScript;

import org.springframework.extensions.webscripts.Status;

import org.springframework.extensions.webscripts.WebScriptRequest;

public class SudoChildNode extends DeclarativeWebScript

{

  private Log logger = LogFactory.getLog(SudoChildNode.class);

  public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)

  {

  Map<String, Object> model = new HashMap<String, Object>();

  model.put("data", "Data retrieved");

  logger.debug("Sudo webscript execution complete");

  return model;

  }

}

Much Thanks

~Vusani

jpotts
World-Class Innovator
World-Class Innovator

Your class looks fine, so it must be the Spring context file that is not correct.

I suspect it is your bean ID. As it says in my tutorial, the syntax for the bean ID is:

webscript.package.service-id.method

But yours is "webscript.sudo". Alfresco has no way of knowing which web script you intend to use that class with unless that ID follows the convention. You need to change it to:

webscript.sudo.sudorename.get

Be sure to restart Alfresco after that change.

darkredd
Star Contributor
Star Contributor

Much thanks,

Indeed it was the bean ID, made the change and worked. It was an oversight while I was working through the tutorial.

~Vusani

jpotts
World-Class Innovator
World-Class Innovator

Awesome, glad you got it working!

kaynezhang
World-Class Innovator
World-Class Innovator

And you'd better check if a variable exists in  FreeMarker template before using it,like this

<#if data??>

    ${data}

</#if>

Or  display a default text when the variable is null ,like this

${(data)!"default text"}

mitpatoliya
Star Collaborator
Star Collaborator

Your issue is due to reasons given by Jeff and and kyne. Error is because you are trying print value of "data" which is null and you do not have any null check in your ftl.

Reason your data is null is because your java class is not getting called.