When login and not right credentials return 401 and nice error HTML page
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 01:14:12 +0000 (03:14 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 01:14:12 +0000 (03:14 +0200)
src/main/java/com/prueba/services/impl/LoginServiceImpl.java
src/main/java/com/prueba/view/UnAuthenticated.java [new file with mode: 0644]

index 05b885a..da7c551 100644 (file)
@@ -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 (file)
index 0000000..464fb5c
--- /dev/null
@@ -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();
+       }
+}