From 51c66bd27c8919abe6b7ce7bede971aa1e19a45f Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 5 Oct 2016 02:45:28 +0200 Subject: [PATCH] Nice UnAuthorized HTML page --- .../com/prueba/core/http/handles/PagesHandler.java | 14 +++++++++++++- src/main/java/com/prueba/view/UnAuthorizedImpl.java | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/prueba/view/UnAuthorizedImpl.java diff --git a/src/main/java/com/prueba/core/http/handles/PagesHandler.java b/src/main/java/com/prueba/core/http/handles/PagesHandler.java index 2f1e12e..cf700a4 100644 --- a/src/main/java/com/prueba/core/http/handles/PagesHandler.java +++ b/src/main/java/com/prueba/core/http/handles/PagesHandler.java @@ -1,6 +1,7 @@ package com.prueba.core.http.handles; import java.io.IOException; +import java.io.OutputStream; import java.net.URI; import org.slf4j.Logger; @@ -11,6 +12,7 @@ import com.prueba.core.http.sessions.SessionContext; import com.prueba.core.http.sessions.SessionInfo; import com.prueba.core.http.sessions.Sessions; import com.prueba.services.impl.AuthorizationServicesImpl; +import com.prueba.view.UnAuthorizedImpl; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @@ -55,7 +57,7 @@ public class PagesHandler implements HttpHandler { pagesController.handle(httpExchange); } else { - httpExchange.sendResponseHeaders(403, 0); + this.doErrorResponse(httpExchange); } Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); @@ -72,4 +74,14 @@ public class PagesHandler implements HttpHandler { responseHeaders.add("Location", SERVER_ADDRESS + LoginHandler.LOGIN_PAGE + requestURIString); httpExchange.sendResponseHeaders(302, 0); } + + protected void doErrorResponse(HttpExchange httpExchange) throws IOException { + final UnAuthorizedImpl view = new UnAuthorizedImpl(); + final String html = view.doNotAuthorized(); + + httpExchange.sendResponseHeaders(403, html.length()); + try (final OutputStream os = httpExchange.getResponseBody()) { + os.write(html.getBytes()); + } + } } diff --git a/src/main/java/com/prueba/view/UnAuthorizedImpl.java b/src/main/java/com/prueba/view/UnAuthorizedImpl.java new file mode 100644 index 0000000..6cd19ce --- /dev/null +++ b/src/main/java/com/prueba/view/UnAuthorizedImpl.java @@ -0,0 +1,19 @@ +package com.prueba.view; + +import java.io.IOException; + +import org.rendersnake.HtmlCanvas; + +public class UnAuthorizedImpl { + + public String doNotAuthorized() throws IOException { + final HtmlCanvas html = new HtmlCanvas(); + return html + .html() + .body() + .h1().content("403 ERROR: YOU ARE NOT AUTHORIZED") + ._body() + ._html() + .toHtml(); + } +} -- 2.1.4