Video Preview Portlet-Liferay Alfresco Integration

Video Preview Portlet can be implemented in couple of ways.

In my scenario, I had to pull the video content from Alfresco and show the preview inside a portlet.

Kept it simple using
JQueryMedia Plug-in.

Steps

1. Copy the JQuery Media Plugin scripts to your web application or if you have a theme, then to the theme folder.

2. Include scripts in the jsp page or the velocity template (in case of Portlet theme)

3. Add the below code in jsp

<script type="text/javascript">
$(function() {

$('a.media').media();

});

</script>

4. To show the video preview add

<a class="media {width:480, height:280}" href=" <YOUR URL HERE >" >VIDEO </a >

Inside href, you need to give the URL for the video. To get the URL need to use the Alfresco API.

Based on the file tye (the JQuery checks for extension) the library will embed the plug-in for the player in the web page.

Ex, if its wmv it will embed Windows Media Player, for flv , flash player, mov ,Qucik time (if available on your browser, else it will ask to download)

Spring SimpleMappingExceptionResolver to log stack trace

SimpleMappingExceptionResolver is generally used to redirect to an error page. It can be extended to log the trace to a log file.

public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {

private static Log log = LogFactory.getLog(LoggingExceptionResolver.class);

public LoggingExceptionResolver() {
super();
}

@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
log.debug(getStackTrace(ex));
return super.resolveException(request, response, handler, ex);
}

/**
* @param t , Throwable
* @return String of the exception
*/
public String getStackTrace(Throwable t) {
StringWriter stringWritter = new StringWriter();
PrintWriter printWritter = new PrintWriter(stringWritter, true);
t.printStackTrace(printWritter);
printWritter.flush();
stringWritter.flush();
return stringWritter.toString();
}
}

Configure this bean,
< bean id="exceptionResolver" class="com.common.service.LoggingExceptionResolver"
>
</bean >

Binary Data Over REST Web Service

There was requiremnet to retrieve images from a Content Management System and render the content to a portlet deployed in Liferay.

The application was built on top of Spring MVC portlet and the Service calls from Portlet was REST API calls. So the REST WS API was made to return a byte[] of the content

To sent binary data in REST call [senting binary over REST WS may be questioned], you should encode using sun.misc.BASE64Encoder and on the receiving end you can use sun.misc.BASE64Decoder to decode the data.

The code looks like this

Service API

@RequestMapping(value = "/cms/image", method = RequestMethod.GET)
public void (ModelMap modelMap){
byte[] buff= getBytes(); // Return the byte[] of the image
String
imgaeArray= new sun.misc.BASE64Encoder().encode(buff);
modelMap.addAttribute("result", imgaeArray);
}

At Portlet end;

@Autowired
RESTRepository repository;

@RequestMapping(params = "action=preview")
public String preview(Model model, RenderRequest renderRequest,RenderResponse renderResponse) {
Map <String, Object> paramMap = new HashMap
<String, Object>();
Map
<String, String> map = repository.get("/cms/image", Map.class,paramMap);
byte []
imgaeArray= new sun.misc.BASE64Decoder().decodeBuffer(map.get("result"));

}

If you don't know the content type, then you may need to return a DTO in the REST API instead of just encoded
byte[] . DTO can have the properties to hold the byte[] and the content type.

Performance Tuning

During what stage of the development cycle we start performance tuning? How much time are we spending to tune application that’s in Production or in UAT?

One comment I heard from even from a Enterprise Architect was, “I know those are important, but my boss says get me the functionality first, other “lities” can come later”. The “lities” include scalability, maintainability etc and performance as well.

I like to politely disagree to him. But this is what most the project goes through. Due to business compulsions or time lines we build the system and later spend sleepless nights in fixing the performance bottle necks.

In most of the Enterprise Java application development performance tuning starts late in the development cycle. We start worrying about performance in the UAT phase or even after production when performance becomes critical.

Performance is as important as any functionality we build. Why are we not investing time on the early stages of development?

Tuning an application; most people sees it only as SQL Query tuning. I agree that is important, but that is not all. Tuning is required in design, presentation layer, and UI components as well. Even some incorrect business requirement could lead to performance bottle necks.

In one of the project the database returned result is 1 minute but the screen rendering took 10 minutes for huge record sets. The issue was with a JSF UI component which displayed the result in table format and due to a wrong Java script implementation in that page. When component rendering was changed from the conventional <table>,<tr>,<td> to <div><span> the results were still encouraging.

What I suggest is performance tuning or performance; engineering track should cut across all stages of development life cycle from Functional Specification, Design, and Coding. As we progress in building system we should go back often tune where ever required. Make sure the application is tested with quality and quantity data.

Early Alarm makes life easy. Performance tuning; “Start Early, Do it regularly!!!”

HTTP PATCH, new entrant

A new action has been adopted to HTTP in March 2010, named PATCH along with GET,POST,POT,DELETE etc.

Before going in detail about PATCH, let's have quick look at PUT and POST and the basic differences

POST to create a new resource when the client cannot predict the identity on the origin server (creating a new purchase order)

The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database. {source RFC}

PUT is to override the definition of a specified resource with what is passed in from the client

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

So the fundamental difference in PUT and POST is that , in a POST the URI mentioned will handle the entity enclosed in the request, or in other words the entity is a subordinate of the URI. Where as in PUT , the URI identifies the entity enclosed in the request.

Most of the developer community avoid using PUT, may be because forms doesn't support that.

Why need a PATCH

The existing HTTP PUT method only allows a complete replacement of a document.New HTTP method, PATCH, to modify an existing HTTP resource.PATCH to override a portion of a specified resource in a predictable and effectively transactional way (if the entire patch cannot be performed, the server should not do any part of it). The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI.

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the
origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version. {RFC}
PATCH is neither safe nor idempotent ,but can be issues as idempotent
Who needs PATCH

In the REST-style of architecture is important to use the appropriate HTTP Verbs and the REST depends on URI for resource identification. "High" REST means the proper and full usage of all the verbs;GET, PUT,POST and PATCH in the days to come.

Then the semantic web need these partial updates than the complete replacement of the documnet.http://www.w3.org/Submission/SPARQL-Update/


There are currently no standardized patch formats in wide RESTful use, but they are likely to be designed for XML, HTML plain text and other common formats.

ResourceMapping Spring MVC

It is pretty easy to create a Resource handler using Spring Portlet MVC
Portlet needs resource URL to create download links for PDF/CSV etc.

Steps,

1.<a href="<portlet:resourceURL escapeXml="false" id="exportAsTxt"/>>Download File </a>

2. Create a controller
@Controller
@RequestMapping("VIEW")
public class ReportController{

}

3.Write a Resource handling method. Add the annotation @ResourceMapping

@ResourceMapping(value = "exportAsTxt")
public void serveFile(ResourceRequest request, ResourceResponse response) {
response.setProperty("Content-Disposition","attachment; filename=report.txt");
response.setContentType("text/plain");
try {
response.getWriter().write("Save to File");
} catch (IOException e) {
e.printStackTrace();
}
}

To the ResourceResponse set the Content-Disposition property to set the file name
and set the ContentType. The above method is for text file. Similar approach can be followed for PDF/CSV etc.


If any portlet interceptors are configured for the application make sure that the method preHandleResource returns 'true',( incase of an empty implementation ) .

@Override
public boolean preHandleResource(ResourceRequest request,
ResourceResponse response, Object handler) throws Exception {
return true;
}