Thursday, August 6, 2015

HTTP Methods

HTTP (Hyper Text Transfer Protocol) enables the communication between client and server. It works as a request response protocol. An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection.

Structure of HTTP transactions

Both response and request messages consist of :
  • An initial line
  • Zero or more header lines
  • Blank line
  • An optional message body  

Initial request line

A request line has three sections, separated by space : a method name, the local path of the requested resource, and the version of the HTTP being used. 

 eg: GET /path/to/file/index.html HTTP/1.0

*The HTTP version always takes the form "HTTP/x.x", uppercase.

Initial response line

The initial line is different for the response than for the request. But it also has three parts separated by space : the HTTP version, a response status code, phase that describes the status 

eg: HTTP/1.1 405 Method Not Allowed

The status code is a three-digit integer, and the first digit identifies the general category of response:
  • 1xx indicates an informational message only
  • 2xx indicates success of some kind
  • 3xx redirects the client to another URL
  • 4xx indicates an error on the client's part
  • 5xx indicates an error on the server's part 
Header Lines
Header lines provide information about the request or response, or about the object sent in the message body.

The Message Body
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular,

  • The Content-Type: header gives the MIME-type of the data in the body, such as text/html or image/gif.
  • The Content-length:  header gives the number of bytes in the body.

Wednesday, July 29, 2015

Git

What is Git

  • Git is software that keeps track of changes that you make to files and directories and it is specially good in keeping keeping track of text changes that you maake.
  • Version control system
  • Source code management system 

Who should use Git ?

Primarily people work with source codes are going to get the most out of Git, but its not just for them its really for anyone who wants to track edits, specially edits for text documents. It is useful for anyone needing to share changes with colaboraters

Installing and Configuring

On ubuntu you can install Git with apt-get install command
$ apt-get install git

To configure with your user name and email address,

$ git config --global user.name <user name>

$ git config --global user.email <your email>


Generating ssh keys

SSH keys are a way to identify trusted computers, without involving passwords. The steps below will walk you through generating an SSH key and adding the public key to your GitHub account.

Step 1: Check for SSH keys

To check the existing SSH keys on your computer
$ ls -al ~/.ssh

Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:

id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub

If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can skip Step 2 and go straight to Step 3.

Step 2:Generate a new SSH key

Try following code with your GitHub email address
$ ssh-keygen -t rsa -b 4096 -C <your email>

and
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Then you'll be asked for passpharse
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

well thats it, now it will be given the fingerprint, or id of your ssh key

Step 3: Add your key to the ssh-agent

Ensure ssh-agent is enabled
$eval "$(ssh-agent -s)"

Add your ssh key to the ssh-agent
$ssh-add ~/.ssh/id_rsa

Step 4: Add your SSH key to your account

To configure your GitHub account to use your SSH key:

In your favorite text editor, open the ~/.ssh/id_rsa.pub file.

Select the entire contents of the file and copy it to your clipboard. Do not add any newlines or whitespace.

Add the copied key to GitHub:
In the top right corner of any page, click your profile photo, then click Settings.
In the user settings sidebar, click SSH keys.
Click Add SSH key.

In the Title field, add a descriptive label for the new key. For example, if you're using a personal Ubuntu, you might call this key "Personal ubuntu Desktop".

Paste your key into the "Key" field.

Click Add key.

Step 5: Test the connection

$ ssh -T git@github.com
# Attempts to ssh to GitHub

You may see a warning and then asked to verify the fingerprint, then type yes 
Then you will see this
  
Hi < your username> You've successfully authenticated, but GitHub does not provide shell access.

If the username in the message is yours, you've successfully set up your SSH key

                               ************************************

Ok !! Lets see what we can do with git

To make your current directory as your git local repository, use the command
$ git init

This will tell git, set up this as your homebase. Make this a Git repository and track all the files that come and go changes that are made inside this directory, only the things inside this directory, Git will be aware of it

lets use ls -la to see what is created after git initialization (git init )

You can see folder named .git inside that folder, it is the Git's workspace where Git does everything that it's going to do.

To see what's inside that .git folder just type ls -la .git in the terminal

Then you can make some changes inside that folder, I am going to create simple helloworld.java file inside the folder

Now switch over to terminal and tell Git add all changes that have been made to this entire project
$ git add .

Or you can just mention the name that you've created recently,
    $ git add helloworld.java


if you want to add all the java files then just type
    $ git add *.java

Now I am going to commit that change, tell Git to put it permanent memory. (put it in the repository)
$ git commit -m “inital commit”
after -m you ca give it a message

So this is the basic process that we are going to follow throughout working with Git
  • Make changes
  • Add the changes
  • Commit changes

Use meaningful commit messages, because when we come back later we can just look at the commit message and know what's inside

Viewing the commit log

$ git log

In first line you can see the commit id, which is unique to each commit message, then the author of the commit
If we want to see most recent five
$ git log -n 5

and
$ git log --since=2015-07-28
$ git log –until=2015-07-28
$ git log –author=“sashikau”
$ git log –grep= “Init”

You can easily create your on-line repo by sign in to your GitHub account.
To connect your local repo with the on-line one, you have to use the command :
$ git remote add origin https://github.com/username/repo.git

To view existing remotes
$ git remote -v  

To rename a remote 

$ git remote rename origin destination  
    - Change remote name from 'origin' to 'destination'

To delete a remote   
$ git remote rm destination

So now we are ready to send our files to remote repository, to do that we can use push command

$ git push <REMOTENAME> <BRANCHNAME>

eg : $ git push -u origin master


Fetching a remote

When working with other people's repositories, there are few basic git commands to remember

  • $ git clone
  • $ git fetch
  • $ git merge
  • $ git pull
      
These commands are very useful when interacting with a remote repository. clone and fetch download remote code from a repository's remote URL to your local computer, merge is used to merge different people's work together with yours, and pull is a combination of fetch and merge.


Clone

To grab a complete copy of another user's repository, use git clone like this: 

$ git clone https://github.com/username/repository.git

When you run git clone, the following actions occur:

   * A new folder called repo is made
   * It is initialized as a Git repository
   * A remote named origin is created, pointing to the URL you cloned from
   * All of the repository's files and commits are downloaded there
   * The default branch (usually called master) is checked out

For every branch foo in the remote repository, a corresponding remote-tracking branch refs/remotes/origin/foo is created in your local repository. You can usually abbreviate such remote-tracking branch names to origin/foo.


Fetch 

Use git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

If you already have a local repository with a remote URL set up for the desired project, you can grab all the new information by using git fetch *remotename* in the terminal:

$ git fetch remotename 
# Fetches updates made to a remote repository

Merge

Merging combines your local changes with changes made by others.Typically, you'd merge a remote-tracking branch (i.e., a branch fetched from a remote repository) with your local branch :

$ git merge remotename/branchname  
# Merges updates made online with your local work
 
Pull

git pull is a convenient shortcut for completing both git fetch and git mergein the same command:

$ git pull remotename branchname
# Grabs online updates and merges them with your local work

Because pull performs a merge on the retrieved changes, you should ensure that your local work is committed before running the pull command. If you run into a merge conflict you cannot resolve, or if you decide to quit the merge, you can use git merge --abort to take the branch back to where it was in before you pulled.

To see the differences, run

$ git diff HEAD

To undo all the changes since the last commit

$git checkout --myclass.java  

To remove a file from git just type

$git rm myclass.java 


Branches


To create a branch
$ git branch <branch name>
branch name with the asterisk sign is the selected branch at the moment

To see the existing branches
$ git branch

To view all of them just add -a (including remote branches)
$ git branch -a

To swich to a different branche
$ git checkout <beanch name>

Similarly you can switch to master
$ git checkout master

You can merge your branch with the master
$ git merge <branch name>

Once you are done with the branch you can easily remove it
$ git branch -d my_branch


What is cherry-pick?

cherry-pick allows you to merge single commit to another branch. 

Why cherry-pick?

Because there are times when you can't merge all the commits in one branch to another due to various reasons. In those cases you can cherry-pick that single commit to intended branch without merging all the commits.

How to perform cherry-pick command





 

Monday, July 27, 2015

Maven

What is Maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Why Maven?
  • Its easy and fast
     
    No need to worry about packaging all the libraries used in the system, but just mention those in the pom file (pom.xml, will be discussed later) under dependencies.

  • Well structured

    All the libraries used in the project can be understood by looking at the pom file.
  • Provide guidelines for best practices
    i)   Keeping your test source code in a separate, but parallel source tree
      
    ii)  Using test case naming conventions to locate and execute tests 

    iii) Have test cases setup their environment and don’t rely on customizing   the build for test preparation.
The POM

The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want.


Basic pom.xml file is shown below :


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.hsenid.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<name>my-app</name>
<url>http://maven.apache.org</url>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>

Installation


1. Download the maven latest version from the link below

https://maven.apache.org/download.cgi

2. Then unpack the compressed directory and place it any where as you desired.

3. Then edit the profile to change environmental variables as you did when installing jdk

Tuesday, July 21, 2015

Install MySQL on ubuntu


First you have to download the required mysql version (eg : mysql-5.6.20)
( http://dev.mysql.com/downloads/mysql/ )

Create user and user group called mysql
Shell> groupadd mysql
Shell> useradd -r -g mysql mysql

Now move the downloaded file (which is gzip compressed) to usr/local from where it is downloaded.
 
Shell> mv mysql-5.6.20-linux-glibc2.5-x86_64.tar.gz /usr/local (assuming we are in the downloads directory itself)

Change directory to /usr/local
Shell>cd /usr/local

Unpack MySQL
Shell> tar xvfz mysql-5.6.8-rc-linux2.6-x86_64.tar.gz

Rename the directory to mysql and change the path
Shell> mv mysql-5.6.8-rc-linux2.6-x86_64 mysql
Shell> cd mysql

Make the directory owned by the user and group mysql:
Shell> chown -R mysql .
Shell> chgrp -R mysql .

Run
Shell> scripts/mysql_install_db –user=mysql

To create the necessary databases (like the mysql database):
Change some ownerships:

Shell> chown -R root .
Shell> chown -R mysql data

The downloaded MySQL package comes with the init script mysql.server which we copy to /etc/init.d as follows:
Shell> cp support-files/mysql.server /etc/init.d/mysql.server
 
We can now start MySQL 5.6 as follows:
Shell> /etc/init.d/mysql.server start

To make MySQL start automatically at boot time,
Shell> update-rc.d mysql.server defaults
 
We cannot call MySQL commands (like mysql, mysql_secure_installation) without specifying the directory where they are located (/usr/local/mysql/bin). To call those commands directly those have to be placed in the PATH (/usr/local/bin), for that we can create symlink as follows :

ln -s /usr/local/mysql/bin/* /usr/local/bin/

To set root password
mysql_secure_installation

Install java on ubuntu

Use
     shell>file /sbin/init
     to check the bit version of your operating system               

Download the java version according to your system requirements

Then unpack the compressed java files, change the path to where you download the  java binaries and use this command
            shell> tar xvzf jdk-7u45-linux-x64.tar.gz

Now move the unpacked directory to your desired location
             shell> mv ./jdk1.7.0_45 /desired/location

Type
             shell>gedit ~/. profile
 to open a special file called profile to set the environment variables.
 Append following lines to end of that document

 If it is JDK
    JAVA_HOME=/usr/localJava7/jdk1.7.0_45
    PATH=/sbin:$HOME/bin:$JAVA_HOME/bin:${PATH}
    export JAVA_HOME
    PATH=$PATH:$HOME/bin
    export PATH

Or it is JRE
    JRE_HOME=/usr/local/java/jre1.7.0_45
    PATH=$PATH:$JRE_HOME/bin
    export JRE_HOME
    export PATH

Monday, July 20, 2015

Bash Scripting


What is bash scripting

Generally bash scripts are used by Developers, System Administers, Network Engineers, Computer Scientists and almost every one who uses Unix/Linux systems. Bash script is a simple text file with series of commands, usually we type on command line(commands that we've discussed in the previous post). Although .sh extension is commonly used with bash scripts it is not mandatory since Linux is an extensionless system.

Contents of a simple script












The Shebang (#!)
#!/bin/bash
First line of the above script, #! character sequence referred to as shebang and then it is the path to the interpreter that should be required to run the next lines in the script.   


Executing the script

Before we can run the script we have to grant the permission, otherwise you'll get an error message saying “Permission denied”




Bash Command

Why you should learn command line ?
     Though it is difficult to learn command line (CLI), it is the quickest and most efficient way to use a computer for many different tasks.

Lets see what we can do with bash commands

Basic Terminal Shortcuts Basic file manipulation

CTRL L = Clear the terminal
CTRL D = Logout
SHIFT Page Up/Down = Go up/down the terminal
CTRL A = Cursor to start of line
CTRL E = Cursor the end of line
CTRL U = Delete left of the cursor
CTRL K = Delete right of the cursor
CTRL W = Delete word on the left
CTRL Y = Paste (after CTRL U,K or W)
TAB = auto completion of file or command
CTRL R = reverse search history

Basic Terminal Navigation

pwd = print working directory
ls = list files and folders
ls -a  = list all files and folders (including hidden files)
ls <folderName> = list files in folder
ls -lh = Detailed list, Human readable
ls -l *.jpg = list jpeg files only
ls -lh <fileName> = Result for the file only

cd <folderName> = change directory
if folder name has spaces use “ “
cd / = go to root
cd .. = go up one folder, tip: ../../../

du -h: Disk usage of folders, human readable
du -ah: Disk usage of files & folders, Human readable
du -sh: only show disc usage of folder

What Is Linux?

There are many operating systems around such as Windows, DOS, and Mac OS and Linux is yet another. Linux is an operating system that evolved from a kernel created by Linus Torvalds when he was a student at the University of Helsinki. However Linux is different from most of those operating systems in many areas and following are some of them

1.  Linux is free
Unless you obtained Windows illegally, you probably paid for it. On the other hand, you can get Linux completely free of charge. This is a massive saving specially when comparing to all the programs you have to install with windows operating system

2.  Freedom
Linux and "Open Source" software are "free". This means their license is a "free license", and the most common is the GPL (General Public License). This license states that anyone is allowed to copy the software, see the source code (the "recipe"), modify it, and redistribute it as long as it remains licensed with the GPL.

3.  Forget about viruses
Linux hardly has any viruses, mainly because
  • Most people use windows and people who develop viruses target larger audience so they attack windows
  • Linux uses strong security mechanism, user cannot just go inside the system folder and what ever they want. Every time user try to do some modification, an administrator password is required. This is same when virus try to attack the system, authorization is required to proceed the action.
  • Linux is open source, meaning it is contributed by developers all over the word that makes it more powerful
4.  Powerful shell
Of-course, GUIs are easy to use but sometimes shell can be so much faster and reliable. It allows you to do large number of complex operations using simple commands.    

5.  It is not only an OS
When you get linux you also get many other programs without installing anything.
  • An office package to create documents, edit spreadsheets, make presentations and do many other things.
  • A web browser to surf the web
  • A photo editor 
  • A music/video player
  • A pdf reader
  • etc...