Sockets, Routing, Sessions, Handlers

Slides:



Advertisements
Similar presentations
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Advertisements

AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Version Control Systems
Helpers, Data Validation
Introduction to Servlets
Static Members and Namespaces
Introduction to Entity framework
Abstract Classes, Abstract Methods, Override Methods
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
Services & Dependency Injection
C# MVC Frameworks – ASP.NET
C# Basic Syntax, Visual Studio, Console Input / Output
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Cookies, Sessions, Bootstrap
Deploying Web Application
Thymeleaf and Spring Controllers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
Module Development Part 2 Magento 1 Stenik Group Ltd. Martin Grozdanov
Introduction to Entity Framework
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Mocking tools for easier unit testing
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
OOP Course - Virtual Trip
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
EF Relations Object Composition
Heaps and Priority Queues
Entity Framework: Code First
Registration, Login, Thymeleaf
Basic Tree Data Structures
Data Definition and Data Types
C#/Java Web Development Basics
MVC Architecture. Routing
Entity Framework: Relations
Functional Programming
The Right Way Control Flow
MVC Architecture, Symfony Framework for PHP Web Apps
ASP.NET SignalR SoftUni Team C# MVC Frameworks Technical Trainers
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best practices and architecture
Testing with Spring Boot
Design & Module Development
WordPress Plugins Popular WP Plugins: Sliders, Forms, Contacts, SEO, Forum, Photo Gallery, e-Commerce WordPress Plugins SoftUni Team Technical Trainers.
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
ASP.NET Filters SoftUni Team ASP.NET MVC Introduction
Making big SPA applications
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
Exporting and Importing Data
Scheduled Tasks and Web Socket
Introduction to TypeScript & Angular
Train the Trainers Course
Spring Data Advanced Querying
Version Control Systems
Polymorphism, Interfaces, Abstract Classes
JavaScript: ExpressJS Overview
Iterators and Generators
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Presentation transcript:

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.