add attributes for response for applications

This commit is contained in:
Jörg Prante 2023-03-27 17:12:30 +02:00
parent 407f8ad2f8
commit 08e965d588
4 changed files with 34 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package org.xbib.net.http.server; package org.xbib.net.http.server;
import org.xbib.datastructures.common.Pair; import org.xbib.datastructures.common.Pair;
import org.xbib.net.Attributes;
import org.xbib.net.buffer.DataBuffer; import org.xbib.net.buffer.DataBuffer;
import org.xbib.net.buffer.DataBufferFactory; import org.xbib.net.buffer.DataBufferFactory;
import org.xbib.net.buffer.DefaultDataBufferFactory; 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 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"; private static final String CRLF = "\r\n";
@ -44,6 +52,9 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
protected HttpResponseStatus status; protected HttpResponseStatus status;
/**
* For getting the server name.
*/
protected HttpServerConfig httpServerConfig; protected HttpServerConfig httpServerConfig;
protected boolean shouldClose; protected boolean shouldClose;
@ -76,6 +87,11 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
protected boolean done; protected boolean done;
/**
* Attributes for applications that want to save data in the response.
*/
protected Attributes attributes;
protected BaseHttpResponseBuilder() { protected BaseHttpResponseBuilder() {
reset(); reset();
} }
@ -88,6 +104,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM; this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM;
this.dataBufferFactory = DefaultDataBufferFactory.getInstance(); this.dataBufferFactory = DefaultDataBufferFactory.getInstance();
this.shouldClose = false; // tell client we want to keep the connection alive this.shouldClose = false; // tell client we want to keep the connection alive
this.attributes = new BaseAttributes();
} }
@Override @Override
@ -313,6 +330,11 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
Long.parseLong(headers.get(HttpHeaderNames.CONTENT_LENGTH)) : null; Long.parseLong(headers.get(HttpHeaderNames.CONTENT_LENGTH)) : null;
} }
@Override
public Attributes getAttributes() {
return attributes;
}
@Override @Override
public abstract HttpResponse build(); public abstract HttpResponse build();

View file

@ -70,6 +70,7 @@ public class BaseHttpServerContext implements HttpServerContext {
this.attributes.put("domain", domain); this.attributes.put("domain", domain);
this.attributes.put("requestbuilder", httpRequestBuilder); this.attributes.put("requestbuilder", httpRequestBuilder);
this.attributes.put("responsebuilder", httpResponseBuilder); this.attributes.put("responsebuilder", httpResponseBuilder);
this.attributes.put("ctx", this);
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package org.xbib.net.http.server; package org.xbib.net.http.server;
import org.xbib.net.Attributes;
import org.xbib.net.buffer.DataBuffer; import org.xbib.net.buffer.DataBuffer;
import org.xbib.net.buffer.DataBufferFactory; import org.xbib.net.buffer.DataBufferFactory;
import org.xbib.net.http.HttpHeaders; import org.xbib.net.http.HttpHeaders;
@ -64,6 +65,8 @@ public interface HttpResponseBuilder {
Long getLength(); Long getLength();
Attributes getAttributes();
void reset(); void reset();
HttpResponse build(); HttpResponse build();

View file

@ -7,6 +7,8 @@ import groovy.text.markup.TemplateConfiguration;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.xbib.net.Attributes;
import org.xbib.net.URL; import org.xbib.net.URL;
import org.xbib.net.URLBuilder; import org.xbib.net.URLBuilder;
import org.xbib.net.buffer.DefaultDataBufferFactory; import org.xbib.net.buffer.DefaultDataBufferFactory;
@ -216,6 +218,10 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
return ZonedDateTime.now().format(formatter); return ZonedDateTime.now().format(formatter);
} }
public Attributes getAttributes() {
return responseBuilder.getAttributes();
}
public String bootstrapCss() { public String bootstrapCss() {
return contextPath("webjars/bootstrap/5.2.3/css/bootstrap.min.css"); return contextPath("webjars/bootstrap/5.2.3/css/bootstrap.min.css");
} }