rename renderBody to renderHtml to avoid misconceptions, add some j2html error handlers, update to Netty 4.1.109

This commit is contained in:
Jörg Prante 2024-04-17 11:26:08 +02:00
parent c56634b281
commit caf267fd9a
9 changed files with 122 additions and 25 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.4.4 version = 4.5.0

View file

@ -2,6 +2,7 @@ package org.xbib.net.http.j2html;
import org.xbib.net.Attributes; import org.xbib.net.Attributes;
import org.xbib.net.Resource; 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.resource.HtmlTemplateResourceHandler;
import org.xbib.net.http.server.route.HttpRouterContext; import org.xbib.net.http.server.route.HttpRouterContext;
import org.xbib.net.util.ExceptionFormatter; 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.code;
import static org.xbib.j2html.TagCreator.div; import static org.xbib.j2html.TagCreator.div;
import static org.xbib.j2html.TagCreator.h1; import static org.xbib.j2html.TagCreator.h1;
import static org.xbib.j2html.TagCreator.html;
import static org.xbib.j2html.TagCreator.pre; import static org.xbib.j2html.TagCreator.pre;
public class InternalServerErrorHandler extends J2HtmlResourceHandler { public class InternalServerErrorHandler extends J2HtmlResourceHandler {
@ -23,21 +25,23 @@ public class InternalServerErrorHandler extends J2HtmlResourceHandler {
protected static class InternalServerErrorResource extends J2HtmlResource { 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); super(templateResourceHandler, httpRouterContext);
} }
@Override @Override
protected String renderBody(HttpRouterContext httpRouterContext) { protected HttpResponseStatus getResponseStatus() {
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
@Override
protected String renderHtml(HttpRouterContext httpRouterContext) {
Attributes attributes = httpRouterContext.getAttributes(); Attributes attributes = httpRouterContext.getAttributes();
String message = attributes.get(String.class, "_message"); String message = attributes.get(String.class, "_message");
Throwable throwable = attributes.get(Throwable.class, "_throwable"); Throwable throwable = attributes.get(Throwable.class, "_throwable");
String stackTrace = ExceptionFormatter.format(throwable); String stackTrace = ExceptionFormatter.format(throwable);
return body( return html(body(h1("Internal Server Error"), div(message), pre(code(stackTrace)))).render();
h1("Internal Server Error"),
div(message),
pre(code(stackTrace))
).render();
} }
} }
} }

View file

@ -29,6 +29,6 @@ public class J2HtmlAuthResourceHandler extends HtmlTemplateResourceHandler {
} }
protected Resource createUnauthenticatedResource(HttpRouterContext httpRouterContext) throws IOException { protected Resource createUnauthenticatedResource(HttpRouterContext httpRouterContext) throws IOException {
return new J2HtmlResource(this, httpRouterContext); return new UnauthorizedHandler.UnauthorizedResource(this, httpRouterContext);
} }
} }

View file

@ -8,11 +8,13 @@ import org.xbib.net.http.server.resource.HtmlTemplateResourceHandler;
import org.xbib.net.http.server.route.HttpRouterContext; import org.xbib.net.http.server.route.HttpRouterContext;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Objects; import java.util.Objects;
import static org.xbib.j2html.TagCreator.body; import static org.xbib.j2html.TagCreator.body;
import static org.xbib.j2html.TagCreator.h1; import static org.xbib.j2html.TagCreator.h1;
import static org.xbib.j2html.TagCreator.html;
import static org.xbib.net.http.HttpHeaderNames.CONTENT_TYPE; import static org.xbib.net.http.HttpHeaderNames.CONTENT_TYPE;
public class J2HtmlResource extends HtmlTemplateResource { public class J2HtmlResource extends HtmlTemplateResource {
@ -29,29 +31,39 @@ public class J2HtmlResource extends HtmlTemplateResource {
this.request = context.getRequest(); this.request = context.getRequest();
Objects.requireNonNull(responseBuilder); Objects.requireNonNull(responseBuilder);
DataBuffer dataBuffer = context.getDataBufferFactory().allocateBuffer(); DataBuffer dataBuffer = context.getDataBufferFactory().allocateBuffer();
dataBuffer.write(renderBody(context), StandardCharsets.UTF_8); dataBuffer.write(renderHtml(context), getCharset());
HttpResponseStatus httpResponseStatus = responseBuilder.getResponseStatus(); HttpResponseStatus httpResponseStatus = responseBuilder.getResponseStatus();
if (httpResponseStatus == null) { if (httpResponseStatus == null) {
httpResponseStatus = HttpResponseStatus.OK; httpResponseStatus = getResponseStatus();
} }
context.status(httpResponseStatus) 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-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); .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. * 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 * @param context the router context
* @return the body string fo the HTTP response * @return the body string fo the HTTP response
*/ */
protected String renderBody(HttpRouterContext context) { protected String renderHtml(HttpRouterContext context) {
return body( return html(body(h1("Hello World"))).render();
h1("Hello World")
).render();
} }
} }

View file

@ -60,10 +60,15 @@ public class J2HtmlServiceBuilder extends BaseHttpServiceBuilder {
} }
public J2HtmlService build() { 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"); HttpHandler httpHandler = new J2HtmlResourceHandler(prefix, suffix, "index.java");
setHandler(httpHandler); setHandler(httpHandler);
} }
}
return new J2HtmlService(this); return new J2HtmlService(this);
} }
} }

View file

@ -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();
}
}
}

View file

@ -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();
}
}
}

View file

@ -223,7 +223,7 @@ public final class Bootstrap {
super(templateResourceHandler, httpRouterContext); super(templateResourceHandler, httpRouterContext);
} }
protected String renderBody(HttpRouterContext context) { protected String renderHtml(HttpRouterContext context) {
return body( return body(
h1("This is a demo") h1("This is a demo")
).render(); ).render();
@ -237,7 +237,7 @@ public final class Bootstrap {
super(templateResourceHandler, httpRouterContext); super(templateResourceHandler, httpRouterContext);
} }
protected String renderBody(HttpRouterContext context) { protected String renderHtml(HttpRouterContext context) {
return body( return body(
h1("This is a demo, unauthenticated") h1("This is a demo, unauthenticated")
).render(); ).render();

View file

@ -3,7 +3,7 @@ dependencyResolutionManagement {
libs { libs {
version('gradle', '8.5') version('gradle', '8.5')
version('groovy', '4.0.20') 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('netty-tcnative', '2.0.65.Final')
version('datastructures', '5.0.7') version('datastructures', '5.0.7')
version('net', '4.3.0') version('net', '4.3.0')