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

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.

[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.

[SMPP] Sending long SMS through SMPP

SMS is designed to send small text messages upto 140 chars. It actually depends upon the encoding. Using 7 bit encoding, you can send 160 chars, using Unicode, you can only send 70 chars. The data is carried in User Data section.

This post talks about ways of sending Long message using SMPP

Using SMPP we can send long SMS by two means

Using Payload option

Submit SM PDU has a field called message_payload. Set the data in it and you can send upto 64K octets. But this option is not supported by a lot of provider. Verify with your provider, if they support the same.

Using Message fragementation

This format is more common and widely supported. Essentially, the long SMS is divided into smaller messages and send with a particular format, by setting UDH.

The key TLV options used are

sar_msg_ref_num - The reference number for a particular concatenated short message

sar_total_segments - Indicates the total number of short messages within the concatenated short message

sar_segment_seqnum - Indicates the sequence number of a particular short message fragment within the concatenated short message

How do you set these, depends upon the SMPP library you use. Will brief the algo

 

  • Break the long sms into parts, so that fragement can fit into single Submit SM PDU
  • The total number of fragements shall give the value of sar_total_segments
  • Generate a unique identifier value, this becomes the values of sar_msg_ref_num
  • Now in a loop send the message fragements, with values in step above. sar_segment_seqnum values shall be the value of the loop count (ensure that it starts from 1)

This is it :-)

[MPXJ Series] Writing MS Project File using MPXJ

In the first part, we shall explore what are the different ways of writing a MS Project file using MPXJ.

Pre-requisite

The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download

 

Now let’s get back to the original intent of reading MPP files

Lets see the simplest form of reading an MPP file

try {
    ProjectWriter writer = ProjectWriterUtility.getProjectWriter("test.mpx");
    ProjectFile project = new ProjectFile();
 
    // Operations on Project File
    writer.write(project, "test.mpx");
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (IOException ioEx) {
    ioEx.printStackTrace();
}

ProjectWriterUtility.getProjectWriter(fileName), provides an appropriate Reader class, based on the version of MS Project file. This is the simplest way to create a writer for an Project file. ProjectFile is the class that contains details about the Schedule, we can iterate and extract information from it. We shall discuss the details on ProjectFile in subsequent posts. ProjectWriterUtility class checks for the file extension and returns an appropriate writer.

Alternative ways

  • For writing MPX file, you can use net.sf.mpxj.mpx.MPXWriter
  • For writing XML Project file, use net.sf.mpxj.mspdi. MSPDIWriter