cancel
Showing results for 
Search instead for 
Did you mean: 

Java Backed Web Script Example

nm_santos
Champ in-the-making
Champ in-the-making
Hi,

I'm following the tutorial located at http://docs.alfresco.com/4.0/topic/com.alfresco.enterprise.doc/tasks/ws-folderListing-Java-scripting...

after completing all the steps up to the java class, when I try to access the url (After rebooting the server), I always get the following error:

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

Message:   05010002 Wrapped Exception (with status template): 05010007 Error during processing of the template 'Expression folder is undefined on line 3, column 19 in org/example/javadir.get.html.ftl.'. Please contact your system administrator.

Exception:   freemarker.core.InvalidReferenceException - Expression folder is undefined on line 3, column 19 in org/example/javadir.get.html.ftl.

Anyone has any idea why?

Here's the javadir.get.html.ftl file:
<html>
<head>
  <title>Folder ${folder.displayPath}/${folder.name}</title>
  </head>
<body>
   Alfresco ${server.edition} Edition v${server.version} : dir
  <p>
  Contents of folder ${folder.displayPath}/${folder.name}
  <p>
  <table>
   <#list folder.children as child>
   <tr>
   <td><#if child.isContainer>d</#if></td>
   <#if verbose>
     <td>${child.properties.modifier}</td>
     <td><#if child.isDocument>
       ${child.properties.content.size}</#if></td>
     <td>${child.properties.modified?date}</td>
   </#if>
   <td>${child.name}</td>
   </tr>
   </#list>
  </table>
</body>
</html>
10 REPLIES 10

mitpatoliya
Star Collaborator
Star Collaborator
As per the error it seems like your webscript is not calling your java class.
make sure it is calling java class as in java class we are putting this folder object in the model.
Which later we are parsing inside the ftl.

hunjet
Champ in-the-making
Champ in-the-making
Hi,

Did you register your Java-based web script as spring bean?
This is important for the Web Script Framework to locate your script.

Regards,
Blaz

nm_santos
Champ in-the-making
Champ in-the-making
Hi,

Did you register your Java-based web script as spring bean?
This is important for the Web Script Framework to locate your script.

Regards,
Blaz

Then what am I doing wrong?

I've got JavaDir.class in data dictionary/web script extensions/org/example

I've got javadir-context.xml in data dictionary/web script extensions.

In what folder do I need do put the javadir.desc.xml and javadir.get.html.ftl ? (right now they are in the same folder as JavaDir.class)

Regards,
Nuno.

abarisone
Star Contributor
Star Contributor
Hi,
try this:
    1) put your class into a jar under alfresco classpath namely under tomcat/webapps/alfresco/WEB-INF/lib
    2) put the javadir.desc.xml and javadir.get.html.ftl under tomcat/webapps/alfresco/extension/templates/webscripts
    3) as stated by point 2.c) Place the Spring Framework configuration file into the extension classpath of the Alfresco content application server, so under tomcat/webapps/alfresco/extension
    4) be sure that the javadir-context.xml is correctly configured
Hope this helps
Regards

nm_santos
Champ in-the-making
Champ in-the-making
Hi,
try this:
    1) put your class into a jar under alfresco classpath namely under tomcat/webapps/alfresco/WEB-INF/lib
    2) put the javadir.desc.xml and javadir.get.html.ftl under tomcat/webapps/alfresco/extension/templates/webscripts
    3) as stated by point 2.c) Place the Spring Framework configuration file into the extension classpath of the Alfresco content application server, so under tomcat/webapps/alfresco/extension
    4) be sure that the javadir-context.xml is correctly configured
Hope this helps
Regards

Only my class or all of the project?

I did not have an extension folder, so I tried creating the missing folders but the same error occurs..

Regards,
Nuno.

abarisone
Star Contributor
Star Contributor
Sorry the correct path for steps 2 and 3 is
/alfresco-4.0.d/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/extension
If you don't have one you can create it.
In your jar put the compiled class only.
I have one since I install my application as an AMP module.

Regards

nm_santos
Champ in-the-making
Champ in-the-making
Sorry the correct path for steps 2 and 3 is
/alfresco-4.0.d/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/extension
If you don't have one you can create it.
In your jar put the compiled class only.
I have one since I install my application as an AMP module.

Regards


Hi!

It now successfully detects my script Smiley Happy

Thanks!

However, when I access the script, it says that "Company Home Folder wasn't found".

What am I still missing?

I haven't changed the code.

Regards,
Nuno.

tbedford
Champ in-the-making
Champ in-the-making
Just following up on this.

Here's what worked for me:

./tomcat/shared/classes/alfresco/extension/javadir-context.xml
./tomcat/shared/classes/alfresco/extension/templates/webscripts/org/example/javadir.get.desc.xml
./tomcat/shared/classes/alfresco/extension/templates/webscripts/org/example/javadir.get.html.ftl
./tomcat/webapps/alfresco/WEB-INF/classes/org/example/JavaDir.class

On the issue of Company Home not being found, I also had the same problem. Curiously, it does find sub-directories however - works fine for those. It seems if you feed in Company Home to the findNodeRef() you get back a null - I'm not sure why. I thought it might be a permission issue, but it doesn't seem to be. So I made a slight change to the code and this works:


package org.example;

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

import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;

public class JavaDir extends DeclarativeWebScript
{
   private Repository repository;

   public void setRepository(Repository repository)
   {
      this.repository = repository;
   }

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

      NodeRef folder;
      
      // extract folder listing arguments from URI
      String verboseArg = req.getParameter("verbose");
      Boolean verbose = Boolean.parseBoolean(verboseArg);
      
      Map<String, String> templateArgs =
            req.getServiceMatch().getTemplateVars();
      String folderPath = templateArgs.get("folderpath");
      
      if (folderPath.equals("Company Home")){
         
         folder = repository.getCompanyHome();   
                  
      }
      else {
         String nodePath = "workspace/SpacesStore/" + folderPath;
         folder = repository.findNodeRef("path", nodePath.split("/"));
      }
      
      // validate that folder has been found
      if (folder == null)
      {
         throw new WebScriptException(Status.STATUS_NOT_FOUND,
               "Folder " + folderPath + " not found");
      }

      // construct model for response template to render
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("verbose", verbose);
      model.put("folder", folder);
      return model;
         }
}


Hope this helps!

abarisone
Star Contributor
Star Contributor
Well, if the code is taken from the Alfresco Help page and is unchanged you can debug it.
Pay particular attention to these few lines of code
String folderPath = templateArgs.get("folderpath");
String nodePath = "workspace/SpacesStore/" + folderPath;
NodeRef folder = repository.findNodeRef("path", nodePath.split("/"));
The findNodeRef method javadoc states:
public org.alfresco.service.cmr.repository.NodeRef findNodeRef(java.lang.String referenceType,
                                                               java.lang.String[] reference)
Helper to convert a Web Script Request URL to a Node Ref
1) Node - {store_type}/{store_id}/{node_id}
Resolve to node via its Node Reference.
2) Path - {store_type}/{store_id}/{path}
Resolve to node via its display path.
3) AVM Path - {store_id}/{path}
Resolve to AVM node via its display path
4) QName - {store_type}/{store_id}/{child_qname_path} TODO: Implement
Resolve to node via its child qname path.

Parameters:
referenceType - one of node, path, avmpath or qname
Returns:
reference array of reference segments (as described above for each reference type)
You might also take a look at the Alfresco Node Browser to find out other information about these folders.
Try also to create other folders and pass them in the URI. The method takes Company Home as the root of the repository
Regards
Andrea