From 773b1a4ea72b8e134281f6e3ac82ec5814fa30cd Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 5 Oct 2016 03:14:12 +0200 Subject: [PATCH] When login and not right credentials return 401 and nice error HTML page --- .../com/prueba/services/impl/LoginServiceImpl.java | 9 ++++++++- src/main/java/com/prueba/view/UnAuthenticated.java | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/prueba/view/UnAuthenticated.java diff --git a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java index 05b885a..da7c551 100644 --- a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java @@ -12,6 +12,7 @@ import com.prueba.core.http.sessions.SessionInfo; import com.prueba.core.http.sessions.Sessions; import com.prueba.model.dao.AccountDao; import com.prueba.view.LoginFormImpl; +import com.prueba.view.UnAuthenticated; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; @@ -78,7 +79,13 @@ public class LoginServiceImpl { Sessions.getInstance().refreshSession(uuid, username); this.doRedirect(httpExchange); } else { - httpExchange.sendResponseHeaders(401, 0); + + final UnAuthenticated view = new UnAuthenticated(); + String html = view.doAuthenticated(); + httpExchange.sendResponseHeaders(401, html.length()); + try (final OutputStream os = httpExchange.getResponseBody()) { + os.write(html.getBytes()); + } } } } else { diff --git a/src/main/java/com/prueba/view/UnAuthenticated.java b/src/main/java/com/prueba/view/UnAuthenticated.java new file mode 100644 index 0000000..464fb5c --- /dev/null +++ b/src/main/java/com/prueba/view/UnAuthenticated.java @@ -0,0 +1,19 @@ +package com.prueba.view; + +import java.io.IOException; + +import org.rendersnake.HtmlCanvas; + +public class UnAuthenticated { + + public String doAuthenticated() throws IOException { + final HtmlCanvas html = new HtmlCanvas(); + return html + .html() + .body() + .h1().content("401 ERROR: YOU ARE NOT AUTHENTICATED") + ._body() + ._html() + .toHtml(); + } +} -- 2.1.4