Java tools « Intelligrape Groovy & Grails Blogs

Archive for the ‘ Java tools ’ Category

Normalization Forms for Accented Characters in java

Posted by on June 29th, 2012

Text Normalization is the process of “standardizing” text to a certain form, so as to enable, searching, indexing and other types of analytical processing on it. Often working with large quantities of text we encounter character with accents like é , â etc. Unicode provides multiple ways to create such characters . For example we can have é created using Unicode sequence \u00E9 (composite character) or we can create it using a combination of e + acute accent. that would be (e + \u0301).
Now the é created would look same in both the representations and would also mean the same thing, but for a java program they are actually not the same characters so the é created using the two methods are actually not equal for your program. Clearly we need to normalize these two different representations to a fixed standard.

And its here, that java.text.Normalizer class comes to our rescue. All we need to do is normalize things to a normalization form out of these 4 :

  1. NFC – Canonical Decomposition, followed by Canonical Composition.
  2. NFD – Canonical Decomposition
  3. NFKC – Compatibility Decomposition, followed by Canonical Composition
  4. NFKD – Compatibility Decomposition



Canonical Decomposition means, taking a character and decomposing it into its component characters



Compatibility decomposition means taking a character and decomposing it by compatibility and arranging them in specific order


Canonical Composition means recomposing characters based on their canonical equivalence.


Canonical equivalence further means that characters have the same appearance and meaning when printed or displayed.


To Fully summarize this in an example,

Consider, the Angstrom sign “Å”, (U+212B) and  the Swedish letter “Å” (U+00C5), both are expanded by NFD (or NFKD) into “A” and “°” (U+0041 and U+030A) which is then reduced by NFC (or NFKC) to the Swedish letter “Å” (U+00C5)  (Swedish Letter “Å” is canonically equivalent to Angstrom sign “Å” as they are printed and displayed as exactly same, though they are different).

Now we know how we can normalize unicode characters to a standard form wherever required.



Sachin Anand

@babasachinanand

sachin[at]intelligrape[dot]com

Posted in Java tools

Normalizing Accented Words

Posted by on June 14th, 2012

We all often need to work on data aggregated together from different sources, and before we analyse it, we often need to normalize it to a certain standard, A normalization process typically includes removing special characters, converting all text to lower case , We can also have certain rules that words like “saint” will always be normalized to “st.” etc.

An important part of such normalization is to account for ‘accented’ characters like é or è , and generally you would want them to normalize to normal English alphabet ‘e’, as that would help in sorting/searching words containing these characters. For eg : you would want “Indianapòlis” to be normalized to “Indianapolis”.

We can achieve this by using java.text.Normalizer class, all we need to do is

Normalizer.normalize("Indianapòlis", Normalizer.Form.NFKD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "")

Lets understand what is happening here, clearly we are calling a static function normalize in java.text.Normalizer class, the first parameter we passed is the string we want to normalize, the second parameter is the normalization form. There are 4 normalization forms

  1. NFC – Canonical Decomposition, followed by Canonical Composition.
  2. NFD – Canonical Decomposition
  3. NFKC – Compatibility Decomposition, followed by Canonical Composition
  4. NFKD – Compatibility Decomposition

So in the second parameter we pass in the NFKD form, which is an enum of type Form.

The normalizer function will still return us “Indianapòlis”. so, what happened there?


Lets understand, we can create an ò using two ways. It can either be a unicode character (U+00F2) or it can be a normal english ‘o’ with a grave accent added to it (o+ (U+0060)).

What normalizer function did was it normalized both cases to english ‘o’ with an accent (o+ (U+0060))

The next part is a replaceAll with a regex, the {InCombiningDiacriticalMarks} is a Unicode block property, which matches the accented characters.

The second part replaces all accented characters (grave accent (U+0060) in this case) with empty string. So what we finally get is “Indianapolis”, and we are done.


Hope it helped!

Sachin Anand

sachin[at]intelligrape[dot]com

@babasachinanand

http://in.linkedin.com/in/sachinanand30

Posted in Groovy, Java tools

Integrating Google plus in grails application

Posted by on December 6th, 2011

In my current project, i needed to integrate Google+ in the application using server-side API. Google uses OAuth2.0 protocol for authorization when our application tries to access the data. All we require is an access token to fetch data from Google using REST calls which serves data in JSON format.

I implemented it using Web Server Applications API and thought it worth sharing.



There are basically 3 steps to fetch data from Google.



1. Register an application.

We need to register an application at Google API Console. Go to the Google console page using the link provided and create an aplication.



Steps involve in registering an application are:-



a.) Create project by providing name to the project.





b.) Turn ON the service required, in our case it is Google Plus API.





c.) Create a Oauth 2.0 Client ID.





d.) Create the OAuth 2.0 ClientId and provide the callback URL where google will send the authorization token.





e.) Note down Client ID and Client Secret as generated in the above step.





2. Obtain an Access Token from the Google Authorization Server.



Obtaining an access token involves 2 steps.



a.) Request for Authorization Code.



In this step, we will request the Google server for authorization code by providing registered application client ID in the URL to Google server.

I created an action to redirect to Goolge, when someone want to connect to google plus.

String CLIENT_ID =client_id_obtained from registered app
String CALLBACK_URL = callback_url_as_mentioned in the registered app.
String GOOGLE_PLUS_SCOPE='https://www.googleapis.com/auth/plus.me'    // scope is the permissions we are requesting.


Action code is as:

def registerOnGooglePlus = {
String authorizeUrl = "https://accounts.google.com/o/oauth2/auth?scope=${GOOGLE_PLUS_SCOPE}&
redirect_uri=${CALLBACK_URL}&response_type=code&client_id=${CLIENT_ID}&access_type=offline"
URL urlForGooglePlus = new URL(authorizeUrl)
redirect(url: urlForGooglePlus)
}

The Redirect will take user to permissions page, if the user is already logged-in or will take to login page and then permissions page.

After approving the required permissions, user will redirect back to the application’s registered Callback URL with the authorization code.





b.) Request for Access Token with the authorization code obtained from the above action.



Access Token can be received by a POST request using the Client Secret and authorization code received.

The POST call requires 5 properties to be send in the body of the request in the encoded form.


	code : The authorization code returned from the initial request
	client_id : The client_id obtained during application registration
	client_secret : The client secret obtained during application registration
	redirect_uri : The URI registered with the application 
	grant_type : authorization_code

// Sample action to receive authorization code

def callBack={
StringBuilder sb = new StringBuilder("code=");
sb.append(URLEncoder.encode(code, "UTF-8"));
sb.append("&client_id=");
sb.append(URLEncoder.encode(clientId, "UTF-8"));
sb.append("&client_secret=");
sb.append(URLEncoder.encode(clientSecret, "UTF-8"));
sb.append("&redirect_uri=");
sb.append(URLEncoder.encode(callbackUrl, "UTF-8"));
sb.append("&grant_type=");
sb.append(URLEncoder.encode('authorization_code', "UTF-8"));

String URL_TO_REQUEST_TOKEN= 'https://accounts.google.com/o/oauth2/token'

URL url = new URL(URL_TO_REQUEST_TOKEN);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + sb.toString().length());
connection.setRequestProperty("Host", "accounts.google.com");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(sb.toString());
outputStreamWriter.flush();
log.debug("Response code ${connection.responseCode} , Message : ${connection.responseMessage}")
String resultData = connection.content.text
def responseJson = JSON.parse(resultData)
String ACCESS_TOKEN = responseJson?.access_token
}
catch (Exception e) {
e.printStackTrace()
}
}


3. Calling Google API.

Now, with the help of access token, we can call google API to fetch Data by appending the access token in the GET request.

Example:



To fetch person’s profile data

GET :  https://www.googleapis.com/oauth2/v1/userinfo?access_token=ACCESS_TOKEN 


To get list of profile acitivties

 GET :  https://www.googleapis.com/plus/v1/people//activities/public?access_token=ACCESS_TOKEN


References:-
http://code.google.com/apis/accounts/docs/OAuth2WebServer.html

http://code.google.com/apis/accounts/docs/OAuth2.html



Hope this helps..!!!



Vishal Sahu
vishal[at]intelligrape[dot]com
www.intelligrape.com

How to get Google Indexed Pages Count, and/or do Google searches through AJAX/programmatically

Posted by on September 27th, 2011

Hi,
Recently i had to get the count of the Pages Google has Indexed for a perticular web Site progmatically.
In the process i found that Google offers an AJAX Search API to perform Google Searches.
.
Hence i Implemented a Class which does the same for me easily(Ajax Search in Google).
And this Solution Does Google Searches programmatically and Also Give us the Indexed Pages Count.
Note: Its Groovy Implementation, Similar can be done in JavaScript using Ajax though intent will remain the same.
The class has been named GoogleAjaxSearch because it uses the Google AJAX Search API ;)

Use Cases Are As Follows:

//Case 1 Simple Search
String searchQuery = "my search Query"
GoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)
println googleAjaxSearch.results //Type List<Expando>, Just print it to see available properties

//Case 2: Get Number Of Indexed Pages in Google
String searchQuery = "site:intelligrape.com"
GoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)
println googleAjaxSearch.estimatedResultCount

//Case 3: Redo a search with same SearchQuery or a different Search Query
String searchQuery = "search string"
GoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)
googleAjaxSearch.redo() //Re-Search with same query
googleAjaxSearch.redo("New Query") // Re-Search With New Query

//Case 4: All Details Available
String searchQuery = "site:intelligrape.com"
GoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)
println googleAjaxSearch.estimatedResultCount //Gives the result count, indexed pages count
println googleAjaxSearch.currentPageIndex    //Search result server side pagination - current page
println googleAjaxSearch.results  //List<Expando> containing results
println googleAjaxSearch.responseStatus //HTTP Status Code
println googleAjaxSearch.responseDetails //Details for response, if any
println googleAjaxSearch.searchQuery     //Search Query 
println googleAjaxSearch.moreResultsUrl // Url to hit for next Page of results
println googleAjaxSearch.jsonResult // Complete raw JSON Result from google
println googleAjaxSearch.pages  // Pagination Pages Details

The Class Code Is As Follows:


package myPackage.googleSearch

import grails.converters.JSON

class GoogleAjaxSearch {

    public String searchQuery
    public String responseDetails
    public String moreResultsUrl
    public String jsonResult

    public Integer responseStatus
    public Integer currentPageIndex
    public Integer estimatedResultCount

    public def pages

    public List<Expando> results = []

    private static final String ajaxSearchTarget = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=###SEARCHQUERY###&filter=0"

    public GoogleAjaxSearch(String searchQuery) {
        this.searchQuery = searchQuery
        search()
    }

    public GoogleAjaxSearch() {
        this(null)
    }

    public void redo() {
        search()
    }

    public void redo(String newSearchQuery) {
        this.searchQuery = newSearchQuery
        search()
    }

    private void search() {
        if (searchQuery) {
            URL url = new URL(ajaxSearchTarget.replace('###SEARCHQUERY###', searchQuery))
            URLConnection connection = url.openConnection()
            connection.setDoInput(true)
            InputStream inStream = connection.getInputStream()
            BufferedReader searchResultContent = new BufferedReader(new InputStreamReader(inStream))
            jsonResult = searchResultContent.getText()
            parseJsonAndPopulateObject()
        }
    }

    private void parseJsonAndPopulateObject() {
        def jsonArray = JSON.parse(jsonResult)
        this.responseStatus = Integer.parseInt(jsonArray.responseStatus as String, 10)
        this.responseDetails = jsonArray.responseDetails
        this.moreResultsUrl = jsonArray.responseData.cursor.moreResultsUrl
        this.currentPageIndex = Integer.parseInt(jsonArray.responseData.cursor.currentPageIndex as String, 10)
        this.pages = jsonArray.responseData.cursor.pages
        this.estimatedResultCount = Integer.parseInt(jsonArray.responseData.cursor.estimatedResultCount as String, 10)
        results = jsonArray.responseData.results.collect {
            new Expando(
                    content: it.content,
                    GsearchResultClass: it.GsearchResultClass,
                    titleNoFormatting: it.titleNoFormatting,
                    title: it.title,
                    cacheUrl: it.cacheUrl,
                    unescapedUrl: it.unescapedUrl,
                    url: it.url,
                    visibleUrl: it.visibleUrl
            )
        }
    }
}

Hope That Helps :)
Regards
Kushal Likhi

Annotation for checking required session fields

Posted by on September 21st, 2011

Recently I worked on a project where I used spring security plugin. Its a very wonderful plugin for making your application secured from unauthorized users. It gives you a simple annotation @Secured to add security to your action and controller. Thats the first time I got to know the real use case of annotation. So I started reading about annotation and few days later I found the use case to implement my own annotation.

All the projects I have worked on had login functionality where we put the userId and projectId into the session. Then in my code I use to get the user from session.userId. Something like

def books = {
   User user = User.get(session.userId)
   Project project = Project.get(session.projectId)
   ......
   .....
}  

The above code fails when user directly hits this action because there is no check to verify the user is not null. The simple answer for this problem is either use beforeInterceptor or filters. So we started checking the session.userId in filters. But again there are cases where you dont want to check this session value or you can say there are public urls as well. Now we have to put few if else statements in filter.

Here I got my use case to implement an annotation for controllers and actions which checks for the required fields in the session before getting into the action. So I created an annotation in src/groovy folder

import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target

@Target([ElementType.FIELD, ElementType.TYPE]) // Annotation is for actions as well as controller so target is field and for class
@Retention(RetentionPolicy.RUNTIME) // We need it at run time to identify the annotated controller and action
@interface RequiredSession {
    String[] exclude() default [] // To exclude some of the actions of controller

    String[] fields() default ["userId","projectId"] // The default value is set to userId and projectId that can be overridden while using the   annotation on controller or action.

    String onFailController() default "home" // Default controller when the field not in session is set to index page

    String onFailAction() default "index" // Default action when the field not in session is set to index page
}

Now I created a ApplicationFilters and before redirected the request to any action I check for the condition of session fields if the requested action or controller is annotated. The code in the filter is something like

class ApplicationFilters {
    def filters = {
        validateSession(controller: '*', action: '*') {
            before = {
                if (controllerName) {
                    
//Get the instance of controller class from string value i.e; controllerName
                    def controllerClass = grailsApplication.controllerClasses.find {it.logicalPropertyName == controllerName}
                    
//Read the RequiredSession annotation from controller class  
                    def annotation = controllerClass.clazz.getAnnotation(RequiredSession)
                   
//Get the current action from actionName otherwise read default action of controller    
                    String currentAction = actionName ?: controllerClass.defaultActionName
                   
//Look for the annotation on action if controller is not annotated or the action name is excluded
                    if (!annotation || currentAction in annotation.exclude()) {

//Get the action field from string value i.e; currentAction
                        def action = applicationContext.getBean(controllerClass.fullName).class.declaredFields.find { field -> field.name == currentAction }
//If action is found get the annotation else set it to null 
                        annotation = action ? action.getAnnotation(RequiredSession) : null
                    }
                    
//Check for the field in session whether the are null or not if any of the field is null loginFailed is true  
                    boolean loginFailed = annotation ? (annotation.fields().any {session[it] == null}) : false

                    if (loginFailed) {

// If login is failed user redirected to on fail action and controller
                        redirect(action: annotation.onFailAction() , controller: annotation.onFailController())
                        return false;
                    }
                }
            }

        }
    }
}


And its all done. Now we just annotate our controller and actions accordingly.

@RequiredSession(exclude = ["registration", "joinProject"])
class UserController {
      def edit ={}
      def update = {}
      def list ={}
      def save ={}

      def registration ={}
      def joinProject = {}
}

In above example registration and joinProject action will bypass the session fields check.

@RequiredSession
class ItemController {
        def index={}
        def buy ={}
        def save ={}
} 

All the action of above examples can be accessed only when user is logged in.

class HomeController {
      def index ={}
      def aboutUs={}
      @RequiredSession
      def dashboard = {}
}

Actions other than dashboard are public actions which can be accessed without login.

class UserController {
  @RequiredSession(fields = ["loggedInUserId"])
   def updatePassword = {
    
   }
}

For updating password user dont need to have some project into session so we specified the fields to be checked in session.

Hope it helps
Uday Pratap Singh
uday@intelligrape.com
https://twitter.com/meudaypratap
http://in.linkedin.com/in/meudaypratap

Setting System property from command line in grails

Posted by on August 24th, 2011

Hi,

In one of my recent grails project, i needed to set System property from command line while running the grails application.
I looked for it and found a simple solution to do so and found it worth sharing.


Suppose we want to set any property, say app.property=”propertyValue”, then the command to set the property in grails application would be:

  grails  -Dapp.property="propertyValue" run-app

Similarly to retrieve the value of System property, we use

String myPropertyValue= System.getProperty("app.property")

It helped me a lot..
Hope it helps


Vishal Sahu
vishal@intelligrape.com
www.intelligrape.com

Tags: ,
Posted in Grails, Java tools, Linux, System

Inject custom validation errors in object

Posted by on August 18th, 2011

Reading the grails docs is like my habit, they always enhances your learning. Today I was going through the grails docs again and I found a very good way of showing some custom error messages. As I saw it I found the use case in one of my project that could be refactored by this approach

There are use cases where we need to show some errors which are beyond your domain / command object constraints validators for these cases you can inject your custom error messages to the object take an example of updating the password. Any of the field is not domain related so we have to create a command object like

class UpdatePasswordCO {

    String password
    String newPassword
    String confirmNewPassword

    static constraints = {
        password(nullable: false, blank: false)
        newPassword(nullable: false, blank: false)
        confirmNewPassword(nullable: false, blank: false, validator: {val,   obj ->
            if (obj.newPassword != val) {
                "confirmPassword.mismatch.newPassword"
            }
        })
    }
}

Now you have to check whether the logged in user entered the correct password or not. So rather than putting the message in flash scope you can directly put this error to the command object it self

def updatePassword = {UpdatePasswordCO updatePasswordCO ->
        if (updatePasswordCO.validate()) {
            if (User.loggedInUser.validatePassword(updatePasswordCO.password)) {
                flash.message = "Password updated successfully"
            } else {
                updatePasswordCO.errors.rejectValue("password", "password.mismatch.current.password")
            }
        }
        if (updatePasswordCO.hasErrors()) {
            render(view: 'profile', model: [updatePasswordCO: updatePasswordCO])
        } else {
            redirect(action: 'profile')
        }
}

So in above approach you have injected a new error on password field with a code. By doing this your hasErrors tag on gsp page will show your custom error as well. For more details you can refer Spring

Hope it helps
Uday Pratap Singh
uday@intelligrape.com
https://twitter.com/meudaypratap
http://in.linkedin.com/in/meudaypratap

Generating EAN-8 standard barcode using Barcode4J

Posted by on April 14th, 2011

Hi,

In one of my project i needed to generate EAN-8 standard bar-code for identifying the various products. I searched for various libraries available for generating bar-code and found a library to do so. The library i used for generating the bar-code is Barcode4J.
You can download it from here

Barcode4J is a flexible generator for barcodes written in Java. It’s free, available under the Apache License, version 2.0.
The bar-code generated with the help of of this library uses eight digit number hence EAN-8.

To generate bar-code image from this library is quite simple. Just put the barcode4j.jar in the lib folder of the application.


Code to generate image is:-

public File generateBarcode(Long codeDigits){
try {
      //'codeDigits' is the code for which we want to generate bar-code image
     EAN8Bean bean = new EAN8Bean();
     final int dpi = 150;
     bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
     bean.setFontSize(2.0)
     bean.doQuietZone(true)
     File outputFile = new File(filepath) // existing file in the file system
     OutputStream out = new FileOutputStream(outputFile)

     // class to convert provided image into barcode image
     BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,   BufferedImage.TYPE_BYTE_BINARY, false, 0)
     bean.generateBarcode(canvas, codeDigits)
     canvas.finish()
     return outputFile
}

So, here it takes a file from the file system and convert it into the required barcode image. Now, the user can simply display this image on the items.

In my case, i needed to generate a bar-code image for each product, so i create new file for every item, and stored it on the file system with unique name.

This works in my case.
Hope it helps


Cheers..!!!
Vishal Sahu
vishal@intelligrape.com

Tags:

Paypal Integration : Auto redirecting to application after processing payment

Posted by on March 15th, 2011

Hi,

In my recent grails project, i needed to return back to the application after processing payment at the paypal website. I searched a lot about it. Then i found the solution to do so and thought it worth sharing.

Here are the steps i followed:-

1. The Paypal button:-
This is the code for putting the paypal button in any web-page

Here:
The email of merchant’s account or the seller account, the account to which payment is to be made and the email by which the account is registered is given in the hidden field “business”.

 

The URL to which we want application to return after processing payment is provided in the hidden field with the name “return” i.e.


This completes the application side implementation for paypal.

2. Paypal Account Configuration
Now we have to configure the Merchant’s account to Auto-redirect to the provided URL after processing the payment.

Steps to configure account are:-

1. Log in to Paypal Account
2. Click the Profile Link in sub-menu of My Account.


3. Under “Selling Preferences” click ‘Website Payment Preferences’ link.

4. Select the Auto-Return radio button to enable the auto return feature.

This enables the auto-redirect feature in the paypal account.

This worked for me.
Hope it helps.

Vishal Sahu
vishal@intelligrape.com

Posted in Grails, Groovy, Java tools

Converting date from one timezone to another in groovy

Posted by on February 9th, 2011

Hi,
In my recent grails project, i came across the situation where i needed to convert the date in given timezone to the date in another timezone. I searched a lot about it and got many solutions for this problem and then i came out with a simple way to do so.

Lets i have a date in TimeZone say oldTimeZone and i want to convert it to another timeZone say newTimeZone, so to convert it to another timezone, i wrote the method given below.


public Date convertToNewTimeZone(Date date, TimeZone oldTimeZone, TimeZone newTimeZone){

      long oldDateinMilliSeconds=date.time - oldtimeZone.rawOffset
      // oldtimeZone.rawOffset returns the difference(in milliSeconds) of time in that timezone with the time in GMT
      // date.time returns the milliseconds of the date

      Date dateInGMT=new Date(oldDateinMilliSeconds)

      long convertedDateInMilliSeconds = dateInGMT.time + newTimeZone.rawOffset
      Date convertedDate = new Date(convertedDateInMilliSeconds)

    return convertedDate
}

This Works for me.
Hope it helps.

Cheers..!!!
Vishal Sahu
vishal@intelligrape.com
www.intelligrape.com