rename renderBody to renderHtml to avoid misconceptions, add some j2html error handlers, update to Netty 4.1.109
This commit is contained in:
parent
c56634b281
commit
caf267fd9a
9 changed files with 122 additions and 25 deletions
|
@ -1,3 +1,3 @@
|
|||
group = org.xbib
|
||||
name = net-http
|
||||
version = 4.4.4
|
||||
version = 4.5.0
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.net.http.j2html;
|
|||
|
||||
import org.xbib.net.Attributes;
|
||||
import org.xbib.net.Resource;
|
||||
import org.xbib.net.http.HttpResponseStatus;
|
||||
import org.xbib.net.http.server.resource.HtmlTemplateResourceHandler;
|
||||
import org.xbib.net.http.server.route.HttpRouterContext;
|
||||
import org.xbib.net.util.ExceptionFormatter;
|
||||
|
@ -12,6 +13,7 @@ import static org.xbib.j2html.TagCreator.body;
|
|||
import static org.xbib.j2html.TagCreator.code;
|
||||
import static org.xbib.j2html.TagCreator.div;
|
||||
import static org.xbib.j2html.TagCreator.h1;
|
||||
import static org.xbib.j2html.TagCreator.html;
|
||||
import static org.xbib.j2html.TagCreator.pre;
|
||||
|
||||
public class InternalServerErrorHandler extends J2HtmlResourceHandler {
|
||||
|
@ -23,21 +25,23 @@ public class InternalServerErrorHandler extends J2HtmlResourceHandler {
|
|||
|
||||
protected static class InternalServerErrorResource extends J2HtmlResource {
|
||||
|
||||
protected InternalServerErrorResource(HtmlTemplateResourceHandler templateResourceHandler, HttpRouterContext httpRouterContext) throws IOException {
|
||||
protected InternalServerErrorResource(HtmlTemplateResourceHandler templateResourceHandler,
|
||||
HttpRouterContext httpRouterContext) throws IOException {
|
||||
super(templateResourceHandler, httpRouterContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String renderBody(HttpRouterContext httpRouterContext) {
|
||||
protected HttpResponseStatus getResponseStatus() {
|
||||
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String renderHtml(HttpRouterContext httpRouterContext) {
|
||||
Attributes attributes = httpRouterContext.getAttributes();
|
||||
String message = attributes.get(String.class, "_message");
|
||||
Throwable throwable = attributes.get(Throwable.class, "_throwable");
|
||||
String stackTrace = ExceptionFormatter.format(throwable);
|
||||
return body(
|
||||
h1("Internal Server Error"),
|
||||
div(message),
|
||||
pre(code(stackTrace))
|
||||
).render();
|
||||
return html(body(h1("Internal Server Error"), div(message), pre(code(stackTrace)))).render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,6 @@ public class J2HtmlAuthResourceHandler extends HtmlTemplateResourceHandler {
|
|||
}
|
||||
|
||||
protected Resource createUnauthenticatedResource(HttpRouterContext httpRouterContext) throws IOException {
|
||||
return new J2HtmlResource(this, httpRouterContext);
|
||||
return new UnauthorizedHandler.UnauthorizedResource(this, httpRouterContext);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,13 @@ import org.xbib.net.http.server.resource.HtmlTemplateResourceHandler;
|
|||
import org.xbib.net.http.server.route.HttpRouterContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.xbib.j2html.TagCreator.body;
|
||||
import static org.xbib.j2html.TagCreator.h1;
|
||||
import static org.xbib.j2html.TagCreator.html;
|
||||
import static org.xbib.net.http.HttpHeaderNames.CONTENT_TYPE;
|
||||
|
||||
public class J2HtmlResource extends HtmlTemplateResource {
|
||||
|
@ -29,29 +31,39 @@ public class J2HtmlResource extends HtmlTemplateResource {
|
|||
this.request = context.getRequest();
|
||||
Objects.requireNonNull(responseBuilder);
|
||||
DataBuffer dataBuffer = context.getDataBufferFactory().allocateBuffer();
|
||||
dataBuffer.write(renderBody(context), StandardCharsets.UTF_8);
|
||||
dataBuffer.write(renderHtml(context), getCharset());
|
||||
HttpResponseStatus httpResponseStatus = responseBuilder.getResponseStatus();
|
||||
if (httpResponseStatus == null) {
|
||||
httpResponseStatus = HttpResponseStatus.OK;
|
||||
httpResponseStatus = getResponseStatus();
|
||||
}
|
||||
context.status(httpResponseStatus)
|
||||
.header("cache-control", "no-cache") // override default must-revalidate behavior
|
||||
.header("cache-control", cacheControl()) // override default must-revalidate behavior
|
||||
.header("content-length", Integer.toString(dataBuffer.writePosition()))
|
||||
.header(CONTENT_TYPE, "text/html; charset=" + StandardCharsets.UTF_8.displayName())
|
||||
.header(CONTENT_TYPE, "text/html; charset=" + getCharset().displayName())
|
||||
.body(dataBuffer);
|
||||
}
|
||||
|
||||
protected String cacheControl() {
|
||||
return "no-cache";
|
||||
}
|
||||
|
||||
protected HttpResponseStatus getResponseStatus() {
|
||||
return HttpResponseStatus.OK;
|
||||
}
|
||||
|
||||
protected Charset getCharset() {
|
||||
return StandardCharsets.UTF_8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rendering a body.
|
||||
* Rendering.
|
||||
* By subclassing this handler, this method should be overridden by custom j2html code.
|
||||
* The body here is just a greeting as an example.
|
||||
* The HTML is here is just a greeting as an example.
|
||||
*
|
||||
* @param context the router context
|
||||
* @return the body string fo the HTTP response
|
||||
*/
|
||||
protected String renderBody(HttpRouterContext context) {
|
||||
return body(
|
||||
h1("Hello World")
|
||||
).render();
|
||||
protected String renderHtml(HttpRouterContext context) {
|
||||
return html(body(h1("Hello World"))).render();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,10 +60,15 @@ public class J2HtmlServiceBuilder extends BaseHttpServiceBuilder {
|
|||
}
|
||||
|
||||
public J2HtmlService build() {
|
||||
if (this.handlers == null) {
|
||||
if (handlers == null) {
|
||||
if (securityDomain != null) {
|
||||
HttpHandler httpHandler = new J2HtmlAuthResourceHandler(prefix, suffix, "index.java");
|
||||
setHandler(httpHandler);
|
||||
} else {
|
||||
HttpHandler httpHandler = new J2HtmlResourceHandler(prefix, suffix, "index.java");
|
||||
setHandler(httpHandler);
|
||||
}
|
||||
}
|
||||
return new J2HtmlService(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package org.xbib.net.http.j2html;
|
||||
|
||||
import org.xbib.net.Resource;
|
||||
import org.xbib.net.http.HttpResponseStatus;
|
||||
import org.xbib.net.http.server.resource.HtmlTemplateResourceHandler;
|
||||
import org.xbib.net.http.server.route.HttpRouterContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.xbib.j2html.TagCreator.body;
|
||||
import static org.xbib.j2html.TagCreator.h1;
|
||||
import static org.xbib.j2html.TagCreator.html;
|
||||
|
||||
public class NotFoundHandler extends J2HtmlResourceHandler {
|
||||
|
||||
@Override
|
||||
protected Resource createResource(HttpRouterContext httpRouterContext) throws IOException {
|
||||
return new UnauthorizedResource(this, httpRouterContext);
|
||||
}
|
||||
|
||||
protected static class UnauthorizedResource extends J2HtmlResource {
|
||||
|
||||
protected UnauthorizedResource(HtmlTemplateResourceHandler templateResourceHandler,
|
||||
HttpRouterContext httpRouterContext) throws IOException {
|
||||
super(templateResourceHandler, httpRouterContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpResponseStatus getResponseStatus() {
|
||||
return HttpResponseStatus.NOT_FOUND;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String renderHtml(HttpRouterContext httpRouterContext) {
|
||||
return html(body(h1("Not found"))).render();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package org.xbib.net.http.j2html;
|
||||
|
||||
import org.xbib.net.Resource;
|
||||
import org.xbib.net.http.HttpResponseStatus;
|
||||
import org.xbib.net.http.server.resource.HtmlTemplateResourceHandler;
|
||||
import org.xbib.net.http.server.route.HttpRouterContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.xbib.j2html.TagCreator.body;
|
||||
import static org.xbib.j2html.TagCreator.h1;
|
||||
import static org.xbib.j2html.TagCreator.html;
|
||||
|
||||
public class UnauthorizedHandler extends J2HtmlResourceHandler {
|
||||
|
||||
@Override
|
||||
protected Resource createResource(HttpRouterContext httpRouterContext) throws IOException {
|
||||
return new UnauthorizedResource(this, httpRouterContext);
|
||||
}
|
||||
|
||||
protected static class UnauthorizedResource extends J2HtmlResource {
|
||||
|
||||
protected UnauthorizedResource(HtmlTemplateResourceHandler templateResourceHandler,
|
||||
HttpRouterContext httpRouterContext) throws IOException {
|
||||
super(templateResourceHandler, httpRouterContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpResponseStatus getResponseStatus() {
|
||||
return HttpResponseStatus.UNAUTHORIZED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String renderHtml(HttpRouterContext httpRouterContext) {
|
||||
return html(body(h1("Unauthorized"))).render();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -223,7 +223,7 @@ public final class Bootstrap {
|
|||
super(templateResourceHandler, httpRouterContext);
|
||||
}
|
||||
|
||||
protected String renderBody(HttpRouterContext context) {
|
||||
protected String renderHtml(HttpRouterContext context) {
|
||||
return body(
|
||||
h1("This is a demo")
|
||||
).render();
|
||||
|
@ -237,7 +237,7 @@ public final class Bootstrap {
|
|||
super(templateResourceHandler, httpRouterContext);
|
||||
}
|
||||
|
||||
protected String renderBody(HttpRouterContext context) {
|
||||
protected String renderHtml(HttpRouterContext context) {
|
||||
return body(
|
||||
h1("This is a demo, unauthenticated")
|
||||
).render();
|
||||
|
|
|
@ -3,7 +3,7 @@ dependencyResolutionManagement {
|
|||
libs {
|
||||
version('gradle', '8.5')
|
||||
version('groovy', '4.0.20')
|
||||
version('netty', '4.1.108.Final')
|
||||
version('netty', '4.1.109.Final')
|
||||
version('netty-tcnative', '2.0.65.Final')
|
||||
version('datastructures', '5.0.7')
|
||||
version('net', '4.3.0')
|
||||
|
|
Loading…
Reference in a new issue