indiWiz.com

Subhash's Tech Log

Archive for February, 2009

Buildr becomes Apache top level project

without comments

Apache Buildr is now a top-level Apache project. Buildr is a Java build tool which uses Ruby (and JRuby) and provides dependency management functionalities. Think Maven has got some real competition now!

Written by Subhash Chandran

February 25th, 2009 at 10:22 pm

Posted in Uncategorized

Tagged with , ,

Serving Static and Dynamic Resources in JSR 286

with 2 comments

To serve static content packaged as part of the portlet WAR, we generally take this approach:

<%=response.encodeURL(request.getContextPath()+"/js/project.js")%>

For generating dynamic content what do we do? In JSR 168 this was not possible. Thankfully, JSR 286 introduced this important concept. For creating dynamic content, just override the void serveResource(ResourceRequest request, ResourceResponse response) method in javax.portlet.ResourceServingPortlet (implemented by javax.portlet.GenericPortlet). A simple example:

    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String paramValue = request.getParameter("abc");
        System.out.println(paramValue);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        PrintWriter pw = response.getWriter();
        pw.println(paramValue);
        pw.close();
    }

To link to this dynamic content:

<portlet:resourceURL var="url">
  <portlet:param name="abc" value="Hello World"/>
</portlet:resourceURL>

<a href="<%=url%>">Dynamic link</a>

Written by Subhash Chandran

February 20th, 2009 at 5:15 pm

Posted in Uncategorized

Tagged with , ,

37 Signals Launches Sprockets

without comments

Sprockets (site) is described thus: Sprockets is a Ruby library that preprocesses and concatenates JavaScript source files. This is not a new idea, I have seen similar compiler in Liferay source for aggregating CSS files. Since this is sponsored by 37 Signals (creators or Ruby on Rails), Sprockets’s syntax can become a kind of standard. Already Prototype and Script.aculo.us JavaScript libraries (two OpenSource JavaScript libraries popular in RoR) comply to Sprockets’s syntax.

Written by Subhash Chandran

February 20th, 2009 at 2:28 pm

Posted in Scripting

Tagged with ,

Multi-part content upload in Apache Http Components / Http Client

with 3 comments

Apache Http Components project hosts an excellent library for making HTTP client requests. This is a simple example for making multipart/form-data request.

Dependencies to be added to you Maven project:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.0-beta2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.0-beta2</version>
</dependency>

And finally the code to execute the request:

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

...

// The input:
File f = ...;
String title = ...;
String desc = ...;

// The execution:
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpPost method = new HttpPost("http://host:port/path");
MultipartEntity entity = new MultipartEntity();
entity.addPart("title", new StringBody(title, Charset.forName("UTF-8")));
entity.addPart("desc", new StringBody(desc, Charset.forName("UTF-8")));
FileBody fileBody = new FileBody(f);
entity.addPart("file", fileBody);
method.setEntity(entity);

HttpResponse response = httpclient.execute(method);
System.out.println(response.getStatusLine());

Written by Subhash Chandran

February 11th, 2009 at 9:29 pm

Posted in Java

Tagged with , ,

WebDav Client in Java

with 5 comments

The history of Java WebDav client is discussed well in this blog: http://pragmaticchris.blogspot.com/2007/11/java-webdav-clients.html. There is one update to it though: the JackRabbit project has a well-defined WebDav client (extending Commons HTTP Client) available.

To add JackRabbit WebDav client to your Maven project, use this:

<dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-webdav</artifactId>
    <version>1.6.0</version>
</dependency>

I will show one example of using the WebDav client which I had developed to upload content to an WebDav server:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;

...

// WebDAV URL:
final String baseUrl = ...;
// Source file to upload:
File f = ...;
try{
    HttpClient client = new HttpClient();
    Credentials creds = new UsernamePasswordCredentials("username", "password");
    client.getState().setCredentials(AuthScope.ANY, creds);

    PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
    RequestEntity requestEntity = new InputStreamRequestEntity(
	    new FileInputStream(f));
    method.setRequestEntity(requestEntity);
    client.executeMethod(method);
    System.out.println(method.getStatusCode() + " " + method.getStatusText());
}
catch(HttpException ex){
    // Handle Exception
}
catch(IOException ex){
    // Handle Exception
}

Written by Subhash Chandran

February 11th, 2009 at 4:24 pm

Posted in Java

Tagged with , ,

Linux command-line XSLT

without comments

To convert an XML based on a XSL, use this command:

$ xsltproc /path/to/xsl.xsl /path/to/xml.xml

The converted document will be written to STDOUT. To write to a particular file, you may use the -o parameter:

$ xsltproc /path/to/xsl.xsl /path/to/xml.xml -o out.html

Check the info/man pages for additional information.

Written by Subhash Chandran

February 2nd, 2009 at 5:28 pm

Posted in Linux

Tagged with , ,