cancel
Showing results for 
Search instead for 
Did you mean: 

Java Class not invoking in web-scripts

mazhar_shaukat
Champ in-the-making
Champ in-the-making
Hi,

I'm new to web-scripts. I'm trying to successfully run an example.
My Java Class is as follow;

package org.alfresco.module.demoscripts;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;

public class SimpleWebScript extends AbstractWebScript {

   @Override
   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 RuntimeException("Unable to serialize JSON");
       }

   }

}

Descriptor simple.get.desc.xml
<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>
  <family>Alfresco Java-Backed WebScripts Demo</family>
</webscript>

When I access it like this;
http://localhost:8080/alfresco/service/demo/simple
I get the following error:
org.springframework.extensions.webscripts.WebScriptException: 04110003 Web Script format '' is not registered

when I try following
http://localhost:8080/alfresco/service/demo/simple?format=json

I get this error:

org.springframework.extensions.webscripts.WebScriptException: 04110002 Cannot locate template processor for template org/alfresco/demo/simple.get.json

please help
7 REPLIES 7

jpotts
World-Class Innovator
World-Class Innovator
I see your descriptor and I see your controller, but I do not see a view. Have you created a view corresponding to the format you want the web script to return? For example, if you are trying to return JSON, you need a FreeMarker file called simple.get.json.ftl.

Also, why did you set the default format in your descriptor to an empty string? I think you may want that to be "json".

Finally, in your controller, it looks like you are building JSON. A more flexible way to create your web script is to have your controller deal with data only and not worry about your response format. Then you can have multiple views implemented in FreeMarker, one of which can return JSON.

If you follow this approach, your controller would simply put data in the model, like this:
Map<String, Object> model = new HashMap<String, Object>();
model.put("field1", "data1");
return model;
Your view (simple.get.json.ftl) might look like this:
<#escape x as jsonUtils.encodeJSONString(x)>
{
   "field1": "${field1}",
}
</#escape>
Hope that helps,

Jeff

mazhar_shaukat
Champ in-the-making
Champ in-the-making
Thanks a lot Jeff for your reply.
I have modified the code accordingly, my java class is as follows now;
package org.alfresco.module.demoscripts;

import java.util.HashMap;
import java.util.Map;

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 SimpleWebScript extends DeclarativeWebScript {


   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest req,
         Status status, Cache cache) {
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("field1", "data1");
      return model;
   }
}

and template file:
<#escape x as jsonUtils.encodeJSONString(x)>
{
   "field1": "${field1}",
}
</#escape>

descriptor:
<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="json">argument</format>
  <family>Alfresco Java-Backed WebScripts Demo</family>
</webscript>

But now I'm getting the following error;

org.springframework.extensions.webscripts.WebScriptException - 04120002 Cannot locate template processor for template org\/alfresco\/demo\/simple.get.json

The bean definition in web-scripts-application-context.xml:

<bean id="webscript.org.example.javadir.get" class="org.example.JavaDir" parent="webscript">
<property name="repository" ref="repositoryHelper"/>
</bean>

My Java class file location: C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\org\alfresco\module\demoscripts

jpotts
World-Class Innovator
World-Class Innovator
You named your template "simple.get.json" when it should be named "simple.get.json.ftl".

Jeff

priyesh
Champ in-the-making
Champ in-the-making
After following all the above steps, I am getting the following error:-


{
   "field1": "{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "02050000 Wrapped Exception (with status template): 02050011 Error during processing of the template 'The following has evaluated to null or missing:\n==> jsonUtils.encodeJSONString(x)  [in template \"demo\/simple.get.json.ftl\" at line 1, column 15]\n\nTip: 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)??\n\nThe failing instruction:\n==> ${field1} auto-escaped  [in template \"demo\/simple.get.json.ftl\" at line 3, column 15]'. Please contact your system administrator.", 
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 02050000 Wrapped Exception (with status template): 02050011 Error during processing of the template 'The following has evaluated to null or missing:\n==> jsonUtils.encodeJSONString(x)  [in template \"demo\/simple.get.json.ftl\" at line 1, column 15]\n\nTip: 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)??\n\nThe failing instruction:\n==> ${field1} auto-escaped  [in template \"demo\/simple.get.json.ftl\" at line 3, column 15]'. Please contact your system administrator.",
 
  "callstack" :
  [
       ""      ,"freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:\n==> jsonUtils.encodeJSONString(x)  [in template \"demo\/simple.get.json.ftl\" at line 1, column 15]\n\nTip: 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)??\n\nThe failing instruction:\n==> ${field1} auto-escaped  [in template \"demo\/simple.get.json.ftl\" at line 3, column 15]"
      ,"freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)"
      ,"freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:382)"
      ,"freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)"
      ,"freemarker.core.DollarVariable.accept(DollarVariable.java:76)"
      ,"freemarker.core.Environment.visit(Environment.java:265)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:93)"
      ,"freemarker.core.Environment.visit(Environment.java:265)"
      ,"freemarker.core.EscapeBlock.accept(EscapeBlock.java:85)"
      ,"freemarker.core.Environment.visit(Environment.java:265)"
      ,"freemarker.core.Environment.process(Environment.java:243)"
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:217)"
      ,"org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:955)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:418)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:600)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScriptInternal(RepositoryContainer.java:307)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:261)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:378)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:727)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)"
      ,"org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)"
      ,"org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)"
      ,"org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)"
      ,"org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)"
      ,"org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)"
      ,"org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)"
      ,"org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)"
      ,"org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)"
      ,"java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)"
      ,"java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)"
      ,"java.lang.Thread.run(Unknown Source)"
      ,"org.alfresco.service.cmr.repository.TemplateException: 02050011 Error during processing of the template 'The following has evaluated to null or missing:\n==> jsonUtils.encodeJSONString(x)  [in template \"demo\/simple.get.json.ftl\" at line 1, column 15]\n\nTip: 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)??\n\nThe failing instruction:\n==> ${field1} auto-escaped  [in template \"demo\/simple.get.json.ftl\" at line 3, column 15]'. Please contact your system administrator."
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:221)"
      ,"org.springframework.extensions.webscripts.WebScriptException: 02050000 Wrapped Exception (with status template): 02050011 Error during processing of the template 'The following has evaluated to null or missing:\n==> jsonUtils.encodeJSONString(x)  [in template \"demo\/simple.get.json.ftl\" at line 1, column 15]\n\nTip: 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)??\n\nThe failing instruction:\n==> ${field1} auto-escaped  [in template \"demo\/simple.get.json.ftl\" at line 3, column 15]'. Please contact your system administrator."
      ,"org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1126)"

  ],
 
  "server" : "Community v5.0.0 (c r91299-b145) schema 8,009",
  "time" : "Mar 5, 2015 6:33:38 PM"
}

rjohnson
Star Contributor
Star Contributor
Your template is wrong it should be


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

{

   "field1": "${field1}"

}

</#escape>


Also whenever Alfresco reports an error in jsonUtils.encodeJSONString(x) its not being entirely truthful. What it means is that the string that you have sent this macro is not well formed JSON. Yours wasn't because of the trailing comma.

priyesh
Champ in-the-making
Champ in-the-making
Thank you, it worked.

risharommi
Champ in-the-making
Champ in-the-making