Archive

Posts Tagged ‘REST’

IE caching Ajax calls

March 2nd, 2011 No comments

If you have been trying to make a REST call from your javascript or jsp page, you might have found that in firefox(FF) and Safari there is a call made to server every time . But this does not happen in IE.

This is the scenario I had

I had script file client.js which was making a rest call to get some data.

First time I could see the results but then with every other subsequent call, there was no change in state, although the data should have changed.

Now this can be fixed by multiple ways

Option 1:

Change the browser settings. To  do this you have to go to

General–Settings>

and change the settings from Automatically to Every Visit to page

This will resolve the issue.

Option 2:

Change from GET to POST

Option 3 :

Add a timestamp at end of url which will looking at which server will think it is a new URL.

Read more…

Invocation of REST service from Java Script

January 29th, 2011 No comments

We had shown an example to create your REST service in Java using JERSEY Creating your first REST service using Jersey. In this post we will explain how to invoke REST service through Java Script

<html>

<head>

<script type=”text/javascript” charset=”utf-8″ src=”json2.js”></script>

<script type=”text/javascript” charset=”utf-8″ src=”jquery-1.4.4.min.js”></script>

<script>

var restDataResults;

function callMyFirstRestService()

{

var myRestService = null;

if (window.XMLHttpRequest) //for mozilla

{

myRestService = new XMLHttpRequest();

if ( typeof myRestService.overrideMimeType != ‘undefined’)

myRestService.overrideMimeType(‘application/json’);

} Read more…

Synchronous calls with REST Service

January 26th, 2011 No comments

In last posts , we had shown the example Creating your first REST service using Jersey. Then we introduced to making the Rest call from JavaScript.

The example which we had shown was for asynchronous request. Now let us assume that you want to make an asynchronous call . How will it work out ?

Option A : Let the call be asynchronous and put a while loop at the end

//Create a callback    for myRestService
myRestService.onreadystatechange = function()
{
if (myRestService.readyState == 4)
{
if(myRestService.status == 200)
{
// Retrieve the json object and work on it

var obj = JSON.parse(myRestService.responseText);

for(var i=0;i<obj.length;i++)
{
var item = obj[i];
$(“#restDataResults”).append(
‘<tr><td>’+ item.value1 + ‘</td>’ +
‘<td>’ + item.value2 + ‘</td>’ +

‘</tr>’);
}

done = true;
}
else if(myRestService.status == 404)
{
alert(‘Error occurred:’ + myRestService.responseText);
}else{
alert(“No resposne–Error”);
}
}
};

tempresults = myRestService.send(null);
while(done == false){

//wait here

}

In this case you will see a warning pop up in Firefox

A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.

Option Bfalse changes to true and we make synchronous calls

myRestService.open(“GET”, serviceUrl, true);

Now this works out well in IE but FF does not accept it.

Why it does not work ? Because we are still using the callback mechanism to get back the results

If we change it to regular method call , it will return results

Here is the code

<html>
<head>
<script type=”text/javascript” charset=”utf-8″ src=”json2.js”></script>
<script type=”text/javascript” charset=”utf-8″ src=”jquery-1.4.4.min.js”></script>
<script>

var restDataResults;
var done = false;

function callMyFirstRestService()
{
var myRestService = null;
if (window.XMLHttpRequest) //for mozilla
{
myRestService = new XMLHttpRequest();

if ( typeof myRestService.overrideMimeType != ‘undefined’)
myRestService.overrideMimeType(‘application/json’);
}
else if (window.ActiveXObject) //for IE
{
myRestService = new ActiveXObject(“Microsoft.XMLHTTP”);
}

//This is the url/ restApp is the web app deployed on tomcat.
var serviceUrl = “http://127.0.0.1:8080/restApp/webresources/paramater/1440″;

myRestService.open(“GET”, serviceUrl, false);
//synchronous calls
tempresults = myRestService.send();
var obj = JSON.parse(myRestService.responseText);

}

</script>
</head>
<body>
<h2>My First Rest Service</h2>

<p>My rest Service:
<input type=”button” id=”submitlookup” name=”submitlookup” value=”My First rest Service” onClick=”javascript:callMyFirstRestService();”/>
</p>

<div id=”divRestData” title=”My rest data”>
<table border=”1″>
<thead><tr>
<td width=’70’>Column A Heading</td>
<td width=’100′>Column B Heading</td>
</tr></thead>
<tbody id=’restDataResults’>
</tbody>
</table>
</div>
</body>
</html>

Hope you would have find this helpful.

XMLHttpRequest cross domain request issues on Firefox and Safari

January 2nd, 2011 No comments

If you are running you web application on a domain http://mydomain.com and you want to invoke XMLHttpRequest on another domain for eg http://mydomain2.com , you would find different issues in IE, Firefox and Safari.

Code

if (window.XMLHttpRequest) //for mozilla
{
myService = new XMLHttpRequest();

if ( typeof cmdService.overrideMimeType != ‘undefined’)
myService.overrideMimeType(‘application/json’);
}
else if (window.ActiveXObject) //for IE
{
myService= new ActiveXObject(“Microsoft.XMLHTTP”);

}

function callCmdService()
{
//Call the REST service’s GET method asynchronously
myService.open(“GET”, “http://mydomain2/restdata/1, true);

}

What happens in IE ?

In IE you might see an error message pop up , kind of a security warning. When you say yes, only then the connection is established with mydomain2.

What happens in Firefox ?

A security restriction within Firefox, the “same origin policy”, causes this error. Essentially, javascript within a page should only interact with the same host that served that page.

I could not find any reliable solution yet, but below are some helpful resources.

Some helpful resources

http://code.google.com/apis/youtube/2.0/developers_guide_jsonc.html#Requesting_JSONC_Feeds

http://blog.monstuff.com/archives/000262.html

http://ejohn.org/blog/cross-site-xmlhttprequest/

Creating your first REST service using Jersey

December 29th, 2010 2 comments

In the last post , we had talked about SOAP being retired in favor of REST based service. in which we discussed advantages of REST over SOAP.

This post will be a short tutorial on creating your first REST based service.

What is REST  and how are they accesses ?

From Sun(oracle) docs

RESTful web services are built to work best on the Web. Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability, that enable services to work best on the Web. In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), typically links on the Web. The resources are acted upon by using a set of simple, well-defined operations. The REST architectural style constrains an architecture to a client/server architecture and is designed to use a stateless communication protocol, typically HTTP. In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.

Why use  JERSEY ?

Java defines standard REST support via JAX-RS (The Java API for RESTful Web Services) in JSR 311. Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services

Here is the objective of our tutorial

  • Set up development environment and infrastructure to build first REST full app using JERSEY
  • Create the sample application
  • Deploy and run in tomcat Read more…
Categories: Programming / tutorials Tags: , ,

Goodbye SOAP – Welcome JSON REST

November 2nd, 2010 18 comments

We look at the leader to give us the guidance.

Not sure if that is quote or not but when Google , Apple , facebook or some of the best companies take some step, it is time to take notice.

Google has killed their SOAP API. XML+SOAP was a good milestone in the history of integration but it is now time to look at the next milestone and that next milestone is JSON+REST

What is REST ?

Representational State Transfer (REST) is a software architectural style for distributed hypermedia systems like the world wide web.

The term originated in a 2000 doctoral dissertation Architectural Styles and the Design of Network- based Software Architectures about the web written by Roy Fielding, one of the principal authors of the HTTP protocol specification, and has quickly passed into widespread use in the networking community.

What is SOAP ?

Simple Object Access Protocol . SOAP is an XML-based messaging protocol. It defines a set of rules for structuring messages that can be used for simple one-way messaging but is particularly useful for performing RPC-style (Remote Procedure Call) request-response dialogues. It is not tied to any particular transport protocol though HTTP is popular

Why REST + JSON is a good choice over SOAP? Read more…

Categories: Programming / tutorials Tags: , ,

Power of WCF

August 19th, 2009 2 comments
WCF (Windows Communication Foundation) is Microsoft’s unified programming model for building service-oriented applications that communicate across the web and the enterprise. WCF clearly demarcates development and implementation. There are various Hosting options available and is independent of the run-time environment in which the service is deployed, they are:-

  • Self-Hosting in a Managed Application
  • Managed Windows Services
  • Internet Information Services (IIS)
  • Windows Process Activation Service (WAS)

All the development part lies in the code while the implementation like the binding can be handled in the config file as Microsoft has abstracted a lot of plumbing work reducing the implementation time. By this way developer can concentrate on core logic and WCF takes care of data delivery in the most reliable, secure and high performance fashion depending on situation.

WCF has an integrated Logging mechanism that helps you log traces. In other technologies developer has to write custom solution but in WCF, all that you need to do is to enable trace by changing the config file.

To achieve security mechanism in WCF you have 3 implementation choices:

  1. Transport: Transport security depends on the mechanism that the binding you’ve selected uses.
  2. Message: Message security means that every message includes the necessary headers and data to keep the message secure.
  3. TransportWithMessageCredential : This choice uses the transport layer to secure the message transfer, while every message includes the rich credentials other services need.

After the launch of modeling edition I feel it gets easy to delegate work in a team. An architect can create a Data Contracts/Relationships, Message Contracts and Service Contracts. After that only the facade layer has to be made with some tweaks here ‘n’ there. In this way lot of architecture intricacies can be hidden from developer.

WCF does not replace MSMQ, COM+ or Web Services, it provides a declarative infrastructure for all forms of communication to and from the Windows platform. Using WCF, communications happen at designated service endpoints, and an endpoint can implement different protocols (such as Web Service, WS-* SOAP style services), encodings (such as Text, Binary & MTOM), and transports (such as MSMQ, Http, etc.).

With .net 3.5, it becomes even easier to provide RESTful endpoints (JSON, POX, RSS and ATOM). So the real powerful leverage shall be building services that can be consumed by a ASP.net web client as JSON while simultaneously being accessed by a business partner through a php web client using the SOAP protocol (with its full complement of WS-* implementations such as WS-Security, WS-ReliableMessaging, etc.)

Categories: Programming / tutorials Tags: ,