Tuesday 29 March 2016

First node.js application

Go to URL : node.js download the software.

On window click on node.js exe to start.

output : >

Basic operation :

> var x
undefined
. define the variable and initialise the value

x= 1000
> x
output : 1000

and var y, and initialise y=09 and print variable y.

Now perform the basic operation.

> x+y
output :  1009

Now creating the application

step 1: 

create the server.js file.

a. define the module "http" for making the request
ex : var http = require("http");
b. call the server instance using http and will listen to port(8888), bind the port the server
ex:
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
c. In the above function pass the request and response parameter.
    function(request, response) {
     ...
    }
d. On making a request. it will call the request
ex : http://localhost:8888/
e. For Response bind the content-type and status code and content to display
ex : response.writeHead(200, {"Content-Type": "text/plain"});
       response.write("Hello World");

step 2 :

a. go to command prompt and go the node.exe file and open it
ex : node server.js
output : Hello World

step 3:

Go to browser and make request to nodejs server
a. http://localhost:8888/
b. output :
Hello world






File upload with  data  using angular js and jersey webservice

1. jersey service.

        @POST
@Path("/public")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void get(
                               @FormDataParam("file") byte[] fileInputStream,
            @FormDataParam("file") FormDataContentDisposition fileInputDetails,
            @FormDataParam("id")String userid){

System.out.println("THis is the ID "+userid);
System.out.println("File Details : "+ fileInputDetails.getFileName());

System.out.println("Reading the String data "+ fileInputStream.toString());

String s = new String(fileInputStream);
   System.out.println("Text Decryted : " + s);

}

2.  create the angular js service,directive,controller

Service.

service('myFileService',["$http",function($http){
console.log("service is called");

this.post = function(uploadUrl, data){

var fd = new FormData();   // bunch of  key,value

console.log("uploaded URL "+uploadUrl);
console.log("uploaded data "+data);

for(var key in data) {

console.log("key is : "+ key + " , value :  "+ data[key]);
fd.append(key, data[key]);
}

$http.post(uploadUrl, fd, {
transformRequest: angular.indentity,
headers: { 'Content-Type': undefined }
})
.success(function(){
console.log("from service success");
})
.error(function(){
console.log("from service failure");
})

}


}]);

Directive : to bind the file data

directive('fileModel', ['$parse', function($parse){
return {
restrict: 'A',
link : function(scope, element, attrs){

var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(changeEvent){
scope.$apply(function(){
                         modelSetter(scope, changeEvent.target.files[0]);
                });

});
}

};
}]);

Controller :

controller("postCTRL",['$scope','myFileService',function($scope,myFileService){
$scope.sample = {};
$scope.Submit = function(){
console.log($scope.sample.name);
var uploadedURL = "/public";
var data = $scope.sample;
myFileService.post(uploadedURL,data);
 
}
 }]);

3. HTML FORM

<form>


<p> Name : <input type="text" name="name" ng-model="sample.id" required /></p>
   
<input type="file" id="exampleInputFile" file-model="sample.file">  
   
<button ng-click="Submit()">Submit</button>

</form>
{{sample}}

</div>



Ref :
1. stackoverflow



Client API

JAX-RS Client API, which is a fluent Java based API for communication
with RESTful Web services. This standard API that is also part of Java EE 7 is designed to make it very easy to consume a Web service exposed via HTTP protocol and enables developers to concisely and efficiently implement portable client-side solutions that leverage existing and well established client-side HTTP connector implementations.

The goals of the client API are threefold:
1. Encapsulate a key constraint of the REST architectural style, namely the Uniform Interface Constraint
and associated data elements, as client-side Java artifacts;
2. Make it as easy to consume RESTful Web services exposed over HTTP, same as the JAX-RS serverside
API makes it easy to develop RESTful Web services; and
3. Share common concepts and extensibility points of the JAX-RS API between the server and the client
side programming models.