Hi! Welcome...

I am Ashish. I am Member of Apache MINA PMC, ASF Committer and avid Code hacker. Contact me at paliwalashish at gmail dot com. I work for Terracotta [http://www.terracotta.org] as Solution Architect. I contribute to jsmpp [http://code.google.com/p/jsmpp/] as well.

17 July 2009 ~ 0 Comments

[Apache Sanselan] Retrieving Image Metadata using Sanselan


Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.

In this article we shall see how to get Image metadata using Apache Sanselan.

Let’s see how the code looks

public static void main(String[] args) {
    String imageFile = args[0];
    try {
         // Metadata details
         IImageMetadata meta = Sanselan.getMetadata(new File(imageFile));
 
         System.out.println("--- Image Metadata ----");
         System.out.println(meta);
     } catch (ImageReadException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
}

Most of the work is done in single line ?

getMetadata() API returns the IImageMetadata details. The toString() method of actual instance class of IImageMetadata prints the image information in a formatted fashion. The instance depends upon the format of the image.

Running the program on one the images shipped with Sanselan source produced this output

--- Image Metadata ----
Exif metadata:
Root:
Image Description: '’'
Make: 'Oregon Scientific'
Model: 'DS6639'
Orientation: 1
XResolution: 72
YResolution: 72
Resolution Unit: 2
Software: 'Adobe Photoshop 7.0'
Modify Date: '2008:07:13 21:05:34'
YCbCr Positioning: 2
Exif Offset: 228

Exif:
Exposure Time: 10/10266 (0.001)
FNumber: 28/10 (2.8)
Exposure Program: 2
ISO: 55
Exif Version: 48, 50, 50, 48
Date Time Original: '2003:10:20 09:01:29'
Create Date: '2003:10:20 09:01:29'
Components Configuration: 1, 2, 3, 0
Compressed Bits Per Pixel: 2
Brightness Value: 9
Exposure Compensation: 0
Max Aperture Value: 3
Metering Mode: 1
Light Source: 0
Flash: 0
Focal Length: 864/100 (8.64)
Subject Location: 27556, 57533, 60147, 20319
Flashpix Version: 48, 49, 48, 48
Color Space: 1
Exif Image Width: 300
Exif Image Length: 225
File Source: 3
Scene Type: 1
Custom Rendered: 0
Exposure Mode: 0
White Balance: 1
Digital Zoom Ratio: 1
Focal Length In 3 5mm Format: 42
Scene Capture Type: 0
Gain Control: 0
Contrast: 0
Saturation: 0
Sharpness: 0

Sub: (jpegImageData)
Compression: 6
XResolution: 72
YResolution: 72
Resolution Unit: 2
Jpg From Raw Start: 838
Jpg From Raw Length: 6258

Photoshop (IPTC) metadata:
Caption/Abstract: ?

16 July 2009 ~ 0 Comments

[Apache Sanselan] Retrieving Image Information using Sanselan


Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.

In this article we shall see how to get Image Information using Apache Sanselan.

Let’s see how the code looks

public static void main(String[] args) {
String imageFile = args[0];
System.out.println("Image File = "+imageFile);
try {
ImageInfo imageInfo = Sanselan.getImageInfo(new File(imageFile));
System.out.println(imageInfo);
} catch (ImageReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Most of the work is done in single line :-)

getImageInfo() API returns the ImageInfo details. The toString() method of ImageInfo class prints the image information in a formatted fashion.

Running the program on one the images shipped with Sanselan source produced this output

Format Details: Jpeg/JFIF v.1.2

Bits Per Pixel: 24

Comments: 0

Format: JPEG

Format Name: JPEG (Joint Photographic Experts Group) Format

Compression Algorithm: JPEG

Height: 225

MimeType: image/jpeg

Number Of Images: 1

Physical Height Dpi: 72

Physical Height Inch: 3.125

Physical Width Dpi: 72

Physical Width Inch: 4.1666665

Width: 300

Is Progressive: true

Is Transparent: false

Color Type: RGB

Uses Palette: false

The ImageInfo class can be used programtically to arrange files, or for other purposes as well

References

Sanselan Source

Sanselan Wiki

08 June 2009 ~ 0 Comments

MINA vs Netty – A User’s perspective [Part 2]


Back with the series. Its going to be quick and small post. Would focus around similarity on these two frameworks

  • Both are NIO frameworks :-)
  • Trustin has been part of both Frameworks, lately contributing to Netty (Thanks to Emmanuel for pointing this out)
  • Both uses Annotation, though in MINA this is more in area of MINA State Machine, whereas Netty, has used annotations for configuring the IO Chains as well.
  • Both Frameworks are working aggressively towards improving their Documentation

Hmm.. that's it for this post. Will be back soon with more

02 April 2009 ~ 2 Comments

MINA vs Netty – A User’s perspective [Part 1]


Have came across this topic a lot, which is better MINA or Netty. Wanted to see what works out well from a User Perspective, thought about trying both Frameworks, from an End Users perspective. This series shall not just find difference, but find similarities in both frameworks as well.


Before I continue, the opinion expressed here are purely mine, and other can tend to disagree. I am a MINA committer myself, so may have a little bias towards MINA. Shall try to keep my discussion unbiased, as much as possible. Having used MINA for a while, have a natural comfort with the framework.

What to Expect

Shall try to evaluate both the frameworks, as if I need to use them in one of my own projects and try to provide rational for my thoughts. User comments shall provide more indepth to the posts. The details shall be covered as I move along on my journey. This post is just the beginning.

Latest Releases

MINA - MINA 2.0 M4
Netty - 3.0.2 GA

Community Activity


Having an active community in an Open Source project is very important. It assures that the project is actively being looked after.

Source of Data


MINA - http://mina.markmail.org/
Netty - http://netty.markmail.org/

MINA community is very active, and scores well above Netty. Though Trustin is very active in Netty, and I hope Netty community shall have active support from more users/developers.

Documentation


MINA and Netty both have sufficient documentation to start with, but there is need for improvement. Netty Documentation is better organized. MINA Documentation needs a better organization. (Blame me for this, I am supposed to take care of this).

Coming up Next


Working of examples on both the frameworks and try to come up with Application Architectures soon.

14 February 2009 ~ 3 Comments

[Spring Security] – Implementing Custom UserDetails using Hibernate


Recently was experimenting with creating a Custom UserDetails in Spring Security. Unfortunately, it took me a while before I could implement. In this post shall share my experience and how I developed it.


Assumption

I shall concentrate only on the configuration of UserDetails service. To see it in Action, you can take Spring petclinic tutorial and add the configuration to it.

Let’s briefly lay down the steps needed to implement a Custom UserDetails

  •  Implement a Custom User Class that implements UserDetails interface (This class can be customized to fit your needs)
  •  Implement Custom UserDetailsService to based on Hibernate
  • Configuring in the Spring context

Implementing Custom UserDetails

For simplicity perspective, I shall be omitting Hibernate related configuration from this section

 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
 
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
 
public class CustomUser implements Serializable, UserDetails {
         private static final long serialVersionUID = 1L;
         private long id;
         private String password;
         private String username;
         private Set roles;
 
       public CustomUser() {
       }
 
       public Set getRoles() {
           return roles;
       }
 
      public void setRoles(Set roles) {
            this.roles = roles;
      }
 
     private boolean accountNonExpired;
     private boolean accountNonLocked;
     private boolean credentialsNonExpired;
     private boolean enabled;
 
    public GrantedAuthority[] getAuthorities() {
            List list = new ArrayList();
            for (Authority role : roles) {
                  list.add(new GrantedAuthorityImpl(role.getAuthority()));
            }
           return (GrantedAuthority[])list.toArray(new GrantedAuthority[0]);
    }
}
 

Take a special note of getAuthorities(). This function from UserDetails interface needs to return all the authorities for the given user. You can store Roles in whichever format you desire, and can translate them here. For simplicity sake, here is my Authorities class.

public class Authority implements Serializable {
        private static final long serialVersionUID = 1L;
        private long id;
        private String username;
        private String authority;
}


Implementing Custom UserDetailsService

Here using spring comes very handy. We extend HibernateDaoSupport and get most of the desired Hibernate functionality. Lets take a look at the class.

 
 
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
 
public class HibernateUserDetailsService extends HibernateDaoSupport implements UserDetailsService {
      public UserDetails loadUserByUsername(String username)
                         throws UsernameNotFoundException, DataAccessException {
              List results = getHibernateTemplate().find("from CustomUser where username = ?",
                                                                        new Object[] {username});
 
              if(results.size() < 1) {
                     throw new UsernameNotFoundException(username + "not found");
               }
               return (UserDetails) results.get(0);
       }
}

Well, its that simple. We just implement loadUserByUsername(String username) function and inside that we query for the specified user using the HibernateTemplate


Configuration

It’s time to tell Spring Security to use our custom UserDetails and UserDetailsService. Ensure that petclinic application is using Hibernate Context file (applicationContext-hibernate.xml). Check the same in web.xml. Let’s take a look at what’s the configuration we need to do

 
<b:bean id="userDetailsService" class="com.ap.user.HibernateUserDetailsService">
		<b:property name="hibernateTemplate" ref="hibernateTemplate" />
</b:bean>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
 

Add the bean definition and update the authentication-provider configuration to use our UserDetailsservice. Restart and Spring Security shall use our Custom UserDetails.


Troubleshooting

It took me a while before I could implement this, and though of sharing the mistakes, so that you don’t repeat them

Test User/Authority Hibernate class in isolation – I did it initially and my test suite was not complete as to detect that all the authorities were being set for the user. This let me to dig into the Spring Security code, add traces and realizing that it’s my mistake.

Ensure that you maintain roles with a prefix ROLE_ (case-sensitive). If you choose otherwise, you have to set the prefix as desired for authorization.