From 08e965d588ddafba25d5247f721f815b0c08563a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Mon, 27 Mar 2023 17:12:30 +0200 Subject: [PATCH] add attributes for response for applications --- .../http/server/BaseHttpResponseBuilder.java | 26 +++++++++++++++++-- .../http/server/BaseHttpServerContext.java | 1 + .../net/http/server/HttpResponseBuilder.java | 3 +++ .../groovy/DefaultMarkupTemplate.java | 6 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java index 90df83b..78a7266 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java @@ -1,6 +1,7 @@ package org.xbib.net.http.server; import org.xbib.datastructures.common.Pair; +import org.xbib.net.Attributes; import org.xbib.net.buffer.DataBuffer; import org.xbib.net.buffer.DataBufferFactory; import org.xbib.net.buffer.DefaultDataBufferFactory; @@ -28,9 +29,16 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { private static final Logger logger = Logger.getLogger(BaseHttpResponseBuilder.class.getName()); - private static final String SPACE = " "; + /** + * A space character for constructing the response. + */ + private static final Character SPACE = ' '; - private static final String COLON = ":"; + + /** + * A colon character for header construction. + */ + private static final Character COLON = ':'; private static final String CRLF = "\r\n"; @@ -44,6 +52,9 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { protected HttpResponseStatus status; + /** + * For getting the server name. + */ protected HttpServerConfig httpServerConfig; protected boolean shouldClose; @@ -76,6 +87,11 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { protected boolean done; + /** + * Attributes for applications that want to save data in the response. + */ + protected Attributes attributes; + protected BaseHttpResponseBuilder() { reset(); } @@ -88,6 +104,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM; this.dataBufferFactory = DefaultDataBufferFactory.getInstance(); this.shouldClose = false; // tell client we want to keep the connection alive + this.attributes = new BaseAttributes(); } @Override @@ -313,6 +330,11 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { Long.parseLong(headers.get(HttpHeaderNames.CONTENT_LENGTH)) : null; } + @Override + public Attributes getAttributes() { + return attributes; + } + @Override public abstract HttpResponse build(); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java index 5f09771..7773efa 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java @@ -70,6 +70,7 @@ public class BaseHttpServerContext implements HttpServerContext { this.attributes.put("domain", domain); this.attributes.put("requestbuilder", httpRequestBuilder); this.attributes.put("responsebuilder", httpResponseBuilder); + this.attributes.put("ctx", this); } @Override diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/HttpResponseBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/HttpResponseBuilder.java index bca3ee8..dc93162 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/HttpResponseBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/HttpResponseBuilder.java @@ -1,5 +1,6 @@ package org.xbib.net.http.server; +import org.xbib.net.Attributes; import org.xbib.net.buffer.DataBuffer; import org.xbib.net.buffer.DataBufferFactory; import org.xbib.net.http.HttpHeaders; @@ -64,6 +65,8 @@ public interface HttpResponseBuilder { Long getLength(); + Attributes getAttributes(); + void reset(); HttpResponse build(); diff --git a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/DefaultMarkupTemplate.java b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/DefaultMarkupTemplate.java index 3bb7a89..55a13c8 100644 --- a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/DefaultMarkupTemplate.java +++ b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/DefaultMarkupTemplate.java @@ -7,6 +7,8 @@ import groovy.text.markup.TemplateConfiguration; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; + +import org.xbib.net.Attributes; import org.xbib.net.URL; import org.xbib.net.URLBuilder; import org.xbib.net.buffer.DefaultDataBufferFactory; @@ -216,6 +218,10 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate { return ZonedDateTime.now().format(formatter); } + public Attributes getAttributes() { + return responseBuilder.getAttributes(); + } + public String bootstrapCss() { return contextPath("webjars/bootstrap/5.2.3/css/bootstrap.min.css"); }