Ubuntu insights, Programming in groovy, java, et als!

Tuesday, September 15, 2015

Basic Authentication using Groovy

Here is a very minimal groovy based Basic Authentication Client which can come handy whilst talking to APIs.

def jsonBody = '{"day" : "11", "month" : "03", "year" : "2010", "hour" : "09", "min" : "08", "lat" : "18.9750", "lon" : "72.8258", "tzone" : "5.5", "gender" : "male"}'

new BasicAuthClient(
              userName:"xyzzy",
              password:"ba4d90e133ad76b103fcedcd00ab5681", 
              address:"https://api.vedicrishiastro.com/v1/basic_panchang/"
).doPost(jsonBody)

class BasicAuthClient{

    def userName, password, address, conn
    
    def getAuthHeader(){
        def authHeader = "$userName:$password".getBytes().encodeBase64().toString()
        println(authHeader)
        return authHeader
    }
    
    def getConnection(){
        this.conn = address.toURL().openConnection()
        conn.setDoOutput(true)
        conn.setRequestMethod("POST")
        conn.setRequestProperty("Authorization", "Basic ${getAuthHeader()}")
        return conn
    }
    
    def doPost(body){
        def out = getConnection().getOutputStream(); 
        out.write(body.getBytes()) 
        out.close();
        return getResponse()
    }
    
    def getResponse(){
        def responseCode = conn.getResponseCode();
        println(responseCode)
        def instream = responseCode < 207 ? conn.getInputStream() : conn.getErrorStream()        
        int i = instream.read()
        while (i != -1) {
            print((char)i)
            i = instream.read()
        } 
        instream.close()
        conn.disconnect()
    }
}      


    

Tuesday, June 16, 2015

ConEmu for developer productivity

For the last one and half years, I have been a loyal user of Console 2, an alternative to command prompt on windows. Now, I have moved on to ConEmu for better productivity at workplace. It comes with many settings for customization. What I really love in ConEmu is its split screen support. At times when one is even lazy to shift between tabs/windows. In conjunction with vim, it is a breeze.

Say, you have an executable jar that also produces a log file. To hell with notepad and other UI editors - You could simply split your ConEmu screen to open up two or more console windows and spare one to skim through the output log file (using vim) simultaneously as and when you run the executable through an other.


The only caveat that I see with conEmu is that it eats up a tad bit more of memory than Console2 does (around 40 MB). But I guess I can live with that considering the fact that my laptop is powered with 16 GB of RAM. :)

Wednesday, June 03, 2015

VIM - first things first

Install theme :

https://benaiah41.wordpress.com/2012/01/17/customizing-gvim-in-windows-7/

Append the following lines in ../.vimrc (Startup Settings)

set guifont=Consolas:h11:cDEFAULT
color wombat
set guioptions-=T
set nu
let g:netrw_liststyle=3