Wednesday, December 18, 2013

Third party calls (CORS), cookies and more...

1. Cookies set from the parent domain are by default not sent to the third party domain in xhr calls even if they from the common parent.

e.g. and

withCredential = true does the trick

2. Cookies set by the third party domain are not set on the client. The client ignores those headers.

Hibernate: OneToOne Mapping and fetch type

If a bean has a property that has mapping that is OneToOne mapping it is better to make the FetchType to EAGER.

EAGER FetchType will add to the main query.

LAZY makes the property to be loaded later, but is not the case with OneToOne mapping. After the bean is loaded all the OneToOne properties are loaded through a separate query if the FetchType is LAZY and will slow the loading.

Take away: OneToOne mapping FetchType.EAGER

Dojo: Download a file with GET/POST

There is a old trick to use iframe and its src to download a file. The same can be achieved through
the takes same parameter as xhrGet or xhrPost. And no iframe component is required!


xhrArgs = {
  url: "service endpoint",
  form: "formid",
  content: ""

<form method="POST" id="formid" />

The formid is dummy and is required (workaround) in this case to make a POST call else it makes a GET call.

Content sent is of type application/x-www-form-urlencoded. Make sure the service takes care of this.

xhrArgs = {
  url: "service endpoint",
  form: "formid",
  content: { data: dojo.toJson({name: "Amod", location: "Bangalore" }) }

The same should be handled in a JAX-WS call through

method(@FormParam("data") String data, ..) {
       MyRequestObject reqObj = new ObjectMapper().readValue(data, MyRequestObject.class);
       ResponseBuilder responseBuilder = Response.ok(file);
       responseBuilder.header("Content-Disposition", "attachment; filename=<filename to appear in download>");

Sunday, September 1, 2013

Financial figures? BigDecimal, yes it is!!

If you have to show high value financial figures BigDecimal should be the right type to use.

Float and Double would not show the correct value even if there is no decimal part to it because of the nature of their storage. Yes, BigDecimal is little expensive than Float or Double but you would not mess with financial figures.



Float.valueOf("342345345") = 342345344

RESTful - to PUT or to POST

This is an effort to simplify your decision making.



1. If the resource created/modified is represented by the URI

The body should contain data about xyz. If xyz is not found it is created else updated. Based on this
201, 200 or 204 is returned. In PUT the call is handles by the resource.

If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem.


1. If the resource created/modified is NOT represented by the URI

The body should contain data about registration (the form filled). It may create multiple resources, account, user, address. It might as well create a single resource. The POST call goes to the handler than directly to the resource.

Why should we worry about these verbs?

It helps the user agent. You can even perform an update during a GET call and no one would stop. But the client might not be aware of it.

GET is a safe method and is allowed for caching.
PUT is an idempotent call, N > 0 same request would have same effect as one request.



Ideally you would design this as POST and multiple call to it would create multiple posts.


This would be called with PUT, assuming POST was called earlier (of course you not create comments passing the ID in URL using PUT). N same calls with same content would result the comment in same state.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

A PUT says to not update any other resource than that in the request URI but entry in a ledger file can be ignored.

There are scenarios where response to POST can be cached but never response to PUT.

So what should be taken care is the expectation of the user agent. None of these things dictate what is done on the server side.

Another example, if I have a class User

Class Post {

createOrUpdate() {}

createComments() {}

createTags() {}


Class Comment {

 createOrUpdate() {}


If you have read so far and have to add. Please do so.

Monday, August 26, 2013

Video streaming - little 'bit'

I want to show a video on my website?

There are two ways:

1. Upload it on a video hosting site and use the embed URL
2. Use your own js player and source it from a streaming server

So what is a streaming server?

There are various protocols for streaming. RTSP kind of proprietary from Adobe has ruled the world with flash players. But now we are moving back to HTTP.

In streaming what is required is, data is sent in chunks and not the whole file.

There are two ways to it:

1. The server does all the job (chunking the files and sending it)
2. There are video containers which help us doing this job. Say MP4

So if you are using a proper file format, say mp4, lot of the work is done by the file itself. And then there is encoding on the top to compress the video.

The player would seek for chunks and the server would answer the same. That is how we can jump to positions.

A 5 minute video can be compressed at different bit rates that would determine the amount of bits received in the same time and thus the quality of video.

Here is sample to use a known js video player

Where I am streaming from?

1. AWS S3 bucket (make it public and take the name. You can cname it)
2. Google drive. The trick is to use this URL format<FILE_ID>. You can easily see the file id once you open it. Of course you have to give necessary permissions to the object.

<link href="" rel="stylesheet">
<script src=""></script>
<video id="example_video_1" class="video-js vjs-default-skin"
  controls preload="auto" width="640" height="264"
 <!--source src="" type='video/mp4' /-->
 <!--source src="" type='video/mp4' /-->
 <source src="" type='video/mp4' />
<iframe src="" width="640" height="385"></iframe>

I have put the embed url for the context. You have very less, or say none, skinning control with embed url. With custom player you can rock and roll.

I have played with RED5 andn Wowza media servers. But I don't think you would need any of those unless you are upto live streaming.

If you have something to add do add a comment, as I am learning !!

Sunday, August 11, 2013

blackday for

I opened and it was not the one I was looking for !! What the hell happened??

The message was to renew domain. Even more surprising!! I looked on whois and found has expired on 9th Aug! How can a big company like yatra could do such a mistake?? I know Monday it will be alarming for so many companies.

$ dig ns

; <<>> DiG 9.8.3-P1 <<>> ns
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 2529
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0


;; Query time: 498 msec
;; WHEN: Sun Aug 11 22:48:07 2013
;; MSG SIZE  rcvd: 27

$ dig +nocmd any +multiline +noall +answer 5421 IN A 141388 IN NS 141388 IN NS

So from the story so far it appears that as was not renewed they pointed it to some random IP from pool.

Even though the TTL for was low 5 minutes and very zone cache as 3 hours the site it not back from the NS ( - ACT broadband) I was using. I got it working when I changed to google open DNS.

Does it have something to do with TTL?

Data from fixed DNS.

$ dig +norec ns

; <<>> DiG 9.8.3-P1 <<>> +norec ns
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 9540
;; flags: qr ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0


;; Query time: 9 msec
;; WHEN: Sun Aug 11 23:16:22 2013
;; MSG SIZE  rcvd: 27

$ dig +nocmd any +multiline +noall     +answer 79 IN SOA (
2013081102 ; serial
10800      ; refresh (3 hours)
3600       ; retry (1 hour)
604800     ; expire (1 week)
300        ; minimum (5 minutes)
) 79 IN TXT "MS=ms49031691" 79 IN TXT "v=spf1 a mx ptr a: a: ip4: ip4: -all" 79 IN MX 10 79 IN A 79 IN NS 79 IN NS 79 IN NS

$ dig +nocmd +noall +answer 197 IN A

Any clue?

Monday, June 24, 2013

opencsv and Japanese character

The problem I faced:

opencsv was corrupting my characters in Japanese language.

- CSV cannot hold all types of unicode characters. (it can. even a notepad can.)
- FileWriter is not good for handling all types of unicode characters.

What was failing?

- The  ResultSetHelperService class of opencsv where there is rs.getString() was corrupting the data.


I need the figure this out :( But ofcourse it must be not encoding it to the correct character set)

What was the solution?

I derived a child class of ResultSetHelperService and overloaded getColumnValues. I copied everything and did a small change.

instead of

value =  rs.getString(colIndex)

I replaced it with

value =  new String(rs.getBytes(colIndex), "UTF-8")

and it worked !!!

I also read with newer version of Java and Oracle it just works. But for mySql 3.0 and JDBC 4 it did't work.


- The classes,, java.lang.String, and classes in the java.nio.charset package can convert between Unicode and a number of other character encodings.(




Tuesday, April 16, 2013

POI - Ordered List and Unordered List ( number list and bullet list )

This is my first week at Amazon. I haven't started any serious coding yet. This is my first itch here. We needed some unordered list ( bullet points ) in one of our program generated word documents. We are using POI.

I thought it should be some simple task. I am hearing about POI from a lot number of years. I was expected some good documentation or forum support. But dude there is none !!

Anyways, this is how I went ahead and have resolved few issues and do have some open questions and looking for answers.

There are two flavors of all the APIs. e.g. HWPF for the old .doc and XWPF for the new .docx. So you better set your game here. Many support you see in forums are for HWPF. Being the newer, I opted for .docx and so choose XWPF.

This is how I found the document structure like:

XWPFDocument  can have multiple XWPFParagraph. XWPFParagraph can have multiple XWPFRun. A XWPFRun is group of words with same formatting.

Open Questions:

1. Why is there not much support for this library? Is there some other library that is being used?
2. The numId is kind of global. i.e. The numbering continues. Also the behavior of this numbering is not clear.
3. Not able to set the level of list. 1, 1.1, 1.1.2
4. How to pick another type of  bullet image.
5. How to number a, b, c, ..

A little explanation would be helpful.

Important: I was struggling with the numId value to get either bullet point or numbers. I created a document and then saved it into .xml to see the structure. I observed variation in value for bullet and numbering. When I created a document from scratch using POI I was not getting the bullets and I was getting only numbers. Then I observed the full xmls, where there are lot of definitions. Somehow ( may be due to encoding ) those definitions were corrupted in case of the document generated from scratch. So I created a document ( can be a template document ) and then used it to write and got the bullet points after 4 hours of work. But feels good.

Friday, January 18, 2013

Shell Script: Analyze time spent in resource load

The developer tools in the new browsers are very helpful. From, debugging html code, analyze load time to in-line development.

The below script will be helpful when you have to analyze load time for bunch of resources which would be difficult to get done one by one in the browser.

Feel free to make your own changes.

Saturday, January 12, 2013

Postgres: Stop a script from accidental run


It will save you from accidental run! Add \q as the first line in your script.

Sunday, January 6, 2013

PHP: declarative property validation through document

Here is an attempt to define validation rules in document metadata and read it through reflection to validate the property against metadata. The metadata should be a valid json (verify using

Keeping it in metadata has the following benefits:

1. keeps it close to the property
2. easier to manage and read ( otherwise a complex structure if specified in another attribute )
3. is cool to do

Having type to php violates the dynamically typed nature of it. This is only in case you need it.

Saturday, January 5, 2013

PHP:json_encode private properties

json_encode( $obj ) works with public properties. In case you have private properties, you can implement jsonSerialize()::JsonSerializable. json_encode for objects of this class will create json with properties or value you specify in jsonSerialize. The output of jsonSerialize should either be array (normal or associative). The below code has a provision to filter attributes added in the $exclude_properties_to_json_arr var.Works with PHP 5.4 and above.