From: Gustavo Martin Morcuende Date: Wed, 5 Oct 2016 01:14:12 +0000 (+0200) Subject: When login and not right credentials return 401 and nice error HTML page X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=773b1a4ea72b8e134281f6e3ac82ec5814fa30cd;p=WebAppTest%2F.git When login and not right credentials return 401 and nice error HTML page --- 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(); + } +}