remove write(String) from HttpResponseBuilder, we don't like sloppyness, try enforcing charset use

This commit is contained in:
Jörg Prante 2023-08-07 18:08:25 +02:00
parent 0d71313cf4
commit 9287e6c36a
11 changed files with 79 additions and 61 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib
name = net-http
version = 3.5.0
version = 3.6.0
org.gradle.warning.mode = ALL

View file

@ -30,7 +30,6 @@ import java.io.UncheckedIOException;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -127,8 +126,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
@Override
public HttpResponse build() {
Objects.requireNonNull(ctx);
if (body != null) {
internalStringWrite(body);
if (bytes != null) {
internalByteWrite(bytes);
} else if (charBuffer != null && charset != null) {
internalBufferWrite(charBuffer, charset);
} else if (dataBuffer != null) {
@ -155,8 +154,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
super.release();
}
private void internalStringWrite(String body) {
internalBufferWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(body)));
private void internalByteWrite(byte[] bytes) {
internalBufferWrite(dataBufferFactory.wrap(bytes));
}
private void internalBufferWrite(DataBuffer dataBuffer) {

View file

@ -38,8 +38,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
public HttpResponse build() {
Objects.requireNonNull(outputStream);
try {
if (body != null) {
internalWrite(body);
if (bytes != null) {
internalWrite(bytes);
} else if (charBuffer != null && charset != null) {
internalWrite(charBuffer, charset);
} else if (dataBuffer != null) {
@ -73,20 +73,11 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
outputStream.close();
}
void internalWrite(String string) throws IOException {
if (string == null) {
void internalWrite(byte[] bytes) throws IOException {
if (bytes == null) {
internalFlush();
} else {
write(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(string)));
}
}
void internalWrite(CharBuffer charBuffer, Charset charset) throws IOException {
if (charBuffer == null) {
internalFlush();
} else {
Objects.requireNonNull(charset);
write(dataBufferFactory.wrap(charset.encode(charBuffer)));
write(dataBufferFactory.wrap(bytes));
}
}
@ -103,6 +94,15 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
}
}
void internalWrite(CharBuffer charBuffer, Charset charset) throws IOException {
if (charBuffer == null) {
internalFlush();
} else {
Objects.requireNonNull(charset);
write(dataBufferFactory.wrap(charset.encode(charBuffer)));
}
}
void internalWrite(InputStream inputStream, int bufferSize) throws IOException {
byte[] b = new byte[bufferSize];
while (inputStream.available() > 0) {

View file

@ -38,8 +38,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
public HttpResponse build() {
Objects.requireNonNull(outputStream);
try {
if (body != null) {
internalWrite(body);
if (bytes != null) {
internalWrite(bytes);
} else if (charBuffer != null && charset != null) {
internalWrite(charBuffer, charset);
} else if (dataBuffer != null) {
@ -72,21 +72,11 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
outputStream.close();
}
void internalWrite(String string) throws IOException {
if (string == null) {
void internalWrite(byte[] bytes) throws IOException {
if (bytes == null) {
internalFlush();
} else {
logger.log(Level.INFO, "internal write: " + string);
internalWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(string)));
}
}
void internalWrite(CharBuffer charBuffer, Charset charset) throws IOException {
if (charBuffer == null) {
internalFlush();
} else {
Objects.requireNonNull(charset);
internalWrite(dataBufferFactory.wrap(charset.encode(charBuffer)));
internalWrite(dataBufferFactory.wrap(bytes));
}
}
@ -105,6 +95,15 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
}
}
void internalWrite(CharBuffer charBuffer, Charset charset) throws IOException {
if (charBuffer == null) {
internalFlush();
} else {
Objects.requireNonNull(charset);
internalWrite(dataBufferFactory.wrap(charset.encode(charBuffer)));
}
}
void internalWrite(InputStream inputStream, int bufferSize) throws IOException {
byte[] b = new byte[bufferSize];
while (inputStream.available() > 0) {

View file

@ -71,7 +71,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
protected Charset charset;
protected String body;
protected byte[] bytes;
protected CharBuffer charBuffer;
@ -260,11 +260,9 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
}
@Override
public BaseHttpResponseBuilder write(String body) {
if (body != null && this.body == null) {
this.body = body;
} else {
logger.log(Level.WARNING, "cannot write more than one body");
public BaseHttpResponseBuilder write(byte[] bytes) {
if (bytes != null) {
this.bytes = bytes;
}
return this;
}

View file

@ -52,12 +52,12 @@ public interface HttpResponseBuilder {
HttpResponseBuilder setResponseId(Long responseId);
HttpResponseBuilder write(String body);
HttpResponseBuilder write(CharBuffer charBuffer, Charset charset);
HttpResponseBuilder write(byte[] bytes);
HttpResponseBuilder write(DataBuffer dataBuffer);
HttpResponseBuilder write(CharBuffer charBuffer, Charset charset);
HttpResponseBuilder write(InputStream inputStream, int bufferSize);
HttpResponseBuilder write(FileChannel fileChannel, int bufferSize);

View file

@ -7,6 +7,8 @@ import java.time.ZoneId;
import java.util.Collection;
import java.util.Locale;
import java.util.Set;
import org.xbib.net.Attributes;
import org.xbib.net.http.HttpAddress;
import org.xbib.net.http.server.HttpRequestBuilder;
import org.xbib.net.http.server.HttpResponseBuilder;
@ -40,6 +42,8 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
Collection<ApplicationModule> getModules();
Attributes getAttributes();
HttpRouterContext createContext(HttpDomain domain,
HttpRequestBuilder httpRequestBuilder,
HttpResponseBuilder httpResponseBuilder);
@ -54,5 +58,6 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
HttpRouter getRouter();
@Override
void close() throws IOException;
}

View file

@ -14,8 +14,10 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xbib.net.Attributes;
import org.xbib.net.http.HttpAddress;
import org.xbib.net.http.cookie.SameSite;
import org.xbib.net.http.server.auth.BaseAttributes;
import org.xbib.net.http.server.route.BaseHttpRouterContext;
import org.xbib.net.http.server.HttpHandler;
import org.xbib.net.http.server.HttpRequestBuilder;
@ -48,12 +50,15 @@ public class BaseApplication implements Application {
private final HttpResponseRenderer httpResponseRenderer;
private final Attributes attributes;
protected List<ApplicationModule> applicationModuleList;
protected BaseApplication(BaseApplicationBuilder builder) {
this.builder = builder;
this.sessionName = builder.settings.get("session.name", "SESS");
this.httpResponseRenderer = newResponseRenderer();
this.attributes = newAttributes();
this.applicationModuleList = new ArrayList<>();
for (Map.Entry<String, Settings> entry : builder.settings.getGroups("module").entrySet()) {
String moduleName = entry.getKey();
@ -129,6 +134,11 @@ public class BaseApplication implements Application {
return applicationModuleList;
}
@Override
public Attributes getAttributes() {
return attributes;
}
@Override
public Collection<HttpDomain> getDomains() {
return builder.httpRouter.getDomains();
@ -182,6 +192,10 @@ public class BaseApplication implements Application {
return new HttpResponseRenderer();
}
protected Attributes newAttributes() {
return new BaseAttributes();
}
protected Codec<Session> newSessionCodec(HttpRouterContext httpRouterContext) {
return new MemoryPropertiesSessionCodec(sessionName,this, 1024, Duration.ofDays(1));
}

View file

@ -6,6 +6,7 @@ import java.io.InputStream;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
@ -234,14 +235,8 @@ public class BaseHttpRouterContext implements HttpRouterContext {
}
@Override
public BaseHttpRouterContext body(String string) throws IOException {
httpResponseBuilder.write(string);
return this;
}
@Override
public BaseHttpRouterContext body(CharBuffer charBuffer, Charset charset) throws IOException {
httpResponseBuilder.write(charBuffer, charset);
public BaseHttpRouterContext body(byte[] bytes) throws IOException {
httpResponseBuilder.write(bytes);
return this;
}
@ -251,6 +246,18 @@ public class BaseHttpRouterContext implements HttpRouterContext {
return this;
}
@Override
public BaseHttpRouterContext body(String string) throws IOException {
httpResponseBuilder.write(CharBuffer.wrap(string), StandardCharsets.UTF_8);
return this;
}
@Override
public BaseHttpRouterContext body(CharBuffer charBuffer, Charset charset) throws IOException {
httpResponseBuilder.write(charBuffer, charset);
return this;
}
@Override
public BaseHttpRouterContext body(InputStream inputStream, int bufferSize) throws IOException {
httpResponseBuilder.write(inputStream, bufferSize);

View file

@ -81,12 +81,14 @@ public interface HttpRouterContext {
HttpRouterContext cookie(Cookie cookie);
HttpRouterContext body(byte[] bytes) throws IOException;
HttpRouterContext body(DataBuffer dataBuffer) throws IOException;
HttpRouterContext body(String string) throws IOException;
HttpRouterContext body(CharBuffer charBuffer, Charset charset) throws IOException;
HttpRouterContext body(DataBuffer dataBuffer) throws IOException;
HttpRouterContext body(InputStream inputStream, int bufferSize) throws IOException;
HttpRouterContext body(FileChannel fileChannel, int bufferSize) throws IOException;

View file

@ -157,7 +157,7 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
if (url.getFragment() != null) {
sb.append('#').append(url.getFragment());
}
if (sb.length() == 0) {
if (sb.isEmpty()) {
sb.append('/');
}
return sb.toString();
@ -172,12 +172,6 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
return uriTemplate.toURL(builder.build()).toString();
}
public void write(String string) {
if (string != null) {
responseBuilder.write(string);
}
}
public void writeBytes(byte[] bytes) {
responseBuilder.write(DefaultDataBufferFactory.getInstance().wrap(bytes));
}