'Alfresco: How to write a simple Java based Alfresco web script?'

If you want to develop a new feature for Alfresco best way is WebScript! Let’s start with a simple Alfresco web script. First, you need to create an Alfresco AMP maven project using archetype. In this example, I’ll use the latest alfresco version 5.0.

nn

First I generated Alfresco All-in-One AMP. (Please refer my blog post on generating AMP projects.)

nn

If you go through the files structure which is generated, you will find out a sample web script. It is a JavaScript-based WebScript. By this example, I’m going to explain how to write a simple Java-based Hello World web script.

nn

HelloWorldWebScript.java

n

package org.dedunu.alfresco;nnimport java.util.HashMap;nimport java.util.Map;nnimport org.springframework.extensions.webscripts.Cache;nimport org.springframework.extensions.webscripts.DeclarativeWebScript;nimport org.springframework.extensions.webscripts.Status;nimport org.springframework.extensions.webscripts.WebScriptRequest;nnpublic class HelloWorldWebScript extends DeclarativeWebScript {nn    @Overriden    protected Map<String, Object> executeImpl(WebScriptRequest req,n             Status status, Cache cache) {n        Map<String, Object> model = new HashMap<String, Object>();nn        model.put("helloMessage", "Hello World! - Welcome to Alfresco Development!");nn        return model;n    }n}n

nn

service-context.xml

n

<?xml version='1.0' encoding='UTF-8'?>n<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>n<!--n	Licensed to the Apache Software Foundation (ASF) under one or moren	contributor license agreements.  See the NOTICE file distributed withn	this work for additional information regarding copyright ownership.n	The ASF licenses this file to You under the Apache License, Version 2.0n	(the "License"); you may not use this file except in compliance withn	the License.  You may obtain a copy of the License atn	http://www.apache.org/licenses/LICENSE-2.0n	Unless required by applicable law or agreed to in writing, softwaren	distributed under the License is distributed on an "AS IS" BASIS,n	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.n	See the License for the specific language governing permissions andn	limitations under the License.n-->n<beans>nn	<bean id="webscript.org.dedunu.alfresco.helloworld.get" class="org.dedunu.alfresco.HelloWorldWebScript" parent="webscript">n	</bean>nn</beans>n

nn

helloworld.get.desc.xml

n

<webscript>n    <shortname>Java based Sample Webscript</shortname>n    <description>This simple WebScript prints Hello World</description>n    <url>/dedunu/helloworld</url>n    <authentication>user</authentication>n    <format default="html"></format>n</webscript>n

nn

helloworld.get.html.ftl

n

${helloMessage}n

nn

Create the above files in below locations of your maven project.

nn

    n

  • HelloWorldWebScript.java – repo-amp/src/main/java/org/dedunu/alfresco/HelloWorldWebScript.java
  • n

  • helloworld.get.desc.xml – repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/helloworld.get.desc.xml
  • n

  • helloworld.get.html.ftl – repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/helloworld.get.html.ftl
  • n

  • service-context.xml – repo-amp/src/main/amp/config/alfresco/module/repo-amp/context/service-context.xml
  • n

nn

Use below command to run the maven project.

nn

$ mvn clean install -Prun n

nn

It may take a while to run the project after that open a browser window. Then visit below URL

nn

http://localhost:8080/alfresco/service/dedunu/helloworld.

nn

nn

The source code of this project is available on GitHub.

nn

Gists

nn

nn

Tags

nn

    n

  • webscript
  • n

  • Java
  • n

  • alfresco
  • n

n

Proudly powered by WordPress

Discover more from Dedunu

Subscribe now to keep reading and get access to the full archive.

Continue reading