package com.prueba.core.http.handles;
import java.io.IOException;
+import java.io.OutputStream;
import java.net.URI;
import org.slf4j.Logger;
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;
pagesController.handle(httpExchange);
} else {
- httpExchange.sendResponseHeaders(403, 0);
+ this.doErrorResponse(httpExchange);
}
Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
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());
+ }
+ }
}
--- /dev/null
+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();
+ }
+}