Monday, March 7, 2011

Dynamically positioning a signature in iText

After much tinkering, I've been able to find a way to dynamically populate a signature image to the bottom of a PDF as follows:


            PdfReader reader = new PdfReader(...);

            PdfStamper stp = PdfStamper.createSignature(reader, os, '\0'); 
            PdfSignatureAppearance sap = stp.getSignatureAppearance();
            sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
            sap.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
                
            
            Image image = Image.getInstance(imageBytes);
            sap.setRender(PdfSignatureAppearance.SignatureRenderGraphicAndDescription);
            sap.setSignatureGraphic(image);
            sap.setAcro6Layers(true);
            sap.setImage(null);
            
            int page = reader.getNumberOfPages();

            Rectangle pageSize = reader.getPageSize(page);

            int verticalPosition = (int)(pageSize.getBottom() + 70);
            int signatureEnd = (int) verticalPosition + 50;
            position = new Rectangle(50, verticalPosition, 250, signatureEnd);                        
            
            sap.setVisibleSignature(position, page, null);

Tuesday, March 1, 2011

I had this unusual problem with Data Tables (http://datatables.net) where the table was being pushed down leaving a blank space above the table. I've shown the problem below:

I've highlighted the blank space in red to make it more visible.

After spending some time and using the webdeveloper firefox plugin, I narrowed it down to a CSS properties called float and clear

To see these properties in action, here is a simple website demonstrating how these properties work:
http://www.quackit.com/css/properties/css_clear.cfm

Looking back at the image above, you can see that the tables were being pushed down to the bottom of menus on the left hand side. Its important to notice this as we shall soon see why. This is the CSS definition for the menus:

#sidebar {
    float: left;    width: 150px;
    margin-right: 0px;
    margin-bottom: 20px;
    margin-left: 0px;
    /*padding-top: 20px;*/
    
    background-image: url(../images/menutop_owr3.gif);
    background-repeat: no-repeat;
    background-position: top;
}

The important property to note, is that the menus are being floated to the left.

Now looking at the CSS applied to Data Tables I was using the demo_table_jui.css which had the following definitions:

.dataTables_wrapper {
    position: relative;
    min-height: 302px;
    _height: 302px;
    clear: both;}
table.display {
 margin: 0 auto;
 width: 100%;
 clear: both;
 border-collapse: collapse;
} 

The clear property meant that any floating elements on either the right or left should be cleared, resulting in the tables being pushed down. To resolve the issue, I simply had to remove this property from the definitions as follows:


.dataTables_wrapper {
    position: relative;
    min-height: 302px;
    _height: 302px;
    /*clear: both;*/
table.display {
 margin: 0 auto;
 width: 100%;
 /*clear: both;*/
 border-collapse: collapse;
} 

And now the tables are displayed without the ugly gap:

Sunday, February 6, 2011

Reconstruct URL from request

<c:set var="jarURL" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}/${pageContext.request.contextPath}/" />

Thursday, January 27, 2011

EhCache integration with Spring

Previously I've used EhCache several times in applications where we would manage and retrieve cached items within our code. For example to store a cached item we may see the following:

cache.put(new Element(key, value));

And to get a cached item we may do the following:

            Element element = cache.get(key);
            if (null != element) {
                return element.getObjectValue();
            }

On top of that we need to manage the caches using code similar to the following:
            manager.addCache(name);
            cache = manager.getCache(name);


Several times I've asked myself 'Wouldn't it be nice if the caching mechanims were more transparent?". Caching is one of those "behind the scenes" type of operations and I never did felt comfortable putting it into our application code even if we layered it properly in multiple service tiers.

Today, I learned of EhCache and spring integration which was so easy to setup with little or no cache management. The idea is quite simple: Cache the results returned from method calls.

Here's an example of how you would declare that the results of a method be cacheable:

    @Cacheable(cacheName="mustererMouseObservations")    public Mouse getMouseObservations(String barcode) throws MustererException;

It automatically creates a cache called "mustererMouseObservations" and stores the returned result. Next time the method is invoked with the same arguments, the object returned is taken from the cache!

To declare the ehcache annotations, add the following to the spring bean wiring file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.0.xsd"
        >

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="au.org.australianphenomics" />
    <!-- Caching of Spring Method results -->
    <ehcache:annotation-driven create-missing-caches="true" cache-manager="ehCacheManager" />
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <!--property name="configLocation" value="classpath:META-INF/spring/ehcache.xml" /-->
    </bean>

If you're using Maven like I am you can add the dependency in your pom file:
        <!--  EhCache annotations -->
        <dependency>
          <groupId>com.googlecode.ehcache-spring-annotations</groupId>
          <artifactId>ehcache-spring-annotations</artifactId>
          <version>1.1.2</version>
        </dependency>

And that's it! There's no need to explicity add items to the cache, or retrieve from the cache. It's all done transparently!

Note:
Be aware that cacheable methods that are self-invocating a method on itself, are not cacheable as stated as follows:

Self-Invocation

Only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual cache interception at runtime even if the invoked method is marked with @Cacheable.


References:
http://code.google.com/p/ehcache-spring-annotations/
http://ehcache.org/recipes/spring-annotations.html
http://developers-blog.org/blog/default/2010/06/07/Caching-Example-with-Spring-Annotations-and-Ehcache
http://onjavahell.blogspot.com/2009/09/annotation-driven-caching-with-ehcache.html

Wednesday, January 26, 2011

JSTL duplication

I wanted to be able to traverse a Java HashMap using JSTL but at the same time I didn't want to display duplicate entries.

To do that with JSTL, I stored the list of already displayed records into a HashMap as follows:

                            <%-- Don't show duplicate gene names --%>
                            <%-- Already process Genes --%>
                            <jsp:useBean id="processed" class="java.util.HashMap" />                            <c:forEach items="${facs.genotypeTranslationErrors}" var="genotypeObservation" varStatus="errorCounter">
                                <c:if test="${empty processed[genotypeObservation.improperGeneName]}">
                                    <c:set target="${processed}" property="${genotypeObservation.improperGeneName}" value="${true}" />                            
                                    <p>
                                        Assay:                            
                                        <span class="errorHighlight">
                                            <strong>${genotypeObservation.improperGeneName}</strong>
                                        </span>
                                    </p>
                                </c:if>                                                
                            </c:forEach>

Reference: http://stackoverflow.com/questions/4517928/how-to-group-rows-with-duplicate-column-properties-in-a-jstl-cforeach

Sunday, January 16, 2011

CSRF Security Error

 This occurred when I combined DWR with Tomcat 7

To resolve this error edited the catalina_home/conf/context.xml

<Context useHttpOnly=”false”>



 Reference: http://redrata.com/2010/11/resolving-dwr-csrf-security-error-popups/#more-125