Sockets, Routing, Sessions, Handlers Hand-Made Server Sockets, Routing, Sessions, Handlers HTTP Server SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Table of Contents Important Information Sockets Web Server Http Classes Request Handlers Routing Cookies and Sessions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Have a Question? sli.do #JavaWeb
Important Information Readme.txt © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
It should not be used in production environment Guideline This is a guideline for creating your own web server It will cover basic concepts, but it will not be complete The tutorial will be focused on the idea behind web servers, not the best practices for creating them. You will be given Interfaces that you need to implement. You decide how to do it. It should not be used in production environment
How to create connection with Java? Sockets How to create connection with Java? © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Server Socket Open a port and listen on it Accept incoming connection from a client Set a timeout for the socket ServerSocket serverSocket = new ServerSocket(8080); Socket clientSocket = serverSocket.accept(); serverSocket.setSoTimeout(10000); 10 seconds timeout
Web Server How to implement one? © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Server Structure Server Socket Open Socket Connection Handler Http Request Http Handler Post Handler Get Handler Close Socket Socket Output Stream Http Response
Server Interface interface Server { void runServer() throws SocketException; } © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Server Implementation The server should keep the ServerSocket After the server is started it should set a timeout to the socket Create a loop that will create connections Each connection should be asynchronous (FutureTask<?>) Create a new class that will handle the connection and implement the Runnable interface
Connection Handler It should keep the client Socket It should keep BufferedReader and PrintWriter, which it will initialize using the socket input and output streams This will be main point of our connection We will implement the run() method later
HttpContext, HttpRequest, HttpResponse Http Classes HttpContext, HttpRequest, HttpResponse © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
HttpRequestType Enumeration GET, POST }
HttpRequest Interface interface HttpRequest { String getURL(); String getPath(); HttpRequestType getRequestType(); String getHeader(String name); Map<String, String> getURLParameters(); Map<String, String> getFormData(); void addParameter(String name, String value); }
HttpContext Interface interface HttpContext { HttpRequest getHttpRequest(); }
HttpResponse Interface interface HttpResponse { String getResponse(); void addResponseHeader(String header, String value); }
HttpRequest Implementation The class should be able to parse any HTTP Request or throw BadRequestException in case it fails The Request Line should hold 3 values The Http version should be 1.1 Request method MUST be GET or POST The URL should contain "/details?user=pesho" The Path should contain "/details" The headers MUST contain the "Host" header.
HttpRequest Implementation (2) The parameters map is filled with the query string parameters The form data map should be filled with the request body if the request method is POST
HttpContext Implementation It should keep the HttpRequest It should take the request string in the constructor and pass it to the HttpRequest class
HttpResponse Implementation It should keep the response headers It should keep the response status It should keep the response body There should be 2 types of response - View and Redirect The response status can be an enum
HttpResponseCode Enumeration enum HttpResponseCode { OK(200, "OK"), MovedPermanently(301, "Moved Permanently"), Created(201, "Created"), Found(302, "Found"), Unauthorized(401, "Unauthorized"); HttpResponseCode(int value, String text) { this.value = value; this.text = text; }
Request Handlers GET and POST handlers Icon made by Freepik from www.flaticon.com Request Handlers GET and POST handlers © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
RequestHandler Interface interface RequestHandler { void handle(HttpContext httpContext) throws IOException; void setWriter(Writer writer); }
RequestHandler Implementation It must be abstract It must implement RequestHandler It should keep Function<HttpContext, HttpResponse> and Writer The constructor must accept the function It should only override the setWriter() method
GetHandler Implementation It must extend the RequestHandler implementation It should take a function in the constructor and send it to the super constructor It should override the handle() method The handle() method should add response headers and write the response
PostHandler Implementation It must extend the RequestHandler implementation It should take a function in the constructor and send it to the super constructor It should override the handle() method The handle() method should add response headers and write the response
Routing Catching URL calls © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
AppRouteConfig Interface interface AppRouteConfig { AppRouteConfig addRoute(String path, RequestHandlerImpl handler); Iterable<Map.Entry<HttpRequestType, Map<String, RequestHandlerImpl>>> getRoutes(); }
AppRouteConfig Implementation It should keep a map with all of the request types as keys and other map as value The inner map should keep string that is the route and RequestHandlerImpl, which will be the Get or Post handler The addRoute() method should put the new route in the corresponding inner map Example of a valid route /users/{(?<id>[0-9]+)}/edit
RoutingContext Interface interface RoutingContext { RequestHandlerImpl getHandler(); Iterable<String> getParamNames(); }
RoutingContext Implementation It will keep the info about a route The handler that needs to handle the route The parameter names that the route needs
ServerRouteConfig Interface interface ServerRouteConfig { Map<HttpRequestType, Map<String, RoutingContext>> getRoutes(); }
ServerRouteConfig Implementation It should keep a map with key request type and value map of string and RoutingContext It should traverse all of the AppRouteConfig routes and parse them For each route it should create a RoutingContext that will keep the info about the route Example of a route after being parsed /users/{(?<id>[0-9]+)}/edit ^/users/(?<id>[0-9]+)/edit$
HttpHandler Implementation It should implement RequestHandler It should keep the ServerRouteConfig It should parse the route and call the corresponding handler from the ServerRouteConfig
Cookies and Sessions Saving user info © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
HttpCookie Interface interface HttpCookie { void addCookie(String key, String value); void removeCookie(String key); boolean contains(String key); String getCookie(String name); }
HttpCookie Implementation It should keep Map<String, String> The add(), remove(), contains() and get() methods should use the map
HttpSession Interface String getId(); void clear(); void add(String key, String value); String get(String key); boolean isAuthenticated(); }
HttpSession Implementation The ID should be random generated The clear() method should clear the session parameters collection The isAuthenticated() method should check if there is any data in the session parameters collection The other methods should do the corresponding operations over the collection
SessionCreator Singleton private SecureRandom random; //TODO: Initialize the random and make the class singleton public String generateSessionId() { return new BigInteger(130, this.random).toString(32); }
HttpRequest Modifications interface HttpRequest { … HttpCookie getCookie(); HttpSession getSession(); void setSession(HttpSession session); }
RequestHandlerImpl Modification Create a new method called createSession(HttpContext httpContext, HttpResponse result) Check if the session is null of if the cookie doesn't contain the key "sessionId" If any of the above is true create a new session and set the cookie The cookie should be "HttpOnly"
Other Modifications The server implementation should keep a map containing all of the sessions The AppRouteConfig should be passed to the server
Hand-Made Server https://softuni.bg/courses/java-web-development-basics © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Trainings @ Software University (SoftUni) Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.