Download presentation
Presentation is loading. Please wait.
1
Sockets, Routing, Sessions, Handlers
Hand-Made Server Sockets, Routing, Sessions, Handlers HTTP Server SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
2
Table of Contents Important Information Sockets Web Server
Http Classes Request Handlers Routing Cookies and Sessions © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
3
Have a Question? sli.do #JavaWeb
4
Important Information
Readme.txt © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
5
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
6
How to create connection with Java?
Sockets How to create connection with Java? © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
7
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
8
Web Server How to implement one?
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
9
Server Structure Server Socket Open Socket Connection Handler Http
Request Http Handler Post Handler Get Handler Close Socket Socket Output Stream Http Response
10
Server Interface interface Server {
void runServer() throws SocketException; } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
11
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
12
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
13
HttpContext, HttpRequest, HttpResponse
Http Classes HttpContext, HttpRequest, HttpResponse © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
14
HttpRequestType Enumeration
GET, POST }
15
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); }
16
HttpContext Interface
interface HttpContext { HttpRequest getHttpRequest(); }
17
HttpResponse Interface
interface HttpResponse { String getResponse(); void addResponseHeader(String header, String value); }
18
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.
19
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
20
HttpContext Implementation
It should keep the HttpRequest It should take the request string in the constructor and pass it to the HttpRequest class
21
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
22
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; }
23
Request Handlers GET and POST handlers
Icon made by Freepik from Request Handlers GET and POST handlers © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
24
RequestHandler Interface
interface RequestHandler { void handle(HttpContext httpContext) throws IOException; void setWriter(Writer writer); }
25
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
26
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
27
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
28
Routing Catching URL calls
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
29
AppRouteConfig Interface
interface AppRouteConfig { AppRouteConfig addRoute(String path, RequestHandlerImpl handler); Iterable<Map.Entry<HttpRequestType, Map<String, RequestHandlerImpl>>> getRoutes(); }
30
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
31
RoutingContext Interface
interface RoutingContext { RequestHandlerImpl getHandler(); Iterable<String> getParamNames(); }
32
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
33
ServerRouteConfig Interface
interface ServerRouteConfig { Map<HttpRequestType, Map<String, RoutingContext>> getRoutes(); }
34
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$
35
HttpHandler Implementation
It should implement RequestHandler It should keep the ServerRouteConfig It should parse the route and call the corresponding handler from the ServerRouteConfig
36
Cookies and Sessions Saving user info
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
37
HttpCookie Interface interface HttpCookie {
void addCookie(String key, String value); void removeCookie(String key); boolean contains(String key); String getCookie(String name); }
38
HttpCookie Implementation
It should keep Map<String, String> The add(), remove(), contains() and get() methods should use the map
39
HttpSession Interface
String getId(); void clear(); void add(String key, String value); String get(String key); boolean isAuthenticated(); }
40
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
41
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); }
42
HttpRequest Modifications
interface HttpRequest { … HttpCookie getCookie(); HttpSession getSession(); void setSession(HttpSession session); }
43
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"
44
Other Modifications The server implementation should keep a map containing all of the sessions The AppRouteConfig should be passed to the server
45
Hand-Made Server © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
46
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 – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
47
Trainings @ Software University (SoftUni)
Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.