Nice UnAuthorized HTML page
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 00:45:28 +0000 (02:45 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 00:45:28 +0000 (02:45 +0200)
src/main/java/com/prueba/core/http/handles/PagesHandler.java
src/main/java/com/prueba/view/UnAuthorizedImpl.java [new file with mode: 0644]

index 2f1e12e..cf700a4 100644 (file)
@@ -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 (file)
index 0000000..6cd19ce
--- /dev/null
@@ -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();
+       }
+}