To complete the Folder Listing Java-backed web script, you must create its
controller. The controller parses the URI to extract the token values, interacts with the
repository to locate the specified folder, and populates the model for subsequent rendering by
the HTML response template.
This develops a controller in Java.
-
Create the Java class for the Folder Listing web script:
- Launch your Java IDE.
- Create a Java package called org.example
- Create a Java class called JavaDir
-
Implement the Java class as follows:
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; } }
-
Compile the Java class.
Note: You will need to link against the various required classes that are imported, such as DeclarativeWebScript. To do this you will need to import the SkyVault SDK into your IDE workspace, or you can import the various SkyVault projects into your workspace from the source code. You can then configure your project properties to include the required projects and libraries into your Java Build Path. Projects that need to be linked could include Core, Data Model, Deployment, MBean, Remote API, Repository, and Web Framework Commons. Libraries could include the spring-webscripts and spring-webscripts-api JAR files. The projects and libraries you need to include will depend on what your Java-backed web script actually needs to do.
- Place the compiled Java class into the folder <install_dir>/tomcat/webapps/alfresco/WEB-INF/classes/org/example.
-
Create the Spring Framework configuration for registering your web script Java
class:
- Create an XML file called javadir-context.xml.
-
Register the Java class as follows:
<?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.org.example.javadir.get" class="org.example.JavaDir" parent="webscript"> <property name="repository" ref="repositoryHelper"/> </bean> </beans>
-
Place the Spring Framework configuration file into the extension classpath of the
SkyVault Community Edition server, in this
case
<install_dir>/tomcat/shared/classes/alfresco/extension/javadir-context.xml.
Important: When deploying a Java-backed web script to the SkyVault Community Edition server, you must restart the server to fully register the web script.
- In Share, create a folder test under the Repository root.
- Upload some sample content files to the test folder.
-
Test your Java-backed web script by typing the following in a tab of your web
browser:
http://localhost:8080/alfresco/service/javadir/Company%20Home/test?verbose=true
If successful, a verbose listing of the contents of the test folder displays.
-
Test your Java-backed web script again by typing the following in your web
browser:
http://localhost:8080/alfresco/service/javadir/Company%20Home
If successful, a verbose listing of the contents of the Company Home folder displays. Externally, this Folder Listing web script looks and behaves the same as its scripted web script implementation.
For ease of reference here are the standalone files that were created in this project,
along with their locations:
./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