Search This Blog

Monday, October 12, 2015

Unix Shell Scripting

Unix uses Shells to accept commands given by users.

Commonly used shells: SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell
Other shells: KSH is based on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is based on CSH.

To check which shell is installed:  echo $SHELL

Shell's command line gives interface between users and UNIX Kernel.

Frequently used commands:
sudo su -
gzip -d xyz.gz
pwd
ls -a
cd /home
./sample.sh (to execute a file)
cp
mv
date "%y,%m,%d,%h"
whoami
ps -e | grep parse
kill
hostname
ifconfig



General:
Use semicolon(;) to separate commands, backslash(\) to continue the commands in new line and pipe(|) to pass the output of one command as input to another command.

Conditional Operators:
&& - A command runs only the previous command is successful
|| - A command runs only the previous command is failed.

Group Commands:
Braces ({})  -Command runs in current shell and returns one exit status for the entire block of the command with in the braces.

Round Brackets(()) - Commands execute is subshell as single block.

Redirection:
> - redirect the output of the command to a new file (ls  > output.log)
< - pass the file as input to the command (grep venkat )

>> - redirect the output of the command to append to the existing file

File Descriptors:
0 - standard input
1 - standard output
2 - standard error ( ls 2> log1.log) - output the error to log1.log file


Substitution & patterns:
Filename substitution:
* - match any string
? - match any single character
[abcd]- match any of these characters for a single character
[a-d] - match any of the characters in the range for a single character.

Command Substitution: Output of one command as input for another command(use single quotes)
cat 'find . -name venkat*.txt' (dump all the files starts with venkat that exists in current directory to the screen)


Thursday, October 8, 2015

REST API - Fundamentals

REST is known as Representational State Transfer meaning a Representational State of the resource(Image, html document, etc) is transferred from the Server(Web Server) to Client (Web Browser). REST is an Architectural Style to design network applications. REST also has Architectural constraints, but they are not enforced. For example, in REST, POST is supposed to create a new record in the server. But we can use POST to query records from the server.

SOAP is a XML based RPC (Remote Procedure Call) over HTTP protocol using HTTP Post method. Also SOAP can be used with FTP, JMS and SMTP Transport protocols.

REST-based architectures communicate primarily through the transfer of representations of resources". This is fundamentally different from the Remote Procedure Call (RPC) approach that encapsulates the notion of invoking a procedure on the remote server. Hence, RPC messages typically contain information about the procedure to be invoked or action to be taken. This information is referred to as a verb in a Web service request. In the REST model, the only verbs allowed are GET, POST, PUT, and DELETE. In the RPC approach, typically many operations are invoked at the same URI. This is to be contrasted with the REST approach of having a unique URI for each resource.

REST has the following architectural constraints:
stateless
cachable
uniform interface (only through GET/POST/PUT/DELETE)
client-server
layered system


REST vs SOAP:
SOAP uses only POST
REST uses GET, POST, PUT, DELETE
SOAP uses only XML Message
REST uses XML, JSON, Text, csv etc.
In SOAP, Interface definition is defined in WSDL file.
In REST, there is no interface definition file. Data can be set in HTTP parameters (URL Query parameters) of GET or in HTTP Body(payload) of POST.

REST Service format:
REST service has three components:
1. Service name (or) Service Endpoint (maps.googleapis.com)
2.Unique URI(path) to identify the Resource (/maps/api/geocode/xml)
3. Query parameters if it's GET method (?param1=sx, param2=123)

Example:
Resource 1: http://maps.googleapis.com/maps/api/geocode/xml ? param1=sx, param2=123
Resource 2: http://maps.googleapis.com/maps/api/geocode/json

Sample REST raw GET request:

GET http://maps.googleapis.com/maps/api/geocode/xml?address=Parkway%2C%20Mountain%20View%2C%20CA&sensor=false HTTP/1.1
Accept-Encoding: gzip,deflate
Host: maps.googleapis.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

Sample REST Raw GET Response:

HTTP/1.1 200 OK
Date: Thu, 15 Oct 2015 09:08:16 GMT
Expires: Fri, 16 Oct 2015 09:08:16 GMT
Cache-Control: public, max-age=86400
Vary: Accept-Language
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Server: mafe
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Content-Length: 555
Content-Type: application/xml; charset=UTF-8
Connection: keep-alive

XML Document>
 .......
...........

Sample SOAP Web Service Request (always use POST method):



Monday, October 5, 2015

JSON Basics

JSON is the abbreviation for  Javascript Object Notation. It's a lightweight data interchange format - Text Format that is independent of any programming language.  It's also a syntax for storing and retrieving javascript objects.

JSON is built on two structures:
1. Name/Value Pairs (something similar to Object in Java)
2. An ordered list of values (something similar to Array in Java)

Example 1:
{"firstName":"venkat", "lastName","chinna"}

Example 2 (Arrays):
"employees": [{"firstName":"venkat", "lastName","chinna"}, {"firstName":"john", "lastName","miller"}]

Example 3 (JSON Object):
"window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    }

Data types:
int, string, float, boolean, array, object, null

JSON vs XML:

JSON
XML
Provides automatic way of serializing/de-serializing javascript objects
Need to write code to do the same.
Format follows simple name/value pairs.
Complex, verbose format with tags and namespaces.
It’s data oriented
It’s document oriented.