Wednesday 11 May 2016

CORS - Cross Domain-Resource Sharing , Camel Restlet


Hi guys, I have  come  with new one on CORS - HTTP Stuff

CORS means CROSS DOAMIN RESOURCE SHARING

If you develop REST Application and tried testing with Chrome REST or Firefox REST Plugin or addons , it would have worked. But When u try with HTML page using JavaScript it might return with HTP - 405 (Method Not Allowed)

In my case it is issue with Camel - RESTLET. I have the same issue as below :
INFO:         13:10:38        127.0.0.1        -        -        24366        OPTIONS        /ORDER/COMPUTER        -        405        487        0        2        http://127.0.0.1:24366        Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0        -

Solution:

Before going to issue you have to understand the CORS concept. As it name says , it is cross domain .

Your code is actually attempting to make a Cross-domain (CORS) request, not an ordinary POST.
That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.
Example: A page in http://www.server.com/index.html can only directly request services that are in http://www.server.com, like http://www.server.com/testservice/etc. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.
To put it shortly, to perform a CORS request, your browser:
  • Will first send an OPTION request to the target URL
  • And then only if the server response to that OPTION contains the adequate headers (Access-Control-Allow-Origin is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).
    • If the expected headers don't come, the browser simply gives up (like it did to you).


I enabled the CORS for RESTLET in camel.
And also upgraded to Camel 2.17.1, which works well with CORS.Bcoz it doesn't support in earlier version which is less than Camel 2.16.2 .

Reference:

Wednesday 4 May 2016

JSON <-> MAP Serialization


Problem:
how to convert to JSON to MAP and Vice-versa in java?

Solution:

I used org.json library simple and best for json operations.

//import
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.json.JSONTokener;

long start = System.currentTimeMillis();
        String test = "12333";
        Map<String,String> map = new HashMap<>();
       
        for (int i = 0; i < 10; i++)
        {
            map.put("key"+i, i+test);
        }
        //System.out.println(map);
        long pause1 = System.currentTimeMillis();
       
       
       
        JSONObject json = new JSONObject(map);
        json.toString(4);
       
        long pause2 = System.currentTimeMillis();
        System.out.println("Map 2 JSON---"+(pause2-pause1)+"Millis");
       
         JSONObject object = (JSONObject) new JSONTokener(json.toString()).nextValue();

        object.toString(4);
        long pause3 = System.currentTimeMillis();
       
        System.out.println("JSON to MAP---"+(pause3-pause2)+"Millis");