Thursday, December 3, 2015

Grails Error Handling With Custom Http Status Code

First need to create a trait Groovy file as follows under src/groovy:

trait CustomExceptionHandler {
    /* Handle common exception */
    def handleException(Exception e) {
        handleCustomException(new CustomException(e.getMessage()))
    }

    /* Handle defined custom exception */
    def handleCustomException2(CustomException2 e) {
        handleCustomException(e)
    }

    /* Handle defined custom exception */
    def handleCustomException(CustomException e) {
        if (!CommonUtils.request) {
            // do something
        }
        else {
            CommonUtils.currentResponse.status = 220 (Custom response code)
            if(e.getStatusCode()) {
                CommonUtils.currentResponse.status = e.getStatusCode()
            }
            if (CommonUtils.request.xhr) {
                def error = [
                        type: "error",
                        message: e.getMessage()
                ]
                render error as JSON
            }
            else {
                render(view: "/error/error", model: [exception: e])
            }
        }
    }
}

Create a groovy class under src/groovy:

import org.springframework.web.context.request.RequestContextHolder
class CommonUtils {
    public static def getRequest() {
        def request = RequestContextHolder.getRequestAttributes()
        return request ? request.request : null
    }

    public static HttpServletResponse getCurrentResponse() {
        return WebUtils.retrieveGrailsWebRequest().getCurrentResponse()
    }
}

Exception Types (Reside in src/groovy):

class CustomException extends RuntimeException {

}
class CustomException2 extends CustomException {

}

How controller can use this feature:

class Controller1 implements CustomExceptionHandler {
    def index() {
        throw new Exception("Common exception")
    }

    def index2() {
        throw new CustomException("Defined custom exception")
    }

    def index3() {
        someService.index3()
    }

    def index4() {
        someService.index4()
    }
}

Exception thrown from service bahave same as from controller

class SomeService {
    def index3() {
        throw new Exception("Common exception")
    }

    def index4() {
        throw new CustomException("Defined custom exception")
    }
}

Java Code To Check Repeated Character In String

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class RepeatedCharacterCheck {
    public static void main (String[] args) throws java.lang.Exception {
        String stringToMatch = "abccdeeefenonn";
        Pattern p = Pattern.compile("(\\w)\\1+");
        Matcher m = p.matcher(stringToMatch);
        while (m.find()) {
            System.out.println("Duplicate character " + m.group(0));
        }
    }
}

Output

Duplicate character cc
Duplicate character eee
Duplicate character nn

Friday, October 16, 2015

Wednesday, October 14, 2015

HQL Use MySQL Functions By Implementing Custom Dialect

Datasource.groovy File


hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
    format_sql = false
    use_sql_comments = false
}
grails.cache.config = {
    cache {
        name 'org.hibernate.cache.UpdateTimestampsCache'
        eternal true
        maxEntriesLocalHeap 500
        persistence {
            strategy localTempSwap
        }
    }
}

/* environment specific settings */
environments {
    development {
        dataSource {
            pooled = true
            url = "jdbc:mysql://localhost/db_name?useUnicode=yes&characterEncoding=UTF-8"
            driverClassName = "com.mysql.jdbc.Driver"
            username = "root"
            password = ""
            dialect = "com.pkm.custom.CustomMySQLDialect"
            dbCreate = "update"
            properties {
                maxActive = 1000
                maxIdle = 100
                minIdle = 50
                initialSize = 1
                minEvictableIdleTimeMillis = 60000
                timeBetweenEvictionRunsMillis = 60000
                numTestsPerEvictionRun = 3
                maxWait = 10000
                testOnBorrow = true
                testWhileIdle = true
                testOnReturn = true
                validationQuery = "SELECT 1"
                minEvictableIdleTimeMillis = 1800000
                timeBetweenEvictionRunsMillis = 1800000
            }
            logSql = false
            loggingSql = false
        }
    }
    test {
        dataSource {
            
        }
    }
    production {
        dataSource {
            
        }
    }
}
log4j = {
    debug 'org.hibernate.SQL'
    trace 'org.hibernate.type.descriptor.sql.BasicBinder'
}

Custom Dialect (.groovy) Used In Datasource

import org.hibernate.dialect.MySQL5InnoDBDialect
import org.hibernate.dialect.function.SQLFunctionTemplate
import org.hibernate.dialect.function.StandardSQLFunction
import org.hibernate.type.IntegerType
import org.hibernate.type.StringType
import org.hibernate.type.TimestampType

class CustomMySQLDialect extends MySQL5InnoDBDialect {
    public CustomMySQLDialect() {
        super()

        /* convert_tz('2015-01-01', '+00:00', '+10:00') = '2015-01-01 10:00' */
        /* convert_tz('2015-01-01 00:00', '+00:00', '+10:00') = '2015-01-01 10:00' */
        /* convert_tz('2015-01-01 20:00', '+00:00', '-10:00') = '2015-01-01 10:00' */
        registerFunction("convert_tz", new StandardSQLFunction("convert_tz"))

        /* group_concat(name) = 'a,b,c,c,d' */
        registerFunction("group_concat", new StandardSQLFunction("group_concat", new StringType()))

        /* group_concat_unique(name) = 'a,b,c,d' */
        registerFunction("group_concat_unique", new SQLFunctionTemplate(new StringType(), "group_concat(DISTINCT ?1)"))

        /* date_add_interval('2015-01-01', DAY, 10) = '2015-01-11' */
        /* date_add_interval('2015-01-20', DAY, -10) = '2015-01-10' */
        registerFunction("date_add_interval", new SQLFunctionTemplate(TimestampType.INSTANCE, "date_add(?1, INTERVAL ?3 ?2)"))

        /* index_of('am', 'i am good') = 3 */
        /* index_of('asm', 'i am good') = 0 */
        registerFunction("index_of", new SQLFunctionTemplate(new IntegerType(), "LOCATE(?1, ?2)"))

        /* format(number, decimal_place, locale) = format(200.343, 2, 'en_AU') = '200.34' */
        registerFunction("format", new StandardSQLFunction("format", new StringType()))
    }

    @Override
    public String transformSelectString(String select) {
        select = super.transformSelectString(select);
        return select;
    }
}

Wednesday, September 30, 2015

Myob Cloud Integration: Use Filter In Myob Integration


Equal Method (String)
{domain}/{cf guid}/Sale/Invoice/?$filter=PaymentDetails/Method eq 'Cash'

Equal Method (UID)
{domain}/{cf guid}/Sale/Invoice/?$filter=Customer/UID eq guid'd61a6a86-453a-48bf-9402-6eb6b4ea23cf'

Equal Method (Boolean)
{domain}/{cf guid}/Contact/?$filter=IsIndividual eq true

Filtering in List
{domain}/{cf guid}/Contact/Customer/?$filter=Addresses/any(x: x/Email eq null) 

Equal Method (Date)
{domain}/{cf guid}/Purchase/Bill/?$filter=Date ge datetime'2014-07-01' and Date le datetime'2014-09-24'

Sunday, August 30, 2015

Grails Get/Use Http Session In Service Or Util Class

import org.codehaus.groovy.grails.web.util.WebUtils
import javax.servlet.http.HttpSession

HttpSession httpSession = WebUtils.retrieveGrailsWebRequest().request.session

Sunday, August 2, 2015

Grails :: How To Override g Message Tag

1. Create a tab lib (grails-app/taglib)

2. Extend with ValidationTagLib

3. Example

package com.test.taglib

import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib

class MyCustomTagLib extends ValidationTagLib {
 static validationTagLibStatic = null

    static namespace = "g"

    Closure message = { attrs ->
        ValidationTagLib validationTagLib = validationTagLibStatic ?: (validationTagLibStatic = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib'))
        validationTagLib.message.call(attrs)
    }
}

Friday, July 31, 2015

Grails Add New Resources To MessageSource Dynamically


First create a groovy file with the following contents

package com.pkm.message.test

import grails.util.Environment
import javax.servlet.ServletContext

class MessageSourceCreator {
    public static def grailsApplication = null
    public static def servletContext = null
    public static def messageSource = null
    private static final String MESSAGE_FILE_DIR = "/WEB-INF/resources/messages"

    /**
     * @param fileName "some.message.properties"
     */
    public static void addNewMessageFile(String fileName) {
        String basePath = servletContext.getRealPath("/")
        fileName = fileName.substring(0, fileName.lastIndexOf("."))
        String relativeFilePath = "${MESSAGE_FILE_DIR}/${fileName}"

        if (Environment.isWarDeployed()) {
            messageSource.pluginBaseNames.add(0, relativeFilePath)
        } 
        else {
            String separator = Environment.current == Environment.TEST ? "/" : ""
            messageSource.pluginBaseNames.add(0, basePath + separator + relativeFilePath)
        }
    }

    public static void initialize(def ga, def sc, def ms) {
        grailsApplication = ga
        servletContext = sc
        messageSource = ms
    }
}



From BootStrap, need to call first initialize & then addNewMessageFile


/**
 * From BootStrap, need to call first initialize & then addNewMessageFile
 */
import com.pkm.message.test.MessageSourceCreator

class BootStrap {
    def grailsApplication
    def servletContext
    def messageSource

    def init = { servletContext ->
        MessageSourceCreator.initialize(grailsApplication, servletContext, messageSource)
        MessageSourceCreator.addNewMessageFile("some.message.properties")
    }

    def destroy = {

    }
}

Friday, June 26, 2015

Thursday, June 4, 2015

Android: Update GUI field vlaue/display or show a message (toast) from Thread

Util Class


package com.pkm.android.utils.thread;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class ThreadUtils {
    public static Object context = null;
    private static Activity activity;
 
    /**
     * setCurrentActivity(this) in onStart()  on each activity 
     * setCurrentActivity(this) in onResume()  on each activity
     * setCurrentActivity(null) in onPause()  on each activity 
     */
    public static void setCurrentActivity(Activity currentActivity) {
        activity = currentActivity;
    }

    public static Activity currentActivity() {
        return activity;
    }
 
    public static void message(final String message) {
        activity.runOnUiThread(new Runnable() {
            public void run() {
             Toast.makeText((Context) context, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Example Usage


@Override
protected void onStart() {
    super.onStart();
    ThreadUtils.setCurrentActivity(this);
    ThreadUtils.context = getBaseContext();
}

ThreadUtils.message("A message from thread, will normally fail!!!");

Java Implementation of String Next Sequence


package time.zone;

import java.util.ArrayList;
import java.util.List;

public class JavaNextCharacter {
    public static void main (String[] args) throws Exception {
        List<String> codes = new ArrayList<String>();
        long started = System.currentTimeMillis();
        String s1 = "IjKl", s2 = "P.R.I.T.O.M", s3 = "{AB374}", s4 = "{ZZ}{U8U}", s5 = "Tp9";
        for (int i = 0; i < 20000; i++) {            
            System.out.println(formatAsLength(("#" + (i + 1)), 10) + 
                    formatAsLength(s1, 7) + " " + 
                    formatAsLength(s2, 12) + " " + 
                    formatAsLength(s3, 10) + " " + 
                    formatAsLength(s4, 12) + " " + 
                    formatAsLength(s5, 10));
            
            if (codes.contains(s1)) {
                throw new Exception("Duplicate code#1: " + s1 + " after generating " + codes.size() + " codes");
            }
            else if (codes.contains(s2)) {
                throw new Exception("Duplicate code#2: " + s2 + " after generating " + codes.size() + " codes");
            }
            else if (codes.contains(s3)) {
                throw new Exception("Duplicate code#3: " + s3 + " after generating " + codes.size() + " codes");
            }
            else if (codes.contains(s4)) {
                throw new Exception("Duplicate code#4: " + s4 + " after generating " + codes.size() + " codes");
            }
            else if (codes.contains(s5)) {
                throw new Exception("Duplicate code#5: " + s5 + " after generating " + codes.size() + " codes");
            }
            
            codes.add(s1);
            codes.add(s2);
            codes.add(s3);
            codes.add(s4);
            codes.add(s5);
            
            s1 = next(s1);
            s2 = next(s2);
            s3 = next(s3);
            s4 = next(s4);
            s5 = next(s5);
        }
        System.out.println("Total time taken: " + (System.currentTimeMillis() - started) + " milli to generate"
                + " " + codes.size() + " codes");
    }
 
    public static String next(String text) {
        int len = text.length();
        if (len == 0) {
            return text;
        }
        boolean alphaNum = false;
        int alphaNumPos = -1;
        for (char c : text.toCharArray()) {
            alphaNumPos++;
            if (Character.isDigit(c) || Character.isLetter(c)) {
                alphaNum = true;
                break;
            }
        }
        StringBuilder buf = new StringBuilder(text);
        if (!alphaNum || alphaNumPos == 0 || alphaNumPos == len) {
            next(buf, buf.length() - 1, alphaNum);
        }
        else {
            String prefix = text.substring(0, alphaNumPos);
            buf = new StringBuilder(text.substring(alphaNumPos));
            next(buf, buf.length() - 1, alphaNum);
            buf.insert(0, prefix);
        }
        return buf.toString();
    }
 
    private static void next(StringBuilder buf, int pos, boolean alphaNum) {
        if (pos == -1) {
            char c = buf.charAt(0);
            String rep = null;
            if (Character.isDigit(c))
                rep = "1";
            else if (Character.isLowerCase(c))
                rep = "a";
            else if (Character.isUpperCase(c))
                rep = "A";
            else
                rep = Character.toString((char) (c + 1));
            buf.insert(0, rep);
            return;
        }

        char c = buf.charAt(pos);
        if (Character.isDigit(c)) {
            if (c == '9') {
                buf.replace(pos, pos + 1, "0");
                next(buf, pos - 1, alphaNum);
            } 
            else {
                buf.replace(pos, pos + 1, Character.toString((char)(c + 1)));
            }
        }
        else if (Character.isLowerCase(c)) {
            if (c == 'z') {
                buf.replace(pos, pos + 1, "a");
                next(buf, pos - 1, alphaNum);
            } 
            else {
                buf.replace(pos, pos + 1, Character.toString((char)(c + 1)));
            }
        } 
        else if (Character.isUpperCase(c)) {
            if (c == 'Z') {
                buf.replace(pos, pos + 1, "A");
                next(buf, pos - 1, alphaNum);
            } 
            else {
                buf.replace(pos, pos + 1, Character.toString((char)(c + 1)));
            }
        } 
        else {
            if (alphaNum) {
                next(buf, pos - 1, alphaNum);
            } 
            else {
                if (c == Character.MAX_VALUE) {
                    buf.replace(pos, pos + 1, Character.toString(Character.MIN_VALUE));
                    next(buf, pos - 1, alphaNum);
                } 
                else {
                    buf.replace(pos, pos + 1, Character.toString((char)(c + 1)));
                }
            }
        }
    }
    
    public static String formatAsLength(String text, Integer minLength) {
        return String.format("%-" + minLength + "s", text);
    }
}



#1        IjKl    P.R.I.T.O.M  {AB374}    {ZZ}{U8U}    Tp9       
#2        IjKm    P.R.I.T.O.N  {AB375}    {ZZ}{U8V}    Tq0       
#3        IjKn    P.R.I.T.O.O  {AB376}    {ZZ}{U8W}    Tq1       
#4        IjKo    P.R.I.T.O.P  {AB377}    {ZZ}{U8X}    Tq2       
#5        IjKp    P.R.I.T.O.Q  {AB378}    {ZZ}{U8Y}    Tq3       
#6        IjKq    P.R.I.T.O.R  {AB379}    {ZZ}{U8Z}    Tq4       
#7        IjKr    P.R.I.T.O.S  {AB380}    {ZZ}{U9A}    Tq5       
#8        IjKs    P.R.I.T.O.T  {AB381}    {ZZ}{U9B}    Tq6       
#9        IjKt    P.R.I.T.O.U  {AB382}    {ZZ}{U9C}    Tq7       
#10       IjKu    P.R.I.T.O.V  {AB383}    {ZZ}{U9D}    Tq8       
#11       IjKv    P.R.I.T.O.W  {AB384}    {ZZ}{U9E}    Tq9       
#12       IjKw    P.R.I.T.O.X  {AB385}    {ZZ}{U9F}    Tr0       
#13       IjKx    P.R.I.T.O.Y  {AB386}    {ZZ}{U9G}    Tr1       
..................................
#19918    JmWm    P.R.J.X.A.N  {AV291}    {AAC}{T4V}   CSf6      
#19919    JmWn    P.R.J.X.A.O  {AV292}    {AAC}{T4W}   CSf7      
#19920    JmWo    P.R.J.X.A.P  {AV293}    {AAC}{T4X}   CSf8      
#19921    JmWp    P.R.J.X.A.Q  {AV294}    {AAC}{T4Y}   CSf9      
#19922    JmWq    P.R.J.X.A.R  {AV295}    {AAC}{T4Z}   CSg0      
#19923    JmWr    P.R.J.X.A.S  {AV296}    {AAC}{T5A}   CSg1      
#19924    JmWs    P.R.J.X.A.T  {AV297}    {AAC}{T5B}   CSg2      
#19925    JmWt    P.R.J.X.A.U  {AV298}    {AAC}{T5C}   CSg3      
#19926    JmWu    P.R.J.X.A.V  {AV299}    {AAC}{T5D}   CSg4      
#19927    JmWv    P.R.J.X.A.W  {AV300}    {AAC}{T5E}   CSg5      
#19928    JmWw    P.R.J.X.A.X  {AV301}    {AAC}{T5F}   CSg6      
#19929    JmWx    P.R.J.X.A.Y  {AV302}    {AAC}{T5G}   CSg7      
#19930    JmWy    P.R.J.X.A.Z  {AV303}    {AAC}{T5H}   CSg8      
#19931    JmWz    P.R.J.X.B.A  {AV304}    {AAC}{T5I}   CSg9      
#19932    JmXa    P.R.J.X.B.B  {AV305}    {AAC}{T5J}   CSh0      
#19933    JmXb    P.R.J.X.B.C  {AV306}    {AAC}{T5K}   CSh1      
#19934    JmXc    P.R.J.X.B.D  {AV307}    {AAC}{T5L}   CSh2      
#19935    JmXd    P.R.J.X.B.E  {AV308}    {AAC}{T5M}   CSh3      
#19936    JmXe    P.R.J.X.B.F  {AV309}    {AAC}{T5N}   CSh4      
#19937    JmXf    P.R.J.X.B.G  {AV310}    {AAC}{T5O}   CSh5      
#19938    JmXg    P.R.J.X.B.H  {AV311}    {AAC}{T5P}   CSh6      
#19939    JmXh    P.R.J.X.B.I  {AV312}    {AAC}{T5Q}   CSh7      
#19940    JmXi    P.R.J.X.B.J  {AV313}    {AAC}{T5R}   CSh8      
#19941    JmXj    P.R.J.X.B.K  {AV314}    {AAC}{T5S}   CSh9      
#19942    JmXk    P.R.J.X.B.L  {AV315}    {AAC}{T5T}   CSi0      
#19943    JmXl    P.R.J.X.B.M  {AV316}    {AAC}{T5U}   CSi1      
#19944    JmXm    P.R.J.X.B.N  {AV317}    {AAC}{T5V}   CSi2      
#19945    JmXn    P.R.J.X.B.O  {AV318}    {AAC}{T5W}   CSi3      
#19946    JmXo    P.R.J.X.B.P  {AV319}    {AAC}{T5X}   CSi4      
#19947    JmXp    P.R.J.X.B.Q  {AV320}    {AAC}{T5Y}   CSi5      
#19948    JmXq    P.R.J.X.B.R  {AV321}    {AAC}{T5Z}   CSi6      
#19949    JmXr    P.R.J.X.B.S  {AV322}    {AAC}{T6A}   CSi7      
#19950    JmXs    P.R.J.X.B.T  {AV323}    {AAC}{T6B}   CSi8      
#19951    JmXt    P.R.J.X.B.U  {AV324}    {AAC}{T6C}   CSi9      
#19952    JmXu    P.R.J.X.B.V  {AV325}    {AAC}{T6D}   CSj0      
#19953    JmXv    P.R.J.X.B.W  {AV326}    {AAC}{T6E}   CSj1      
#19954    JmXw    P.R.J.X.B.X  {AV327}    {AAC}{T6F}   CSj2      
#19955    JmXx    P.R.J.X.B.Y  {AV328}    {AAC}{T6G}   CSj3      
#19956    JmXy    P.R.J.X.B.Z  {AV329}    {AAC}{T6H}   CSj4      
#19957    JmXz    P.R.J.X.C.A  {AV330}    {AAC}{T6I}   CSj5      
#19958    JmYa    P.R.J.X.C.B  {AV331}    {AAC}{T6J}   CSj6      
#19959    JmYb    P.R.J.X.C.C  {AV332}    {AAC}{T6K}   CSj7      
#19960    JmYc    P.R.J.X.C.D  {AV333}    {AAC}{T6L}   CSj8      
#19961    JmYd    P.R.J.X.C.E  {AV334}    {AAC}{T6M}   CSj9      
#19962    JmYe    P.R.J.X.C.F  {AV335}    {AAC}{T6N}   CSk0      
#19963    JmYf    P.R.J.X.C.G  {AV336}    {AAC}{T6O}   CSk1      
#19964    JmYg    P.R.J.X.C.H  {AV337}    {AAC}{T6P}   CSk2      
#19965    JmYh    P.R.J.X.C.I  {AV338}    {AAC}{T6Q}   CSk3      
#19966    JmYi    P.R.J.X.C.J  {AV339}    {AAC}{T6R}   CSk4      
#19967    JmYj    P.R.J.X.C.K  {AV340}    {AAC}{T6S}   CSk5      
#19968    JmYk    P.R.J.X.C.L  {AV341}    {AAC}{T6T}   CSk6      
#19969    JmYl    P.R.J.X.C.M  {AV342}    {AAC}{T6U}   CSk7      
#19970    JmYm    P.R.J.X.C.N  {AV343}    {AAC}{T6V}   CSk8      
#19971    JmYn    P.R.J.X.C.O  {AV344}    {AAC}{T6W}   CSk9      
#19972    JmYo    P.R.J.X.C.P  {AV345}    {AAC}{T6X}   CSl0      
#19973    JmYp    P.R.J.X.C.Q  {AV346}    {AAC}{T6Y}   CSl1      
#19974    JmYq    P.R.J.X.C.R  {AV347}    {AAC}{T6Z}   CSl2      
#19975    JmYr    P.R.J.X.C.S  {AV348}    {AAC}{T7A}   CSl3      
#19976    JmYs    P.R.J.X.C.T  {AV349}    {AAC}{T7B}   CSl4      
#19977    JmYt    P.R.J.X.C.U  {AV350}    {AAC}{T7C}   CSl5      
#19978    JmYu    P.R.J.X.C.V  {AV351}    {AAC}{T7D}   CSl6      
#19979    JmYv    P.R.J.X.C.W  {AV352}    {AAC}{T7E}   CSl7      
#19980    JmYw    P.R.J.X.C.X  {AV353}    {AAC}{T7F}   CSl8      
#19981    JmYx    P.R.J.X.C.Y  {AV354}    {AAC}{T7G}   CSl9      
#19982    JmYy    P.R.J.X.C.Z  {AV355}    {AAC}{T7H}   CSm0      
#19983    JmYz    P.R.J.X.D.A  {AV356}    {AAC}{T7I}   CSm1      
#19984    JmZa    P.R.J.X.D.B  {AV357}    {AAC}{T7J}   CSm2      
#19985    JmZb    P.R.J.X.D.C  {AV358}    {AAC}{T7K}   CSm3      
#19986    JmZc    P.R.J.X.D.D  {AV359}    {AAC}{T7L}   CSm4      
#19987    JmZd    P.R.J.X.D.E  {AV360}    {AAC}{T7M}   CSm5      
#19988    JmZe    P.R.J.X.D.F  {AV361}    {AAC}{T7N}   CSm6      
#19989    JmZf    P.R.J.X.D.G  {AV362}    {AAC}{T7O}   CSm7      
#19990    JmZg    P.R.J.X.D.H  {AV363}    {AAC}{T7P}   CSm8      
#19991    JmZh    P.R.J.X.D.I  {AV364}    {AAC}{T7Q}   CSm9      
#19992    JmZi    P.R.J.X.D.J  {AV365}    {AAC}{T7R}   CSn0      
#19993    JmZj    P.R.J.X.D.K  {AV366}    {AAC}{T7S}   CSn1      
#19994    JmZk    P.R.J.X.D.L  {AV367}    {AAC}{T7T}   CSn2      
#19995    JmZl    P.R.J.X.D.M  {AV368}    {AAC}{T7U}   CSn3      
#19996    JmZm    P.R.J.X.D.N  {AV369}    {AAC}{T7V}   CSn4      
#19997    JmZn    P.R.J.X.D.O  {AV370}    {AAC}{T7W}   CSn5      
#19998    JmZo    P.R.J.X.D.P  {AV371}    {AAC}{T7X}   CSn6      
#19999    JmZp    P.R.J.X.D.Q  {AV372}    {AAC}{T7Y}   CSn7      
#20000    JmZq    P.R.J.X.D.R  {AV373}    {AAC}{T7Z}   CSn8      
Total time taken: 74740 milli to generate 100000 codes

Wednesday, June 3, 2015

Validate URL Using Java & Regex


class UrlValidator {
    public static void main (String[] args) {
        check("http://google.com");
        check("http://google.com?a=b#99");
        check("http://google.com?a=b#99#sty");
        check("http://www.google.com?a=1&b=2 / 3");
        check("http://www.google.com?a=1&orderRate=orderRateUUID");
    }

    public static void check(String url) {
        String Regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

        Boolean matches = url.matches(Regex);

        if(matches) {
            System.out.println("URL (YES): " + url);
        }
        else {
            System.out.println("URL (NOT): " + url);
        }
    }
}


URL (YES): http://google.com
URL (YES): http://google.com?a=b#99
URL (YES): http://google.com?a=b#99#sty
URL (NOT): http://www.google.com?a=1&b=2 / 3
URL (YES): http://www.google.com?a=1&orderRate=orderRateUUID