AnsweredAssumed Answered

Problem with Web script folder.get json template/javascript

Question asked by alexr on Mar 28, 2008
Latest reply on Mar 31, 2008 by alexr
I have added the json template and javascript to the folder.get webservice
When I do a webservice call to retrieve 'html' (original functionality) I get a
folder list as expected.
However when I do a json call:
http://localhost:8080/alfresco/service/sample/folder.json/Company%20Home
I get an error 500 (see below).
Am I missing something or is this a case of misunderstanding the concept?

I have added a .json.ftl and a .json.js to bind to the original webscript.
For the record: I am using Alfresco Community 2.1 out of the box (no sdk).

Alex

Below is the webscript dump of "folder.get" and the Error 500 message.

============================================
Web Script: org/alfresco/sample/folder.get
Alfresco Community Network v2.1.0 (482)
Generated from /alfresco/service/script/org/alfresco/sample/folder.get on 28-mrt-2008 11:16:16

Script Properties  Id: org/alfresco/sample/folder.get
Short Name: Folder Listing Sample
Description: Sample demonstrating the listing of folder contents
Authentication: guest
Transaction: required
Method: GET
URL Template: /sample/folder/{path}
URL Template: /sample/folder.json/{path}
Format Style: extension
Default Format: html
Implementation: class org.alfresco.web.scripts.DeclarativeWebScript


Store: workspace://SpacesStore/app:company_home/app:dictionary/cm:extensionwebscripts

[No implementation files]


Store: workspace://SpacesStore/app:company_home/app:dictionary/cm:webscripts

File: org/alfresco/sample/folder.get.json.js 
/*
   json.js
(modified 2006-09-07): added support for RHINO
(modified 2006-05-02): json.parse, json,stringify added
  USAGE:
  var jsObj = JSON.parse(jsonStr);
  var jsonStr = JSON.stringify(jsObj);
*/
if (!this.json) {
  var json = (function () {
      var m = {
              '\b': '\\b',
              '\t': '\\t',
              '\n': '\\n',
              '\f': '\\f',
              '\r': '\\r',
              '"' : '\\"',
              '\\': '\\\\'
          },
          s = {
              array: function (x) {
                  var a = ['['], b, f, i, l = x.length, v;
                  for (i = 0; i < l; i += 1) {
                      v = x;
                      f = s[typeof v];
                      if (f) {
                          v = f(v);
                          if (typeof v == 'string') {
                              if (b) {
                                  a[a.length] = ',';
                              }
                              a[a.length] = v;
                              b = true;
                          }
                      }
                  }
                  a[a.length] = ']';
                  return a.join();
              },
              'boolean': function (x) {
                  return String(x);
              },
              'null': function (x) {
                  return "null";
              },
              number: function (x) {
                  return isFinite(x) ? String(x) : 'null';
              },
              object: function (x) {
                  if (x) {
 
                      if (x instanceof Array) {
                          return s.array(x);
                      }
                      if (x.hashCode) return s.string(+x.toString());
 
                      var a = ['{'], b, f, i, v;
                      for (i in x) {
                          v = x;
                          f = s[typeof v];
                          if (f) {
 
                              v = f(v);
 
                              if (typeof v == 'string') {
                                  if (b) {
                                      a[a.length] = ',';
                                  }
                                  a.push(s.string(i), ':', v);
                                  b = true;
                              }
                          }
                      }
                      a[a.length] = '}';
                      return a.join();
                  }
                  return 'null';
              },
              string: function (x) {
                  if (/["\\\x00-\x1f]/.test(x)) {
                      x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                          var c = m;
                          if © {
                              return c;
                          }
                          c = b.charCodeAt();
                          return '\\u00' +
                              Math.floor(c / 16).toString(16) +
                              (c % 16).toString(16);
                      });
                  }
                  return '"' + x + '"';
              }
          };
 
     return {
        parse: function(s) {
           try {
              return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                 s.replace(/"(\\.|[^"\\])*"/g, ))) &&
                 eval('(' + s + ')');
           } catch (e) {
              return false;
           }
        },
        stringify: s.object
     };
  })();
}
var WEBSERVICE_PATH = '/com/onepoint/alfresco/webservices/folderbrowser';
/**
* This is a Javascript object that can be used
* for JSON serialization.
* @param folder The Java object that is going to be transformed into a Javascript object.
*/
function JsonFolder(folder) {
  this.displayPath = folder.displayPath;
  this.name        = folder.name;
  this.hasParent   = folder.parent.parent != null;
  this.parentPath  = escape(encodePath(folder.parent));
  this.folders     = new Array();
  this.files       = new Array();
  this.fileLinks   = new Array();
 
  for(var i = 0, length = folder.children.length; i < length; i++) {
     var child = folder.children;
     if (child.isContainer) {
        this.folders[this.folders.length] = escape(encodePath(child));
     }
     else {
        this.files[this.files.length] = escape(encodePath(child));
        var childName = child.name != null ? child.name : "";
        this.fileLinks[this.files.length] = escape(url.serviceContext + "/api/node/content/"
           + child.nodeRef.storeRef.protocol + "/" + child.nodeRef.storeRef.identifier + "/"
           + child.nodeRef.id + "/" + childName);
     }
  }
}
/**
* Encodes the path of one node.
* @param node The node that is to be encoded.
*/
function encodePath(node) {
  var path = "";
  if(node.parent) {
     path = node.name;
     path = encodePath(node.parent) + '/' + path;
  }
  return path;
}
// locate folder by path
if ((args.p == null || args.p.length == 0) && (url.extension == null || url.extension.length == 0))
{
  status.code = 400;
  status.message = "No folder has been specified.";
  status.redirect = true;
}
else {
  var folder = roothome.childByNamePath(args.p != null ? args.p : url.extension);
  model.folder = folder;
  var jsonFolder = new JsonFolder(folder);
  model.jsonFolder = json.stringify(jsonFolder);
}
File: org/alfresco/sample/folder.get.js 
// locate folder by path
// NOTE: only supports path beneath company home, not from root
var folder = roothome.childByNamePath(url.extension);
if (folder == undefined || !folder.isContainer)
{
      status.code = 404;
   status.message = "Folder " + url.extension + " not found.";
   status.redirect = true;
}
model.folder = folder;
File: org/alfresco/sample/folder.get.atom.ftl 
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator version="${server.version}">Alfresco (${server.edition})</generator>
  <title>Folder: ${folder.displayPath}/${folder.name}</title>
  <updated>${xmldate(date)}</updated>
  <icon>${absurl(url.context)}/images/logo/AlfrescoLogo16.ico</icon>
<#list folder.children as child>
  <entry>
    <title>${child.name}</title>
    <#if child.isContainer>
      <link rel="alternate" href="${absurl(url.serviceContext)}/sample/folder<@encodepath node=child/>"/>
    <#else>
      <link rel="alternate" href="${absurl(url.serviceContext)}/api/node/content/${child.nodeRef.storeRef.protocol}/${child.nodeRef.storeRef.identifier}/${child.nodeRef.id}/${child.name?url}"/>
    </#if>
    <icon>${absurl(url.context)}${child.icon16}</icon>
    <id>urn:uuid:${child.id}</id>
    <updated>${xmldate(child.properties.modified)}</updated>
    <summary>${child.properties.description!""}</summary>
    <author>
      <name>${child.properties.creator}</name>
    </author>
  </entry>
</#list>
</feed>
<#macro encodepath node><#if node.parent?exists><@encodepath node=node.parent/>/${node.name?url}</#if></#macro>
File: org/alfresco/sample/folder.get.html.ftl 
<html>
  <head>
    <title>${folder.displayPath}/${folder.name}</title>
  </head>
  <body>
    Folder: ${folder.displayPath}/${folder.name}
    <br>
    <table>
     <#if folder.parent.parent?exists>
     <tr>
       <td><td><a href="${url.serviceContext}/sample/folder<@encodepath node=folder.parent/>">..</a>
     </tr>
     </#if>
<#list folder.children as child>
     <tr>
       <#if child.isContainer>
         <td>&gt;<td><a href="${url.serviceContext}/sample/folder<@encodepath node=child/>">${child.name}</a>
       <#else>
         <td><td><a href="${url.serviceContext}/api/node/content/${child.nodeRef.storeRef.protocol}/${child.nodeRef.storeRef.identifier}/${child.nodeRef.id}/${child.name?url}">${child.name}</a>
       </#if>
     </tr>
</#list>
    </table>
  </body>
</html>

<#macro encodepath node><#if node.parent?exists><@encodepath node=node.parent/>/${node.name?url}</#if></#macro>
File: org/alfresco/sample/folder.get.json.ftl 
${jsonFolder}
File: org/alfresco/sample/folder.get.desc.xml 
<webscript>
<shortname>Folder Listing Sample</shortname>
<description>Sample demonstrating the listing of folder contents</description>
  <url>/sample/folder/{path}</url>
  <url>/sample/folder.json/{path}</url>
  <format default="html">extension</format>
  <authentication>guest</authentication>
  <transaction>required</transaction>
</webscript>




Store: classpath:alfresco/templates/webscripts

[No implementation files]




======================================
ERROR:


Web Script Status 500 - Internal Error

The Web Script /alfresco/service/sample/folder.json/Company Home has responded with a status of 500 - Internal Error. 

500 Description: An error inside the HTTP server which prevented it from fulfilling the request. 
  
Message: Web Script format 'json/Company Home' is not registered 
  
Exception: org.alfresco.web.scripts.WebScriptException - Web Script format 'json/Company Home' is not registered 
   
org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:93) 
org.alfresco.web.scripts.WebScriptRuntime.wrappedExecute(WebScriptRuntime.java:342) 
org.alfresco.web.scripts.WebScriptRuntime.authenticatedExecute(WebScriptRuntime.java:308) 
org.alfresco.web.scripts.WebScriptRuntime$1.execute(WebScriptRuntime.java:163) 
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:225) 
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:155) 
org.alfresco.web.scripts.WebScriptRuntime.executeScript(WebScriptRuntime.java:174) 
org.alfresco.web.scripts.WebScriptServlet.service(WebScriptServlet.java:109) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:810) 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) 
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175) 
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) 
org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156) 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) 
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) 
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) 
org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) 
java.lang.Thread.run(Thread.java:595) 
   
Server: Alfresco Community Network v2.1.0 (482) schema 64 
Time: 28-mrt-2008 11:18:43 
  
Diagnostics: Inspect Web Script (org/alfresco/sample/folder.get) 

Outcomes