Friday, August 11, 2017

Java Memory Model



When Threads share objects in Heap, each thread has to acquire lock(a flag that will be set by the JVM upon request from thread) of the object that it wants to use. In case of method area, threads have to acquire lock of *Class to use its static data.

*Class: In Java, there is a class called Class that represents the actual Class template(not the object)

SOAP vs REST

SOAP RESTful
Heavy Weight Light Weight
Highly standardized with a dedicated team looking after the specifications Its just an archtectural style. No specific guidelines or spec
Slow because of High Payload and processing. Both payload and processing are related to guidelines. SOAP message(Payload) should contain all the information adhering to all the standandards(Eg: Secruity, Distirbuted nodes, Addressing etc) Hence the Message generator(client) and and its processing server need to accommadate all the code to process the guidelines. Thats the overhead while scaling. No rules. No overhead. Note: And to be fair cant even be compared with SOAP. Since REST does not follow any guidelines.
Tool need to test SOAP-WS since the WSDL cannot read by any browser so far. Browser will be sufficent but for complex requests POSTMAN is required
Built in error handling guidelines are to be mentioned in the message No built in error handling is specified in the message
Highly secure. Independent of Transport Protocol's security No message layer security can be achieved. Need to depend on Transport layer and Applicaiton security

Sunday, August 6, 2017

GIt Tutorial - Important commands

1. Open Git bash

Download existing source from Sever(Github/Gitlab):
1. cd path_of_the_workspace
2. git clone url_of_repo_on_github/gitlab name_of_the_local_folder
Eg: git clone https://github.com/naveenanem22/pmt-phase1.git demo
Note: Name of the localfolder is optional. If not provided, it will create a same name as per the repo on server
The above command will create a folder with the name of the root folder of the project on server inside the workspace directory
a remote called 'origin' gets created with the url of the remote-project-on-the-server
The local project folder gets initialised with git init command
3. cd name_of_project_folder
eg: cd pmt-phase1
4. Clone a branch of a repo..not the master
git clone --single-branch
as in:
git clone --branch --single-branch []
git clone https://NKumar@www.servername.com./scm/reponame/someapp.git --branch v3-update --single-branch

Following commands are valid from the project folder on the local machine:
cd name_of_prj_flder
1)git branch: Shows the details of all the branches the local project has
2) git remote: show the details of the remotes the project connected to
What is remote?
Remote is the alias of the url from where branch is created
A project can have as many remotes as possible
A branch can update its source from any of the remote
3) git init: This is the first command to bring a project(folder) under the radar of Git.
Execute this command from project-root-folder.
4a) git add . : commits all the files from working-dir to staging-area
4b) git add "name_of_the_file.ext" : adds specific file from working-dir to staging-area
eg: git add tobeaddedfile.txt 
5) git commit: commits all the changes from staging-area to repo
eg: To commit all files => git commit -m "This is the commit comment"
To commit specific file => git commit "name_of_the_file.ext" -m "comment"
eg: git commit system.js -m "Committing system.js"
6) git push: pushes local dir to server
7) git status: shows the details of the git local project
8) Rename a remote:
git remote rename old_name new_name
eg: git remote rename old_name new_name
9) Rename the current branch to a new name
Eg: git branch -m new_branch_name
9) Switch to another branch
10) To discard changes done to a file
eg: git checkout -- name_of_thefile.java
11. How to remove a project-root folder from Git Version system if it is initiated by 'git init' command
Navigate to project-root folder in windows explorer-> Show Hidden files -> Delete .git folder. This should remove the project-root from Git system. Use git init command to add it again.
12. git branch -a
Lists all the branches local and remote
13. git commit history with timestamp
git log --pretty=format:"%ad %h by %an, %s" --date=iso | sort -r | less
14.To reset to a previous commit
git reset --hard
15. To delete a branch
git branch -d
16. To revert a particular commit
git revert <#commit-hash>
eg: git revert 00eer6549
17. To rebase a branch1 from branch2
Step 1: Switch to the branch1
git checkout branch1
Step 2: git rebase origin/branch2
Step 3: git status --> This will show you to pull the changes..but don't do that..rather issue the following command..
git push -f -> this is to force push the changes




Following are useful linux(unix) commands:
1. ls --> lists all the subfiles and folder under a specific directory
2. ls -la --> lists all sub files and fodlers including hidden and meta files


Create a branch from master branch in local
Note: Before creating a new branch, always uptodate the current branch
1. cd name_of_prj_flder
2. git checkout -b new_branch_name
eg: git checkout -b pmtnaveen
Once work changes committed to local repo, push the branch to server.
3. add all files to staging area
git add .
4. commit files from staging to final-repo
git commit -m "This is the commit comment"
5. git push remote_name branch_name or git push --set-upstream remote_name branch_name 
a new branch gets created on the server(github)

How to add a local project to git version control and eventually create a repo on server
1. from Git bash: cd to Project_root folder
2. git init
Desc: this adds the whole project folder into git version control
3. git add .
Desc: to add all files and folders under the project to staging area
4. git commit -m "comment"
Desc: to commit the files from staging to commit
4.a git commit
Desc: To commit the files and write the comments on the editor. ':wq' to exit
5. Create a 'repo' on github and copy the url
6. create remote pointing to the step-5's url
git remote add 'name_of_the_remote'  'url_of_the_remote_git_repo.git'
eg: git remote add origin https://github.com/naveenanem22/meandemo.git
7. git push 'name_of_the_remote' 'name_of_the_branch'
git push origin master


Update localbranch to track remote branch:
cd name_of_prj_folder
1. git branch -u remote_name/branch_name
2. git pull

Git editor shortcut keys:
1. press "i"
2. write your merge message
3. press "esc"
4. write ":wq"
then press enter

Gitignore:
How to tell git to ignore some files/folders from tracking
Ans: 
1. Add a file called '.gitignore' in project root dir.
2. Specify the names of the files or folders or with pattern in .gitignore file. Save the file.
3. Add the file to git system by git add command
4. Then commit .gitignore file
5. Run git status command to check if the ignored files shown under untracked are not visible anymore

Saturday, July 29, 2017

Web Services

Web Services are mainly for 2 reasons:

1. For machine consumption
2. Language/Platform independent application development


According to J2EE spec, following are the categories of Web Services:
a) JAX-WS* :- SOAP based[Earlier JAX-RPC]
b) JAX-RS :- RESTful

SOAP Based / JAX - WS:
JAX-WS* Specifies various standards of how a communication of messages between source and destination(including intermediaries) to happen in a distributed Enterprise Architecture.


Theoretical Specification:
WS-Addressing
WS-Policy & WS-Security
WS-Federation
WS-Reliable Messaging
WS-Coordination
WS-Transaction
WS-Remote Portlets

Technologies in SOAP space: SOAP, XML, WSDL, XSD, JAXB, UDDI, SAAJ & ws-gen, ws-import

Most Important:-
WS-Addressing: SOAP message travels from a Source to Destination thorugh various intermediate nodes. Intermediaries are also web applications but they only process some part of the SOAP message then forward it to next node in the path. All addressing related intermediaries etc is contained in SOAP XML message as per WS-Addressing.

WS-Security: This ensures "message layer security" does not depend on Transport Layer(Or Protocol Specific) security. Means, SOAP-XML containing encrypted security information is carried along with the message preventing it from being intercepted by unintended recipients.

Sample SOAP Request and Response:










Security in J2EE vs Spring Application

There are 3 types of Security in Web Applications:

1. Transport layer security - TLS
2. Message layer security - SOAP
3. Application layer security :
   a) Container managed in J2EE
   b) Independent of Container in Spring MVC. Handled by AOP.


J2EE
Application layer security is being handled by Containers. Means, web container has the code to verify the security aspects of the request and upon verification lets it to access servlets. Before this request to be dispatched to an EJB Bean, EJB container has the code to verify the requester has sufficient privileges[Authorization]  to access beans and upon verification, ti lets to access the EJB beans to process Business Logic.

Spring MVC:
In Spring MVC containers dont handled the security mechanism. This will be separately handled by another "Class" as an aspect[AOP]. Containers have no code to verify access related to the requester.

Spring Security Class/Handler has an XML[just like IOC Container has an XML] configured in web.xml. Spring Security Class has further processes the Authentication/ Authorization of the requester via various Filters/Interceptors chain. Hence, the security mechanism is independent of container. Kind of AOP, the security code can be injected anywhere.

Security in Web Applications

In the context of security in Web Applications, predominantly there are 3 layers:

1. Message Layer Security
2. Transport layer Security
3. Application Layer Security

Message Layer Security: Encrypting message[XML] in SOAP request. This can be made possible with SOAP. But not REST.
Consider a message/request travelling from on client to a server. But not that, there will be intermediate nodes in between which might do one or more of intercept, process and forward the message.


SOAP Request Travel Path
The SOAP request contains some encrypted portions, only the intended server can only process and read. No other intermediate server can process. This can be achieved by WS-Security Specification by J2EE.

Major advantage is that even though TLS is broken, Message(SOAP) cant be decoded by attacker which is another layer of security. Hence, SOAP security is independent of Protocol-Security.

Transport Layer Security: 
TLS is the successor of SSL. Part of TCP/IP or OSI.
TLS Security
The channel of message communication is encrypted. But if an attacker hack the TLS layer encryption, access to actual message is easy which is not possible in SOAP.

*How to configure TLS:
At server end, Server.xml config file need to be modified to enable TLS Security.

Application Layer Security:
The security constraints which imply on the login credentials of the User/Client. This is composed of 
1. Basic Authentication: Login ID and Pwd
2. Authorization: Roles and Privileges mapping
3. SAML and oAuth

These are coded as part of Web Application's Servlet and other security component

J2EE vs Spring Eco Systems

A comparison of J2EE Standard Application Server and Spring IOC with Web Server:


Spring MVC with TomCat Web server






IBM WebSphere App Server

How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...