Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Sunday, March 20, 2016

How a Http Server works and develop a simple web server for yourself


I worked with HTTP Servers for a long time but I never tried to explore how an HTTP server works, so let do it together.

What is an HTTP server actually? 

A server which runs in a computer to serve HTTP request from remote browsers.

How it works?

As a user when we type an URL (Unified Resource Locator) in our browser it sends an HTTP request to the internet then it forwarded to the concerned server, the server process the request and send back HTTP response to the client browser.

Okay, it sounds simple to make HTTP server work. 

Let's get into it.
A Server class which creates a server program, do initial setup for web server operation like port number, file directory etc then it run in an infinite loop and wait for requests.


In request object we are going to process the HTTP request coming from client browsers. In Request.java class we are going to process the request and see the URLs browsers are requesting are valid or not.
In request class we are read the request from socket request stream and striping up the requested page name.



Now let work on HTTP response. In this class we create a response and send it back to requesting browser.
So what we are doing here is we are looking at a specific location where all the web files should be present and if the requested page found then simply write it to socket output stream.



Source Code of SimpleHttpWebServer

Welcome to Anindya's Simple Web Server


Thursday, March 17, 2016

Immutable Object


What is Immutable Object?
The immutable object is the object whose state cannot be changed, but immutable object values often required modification and we are not here to declare constants so whenever immutable object values get modified it create a new object with new value.

We need immutable object as a cache key. As in no circumstances we are not allowed to change the cache key.

Here we will see how to create an immutable object and what are the advantages and disadvantages of it.

A class needs to meet certain conditions to become immutable object when we create an object of it. We will also see why this condition needs to be meet in order to get the immutable object.

  1. The class must be declared final. Why? So that no one can extend it. 
  2. All its fields must be final and private. Why? So that no can access from outside of this class and once the value is set then no one can change it. 
  3. Don't allow "setter" methods in order to protect any change. Why? No one can change the value from outside.
  4. Any field which is a type of mutable (value can be changed) object should not directly return the reference. Otherwise, caller method can modify the value of it. 
  5. Any method which modifies any field which is a type of mutable object should be private
  6. The class may have factory method instead of the constructor as it will provide more sophistication. 
  7. In some of the cases where outside code needs to send mutable object to immutable object here things become more tricky. In this case, we need to create a defensive copy of that mutable parameter object and so that immutable class can work on a separate copy.  

ImmutableClass.java
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3. public class ImmutableClass {
  4. private final Date callDate;
  5. private final String friendName;
  6. private final List<String> address = new CopyOnWriteArrayList<String>();
  7. public ImmutableClass(Date date, String name, List<String> modifyData){
  8. callDate = new Date(date.getTime());
  9. friendName = name;
  10. Collections.copy(address,modifyData);
  11. if(callDate.compareTo(new Date()) == 1) {
  12. throw new IllegalArgumentException("Date cannot be greater than today's date. ");
  13. }
  14. modifyAddress();
  15. }
  16. public Date modifyDate(Date date){
  17. return new Date(date.getTime());
  18. }
  19. private void modifyAddress(){
  20. address.add(this.friendName);
  21. }
  22. public List<String> getAddress() {
  23. return Collections.unmodifiableList(address);
  24. }
  25. }

Monday, March 7, 2016

Yet another date time in Java 8

In Java 8 there is new package introduced related to Date and Time java.time package.  
This improvement is done against JSR - 310 by threeten.org  


Why we need another Date/Time class in Java 8 as there is already lots of Date/Time class available in previous Java versions ?

In previous versions of JDK we didn't find good support for date/time operation. Everything thing jumbled up together in Date and Calendar class as a result it is not convenient to use for developer.
Date and Time is wrapped in Date class. So whenever we need to do operations related to date only then we need to remove the time component from Date object. To do simple things developers need to write lots of extra code just to do formatting, getting date/time and day light saving. Using Date class we cannot go below milliseconds. 
More than that Date class have lots of bugs in API and equals method equality breaks when using date object case to case basis. 

In case of Calendar class it better than Date but it still have some problems like date formatting support is not available. We need to covert Calendar object to Date object to do formatting. 


So, what we are going to get in JDK 8  Date and Time ? 

  1. Clear bug free API for date, time and date-time. 
  2. All these objects are thread safe as these objects are immutable. 
  3. Package are organised well and classes are design to cater API extensible.  
Detail about new Java 8 Date-Time 



Examples can be downloaded from https://github.com/andysundar/JDK8