Calling web scripts from a JSP page

Now we will create a simple JSP page from which we will make a call to our web script to render the news page. This JSP can be a part of any of your web applications.

To call a web script from the JSP, this will be an HTTP call. Just add the following scriptlet in the JSP file wherever you want to include the HTML output of this web script:

<%
String resultString = "";
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope("localhost", 8080, "Alfresco"),
new UsernamePasswordCredentials("admin", "admin")
);
GetMethod get = new
GetMethod("http://localhost:8080/alfresco/service/org/cignex/news/getNewsItem.html?storeId=wwwcignex&newsId=newsAlfrescoBookRelease.xml");
get.setDoAuthentication(true);
try {
int status = client.executeMethod( get );
resultString = get.getResponseBodyAsString();
out.println(resultString);
} finally {
get.releaseConnection();
}
%>

Because we are using an HTTP client, we also need to import the used classes here in this scriptlet before using it. To import those, we can use the page directive of JSP as follows:

<%@ page import="org.apache.commons.httpclient.HttpClient" %>
<%@ page import="org.apache.commons.httpclient.UsernamePasswordCredentials" %>
<%@ page import="org.apache.commons.httpclient.auth.AuthScope" %>
<%@ page import="org.apache.commons.httpclient.methods.GetMethod" %>

This will import all of the required classes.

Note

The source files for the whole JSP page can be downloaded from the Packt website.

The following screenshot is the output of that JSP page. This JSP page is part of a sample web application, which is outside of Alfresco and deployed on a separate Tomcat web server:

Calling web scripts from a JSP page

Enhancing the news item web script

In this case study, we will make enhancements to the previously mentioned news web script. Consider a case where you have some part of the content that is being used in multiple places, such as some contact number, e-mail address, link URLs, and so on. When this kind of content needs to be modified in the future, you will need to modify it at multiple places. Instead, we can have a concept where we store this kind of content at one place and then refer that in all of the places when we want to actually use it. In this way, it will enable reusability and it will be easier to modify it in the future. No longer will we have to modify the same content at multiple places; we just need to modify it at one place and that will be reflected in all of the places where it is being used.

Consider this kind of content as tag and store it separately in one place only. We can then refer to it with its tag ID in the actual content when it needs to be used. This means we will have one tag mapping XML file where we will store all these kinds of tags with some ID and value. And then while creating any content, if we want to use it, we will refer it with the ID, and at the time of rendering the response through the web script, it will replace those tag IDs with actual values.

Create a TagMapping.xml file in the /ROOT/common folder under your web project using the tag_mapping web form.

Note

Download the XSD file for this web form from the Packt website.

In the news content used in the previous case study, the website URL for the book is used at multiple places. We will create a tag for this and then use this tag in the news content.

A sample TagMapping.xml file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<items xmlns:alf="http://www.alfresco.org" xmlns:chiba="http://chiba.sourceforge.net/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item>
<id>phone_number</id>
<value>1-408-923-4231</value>
</item>
<item>
<id>book_website</id>
<value>http://www.packtpub.com/alfresco-3-enterprise-content- management-implementation/book/mid/160609knbhtv</value>
</item>
</items>

Here we have created a tag for the URL of the website for our Alfresco book with ID book_website, and in value we have specified the URL for this.

Now when we create XML content for the news, wherever we want to mention the book URL, instead of writing the actual URL, we will refer this tag with ID book_website as:

<a href="http://${book_website}">Alfresco 3 Enterprise Content Management Implementation</a>

We need to use it as ${tag_id} only (syntax for a FreeMarker template), because we are going to use the template service to replace this tag's runtime with the actual value of this tag.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset