remove write(String) from HttpResponseBuilder, we don't like sloppyness, try enforcing charset use
This commit is contained in:
parent
0d71313cf4
commit
9287e6c36a
11 changed files with 79 additions and 61 deletions
|
@ -1,5 +1,5 @@
|
||||||
group = org.xbib
|
group = org.xbib
|
||||||
name = net-http
|
name = net-http
|
||||||
version = 3.5.0
|
version = 3.6.0
|
||||||
|
|
||||||
org.gradle.warning.mode = ALL
|
org.gradle.warning.mode = ALL
|
||||||
|
|
|
@ -30,7 +30,6 @@ import java.io.UncheckedIOException;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
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;
|
||||||
|
@ -127,8 +126,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
@Override
|
@Override
|
||||||
public HttpResponse build() {
|
public HttpResponse build() {
|
||||||
Objects.requireNonNull(ctx);
|
Objects.requireNonNull(ctx);
|
||||||
if (body != null) {
|
if (bytes != null) {
|
||||||
internalStringWrite(body);
|
internalByteWrite(bytes);
|
||||||
} else if (charBuffer != null && charset != null) {
|
} else if (charBuffer != null && charset != null) {
|
||||||
internalBufferWrite(charBuffer, charset);
|
internalBufferWrite(charBuffer, charset);
|
||||||
} else if (dataBuffer != null) {
|
} else if (dataBuffer != null) {
|
||||||
|
@ -155,8 +154,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
super.release();
|
super.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void internalStringWrite(String body) {
|
private void internalByteWrite(byte[] bytes) {
|
||||||
internalBufferWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(body)));
|
internalBufferWrite(dataBufferFactory.wrap(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void internalBufferWrite(DataBuffer dataBuffer) {
|
private void internalBufferWrite(DataBuffer dataBuffer) {
|
||||||
|
|
|
@ -38,8 +38,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
public HttpResponse build() {
|
public HttpResponse build() {
|
||||||
Objects.requireNonNull(outputStream);
|
Objects.requireNonNull(outputStream);
|
||||||
try {
|
try {
|
||||||
if (body != null) {
|
if (bytes != null) {
|
||||||
internalWrite(body);
|
internalWrite(bytes);
|
||||||
} else if (charBuffer != null && charset != null) {
|
} else if (charBuffer != null && charset != null) {
|
||||||
internalWrite(charBuffer, charset);
|
internalWrite(charBuffer, charset);
|
||||||
} else if (dataBuffer != null) {
|
} else if (dataBuffer != null) {
|
||||||
|
@ -73,20 +73,11 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void internalWrite(String string) throws IOException {
|
void internalWrite(byte[] bytes) throws IOException {
|
||||||
if (string == null) {
|
if (bytes == null) {
|
||||||
internalFlush();
|
internalFlush();
|
||||||
} else {
|
} else {
|
||||||
write(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(string)));
|
write(dataBufferFactory.wrap(bytes));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void internalWrite(CharBuffer charBuffer, Charset charset) throws IOException {
|
|
||||||
if (charBuffer == null) {
|
|
||||||
internalFlush();
|
|
||||||
} else {
|
|
||||||
Objects.requireNonNull(charset);
|
|
||||||
write(dataBufferFactory.wrap(charset.encode(charBuffer)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
void internalWrite(InputStream inputStream, int bufferSize) throws IOException {
|
||||||
byte[] b = new byte[bufferSize];
|
byte[] b = new byte[bufferSize];
|
||||||
while (inputStream.available() > 0) {
|
while (inputStream.available() > 0) {
|
||||||
|
|
|
@ -38,8 +38,8 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
public HttpResponse build() {
|
public HttpResponse build() {
|
||||||
Objects.requireNonNull(outputStream);
|
Objects.requireNonNull(outputStream);
|
||||||
try {
|
try {
|
||||||
if (body != null) {
|
if (bytes != null) {
|
||||||
internalWrite(body);
|
internalWrite(bytes);
|
||||||
} else if (charBuffer != null && charset != null) {
|
} else if (charBuffer != null && charset != null) {
|
||||||
internalWrite(charBuffer, charset);
|
internalWrite(charBuffer, charset);
|
||||||
} else if (dataBuffer != null) {
|
} else if (dataBuffer != null) {
|
||||||
|
@ -72,21 +72,11 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void internalWrite(String string) throws IOException {
|
void internalWrite(byte[] bytes) throws IOException {
|
||||||
if (string == null) {
|
if (bytes == null) {
|
||||||
internalFlush();
|
internalFlush();
|
||||||
} else {
|
} else {
|
||||||
logger.log(Level.INFO, "internal write: " + string);
|
internalWrite(dataBufferFactory.wrap(bytes));
|
||||||
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)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
void internalWrite(InputStream inputStream, int bufferSize) throws IOException {
|
||||||
byte[] b = new byte[bufferSize];
|
byte[] b = new byte[bufferSize];
|
||||||
while (inputStream.available() > 0) {
|
while (inputStream.available() > 0) {
|
||||||
|
|
|
@ -71,7 +71,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
|
||||||
|
|
||||||
protected Charset charset;
|
protected Charset charset;
|
||||||
|
|
||||||
protected String body;
|
protected byte[] bytes;
|
||||||
|
|
||||||
protected CharBuffer charBuffer;
|
protected CharBuffer charBuffer;
|
||||||
|
|
||||||
|
@ -260,11 +260,9 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseHttpResponseBuilder write(String body) {
|
public BaseHttpResponseBuilder write(byte[] bytes) {
|
||||||
if (body != null && this.body == null) {
|
if (bytes != null) {
|
||||||
this.body = body;
|
this.bytes = bytes;
|
||||||
} else {
|
|
||||||
logger.log(Level.WARNING, "cannot write more than one body");
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,12 +52,12 @@ public interface HttpResponseBuilder {
|
||||||
|
|
||||||
HttpResponseBuilder setResponseId(Long responseId);
|
HttpResponseBuilder setResponseId(Long responseId);
|
||||||
|
|
||||||
HttpResponseBuilder write(String body);
|
HttpResponseBuilder write(byte[] bytes);
|
||||||
|
|
||||||
HttpResponseBuilder write(CharBuffer charBuffer, Charset charset);
|
|
||||||
|
|
||||||
HttpResponseBuilder write(DataBuffer dataBuffer);
|
HttpResponseBuilder write(DataBuffer dataBuffer);
|
||||||
|
|
||||||
|
HttpResponseBuilder write(CharBuffer charBuffer, Charset charset);
|
||||||
|
|
||||||
HttpResponseBuilder write(InputStream inputStream, int bufferSize);
|
HttpResponseBuilder write(InputStream inputStream, int bufferSize);
|
||||||
|
|
||||||
HttpResponseBuilder write(FileChannel fileChannel, int bufferSize);
|
HttpResponseBuilder write(FileChannel fileChannel, int bufferSize);
|
||||||
|
|
|
@ -7,6 +7,8 @@ import java.time.ZoneId;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.xbib.net.Attributes;
|
||||||
import org.xbib.net.http.HttpAddress;
|
import org.xbib.net.http.HttpAddress;
|
||||||
import org.xbib.net.http.server.HttpRequestBuilder;
|
import org.xbib.net.http.server.HttpRequestBuilder;
|
||||||
import org.xbib.net.http.server.HttpResponseBuilder;
|
import org.xbib.net.http.server.HttpResponseBuilder;
|
||||||
|
@ -40,6 +42,8 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
|
||||||
|
|
||||||
Collection<ApplicationModule> getModules();
|
Collection<ApplicationModule> getModules();
|
||||||
|
|
||||||
|
Attributes getAttributes();
|
||||||
|
|
||||||
HttpRouterContext createContext(HttpDomain domain,
|
HttpRouterContext createContext(HttpDomain domain,
|
||||||
HttpRequestBuilder httpRequestBuilder,
|
HttpRequestBuilder httpRequestBuilder,
|
||||||
HttpResponseBuilder httpResponseBuilder);
|
HttpResponseBuilder httpResponseBuilder);
|
||||||
|
@ -54,5 +58,6 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
|
||||||
|
|
||||||
HttpRouter getRouter();
|
HttpRouter getRouter();
|
||||||
|
|
||||||
|
@Override
|
||||||
void close() throws IOException;
|
void close() throws IOException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,10 @@ import java.util.Set;
|
||||||
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.http.HttpAddress;
|
import org.xbib.net.http.HttpAddress;
|
||||||
import org.xbib.net.http.cookie.SameSite;
|
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.route.BaseHttpRouterContext;
|
||||||
import org.xbib.net.http.server.HttpHandler;
|
import org.xbib.net.http.server.HttpHandler;
|
||||||
import org.xbib.net.http.server.HttpRequestBuilder;
|
import org.xbib.net.http.server.HttpRequestBuilder;
|
||||||
|
@ -48,12 +50,15 @@ public class BaseApplication implements Application {
|
||||||
|
|
||||||
private final HttpResponseRenderer httpResponseRenderer;
|
private final HttpResponseRenderer httpResponseRenderer;
|
||||||
|
|
||||||
|
private final Attributes attributes;
|
||||||
|
|
||||||
protected List<ApplicationModule> applicationModuleList;
|
protected List<ApplicationModule> applicationModuleList;
|
||||||
|
|
||||||
protected BaseApplication(BaseApplicationBuilder builder) {
|
protected BaseApplication(BaseApplicationBuilder builder) {
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
this.sessionName = builder.settings.get("session.name", "SESS");
|
this.sessionName = builder.settings.get("session.name", "SESS");
|
||||||
this.httpResponseRenderer = newResponseRenderer();
|
this.httpResponseRenderer = newResponseRenderer();
|
||||||
|
this.attributes = newAttributes();
|
||||||
this.applicationModuleList = new ArrayList<>();
|
this.applicationModuleList = new ArrayList<>();
|
||||||
for (Map.Entry<String, Settings> entry : builder.settings.getGroups("module").entrySet()) {
|
for (Map.Entry<String, Settings> entry : builder.settings.getGroups("module").entrySet()) {
|
||||||
String moduleName = entry.getKey();
|
String moduleName = entry.getKey();
|
||||||
|
@ -129,6 +134,11 @@ public class BaseApplication implements Application {
|
||||||
return applicationModuleList;
|
return applicationModuleList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Attributes getAttributes() {
|
||||||
|
return attributes;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<HttpDomain> getDomains() {
|
public Collection<HttpDomain> getDomains() {
|
||||||
return builder.httpRouter.getDomains();
|
return builder.httpRouter.getDomains();
|
||||||
|
@ -182,6 +192,10 @@ public class BaseApplication implements Application {
|
||||||
return new HttpResponseRenderer();
|
return new HttpResponseRenderer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Attributes newAttributes() {
|
||||||
|
return new BaseAttributes();
|
||||||
|
}
|
||||||
|
|
||||||
protected Codec<Session> newSessionCodec(HttpRouterContext httpRouterContext) {
|
protected Codec<Session> newSessionCodec(HttpRouterContext httpRouterContext) {
|
||||||
return new MemoryPropertiesSessionCodec(sessionName,this, 1024, Duration.ofDays(1));
|
return new MemoryPropertiesSessionCodec(sessionName,this, 1024, Duration.ofDays(1));
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import java.io.InputStream;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -234,14 +235,8 @@ public class BaseHttpRouterContext implements HttpRouterContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseHttpRouterContext body(String string) throws IOException {
|
public BaseHttpRouterContext body(byte[] bytes) throws IOException {
|
||||||
httpResponseBuilder.write(string);
|
httpResponseBuilder.write(bytes);
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BaseHttpRouterContext body(CharBuffer charBuffer, Charset charset) throws IOException {
|
|
||||||
httpResponseBuilder.write(charBuffer, charset);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,6 +246,18 @@ public class BaseHttpRouterContext implements HttpRouterContext {
|
||||||
return this;
|
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
|
@Override
|
||||||
public BaseHttpRouterContext body(InputStream inputStream, int bufferSize) throws IOException {
|
public BaseHttpRouterContext body(InputStream inputStream, int bufferSize) throws IOException {
|
||||||
httpResponseBuilder.write(inputStream, bufferSize);
|
httpResponseBuilder.write(inputStream, bufferSize);
|
||||||
|
|
|
@ -81,12 +81,14 @@ public interface HttpRouterContext {
|
||||||
|
|
||||||
HttpRouterContext cookie(Cookie cookie);
|
HttpRouterContext cookie(Cookie cookie);
|
||||||
|
|
||||||
|
HttpRouterContext body(byte[] bytes) throws IOException;
|
||||||
|
|
||||||
|
HttpRouterContext body(DataBuffer dataBuffer) throws IOException;
|
||||||
|
|
||||||
HttpRouterContext body(String string) throws IOException;
|
HttpRouterContext body(String string) throws IOException;
|
||||||
|
|
||||||
HttpRouterContext body(CharBuffer charBuffer, Charset charset) 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(InputStream inputStream, int bufferSize) throws IOException;
|
||||||
|
|
||||||
HttpRouterContext body(FileChannel fileChannel, int bufferSize) throws IOException;
|
HttpRouterContext body(FileChannel fileChannel, int bufferSize) throws IOException;
|
||||||
|
|
|
@ -157,7 +157,7 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
|
||||||
if (url.getFragment() != null) {
|
if (url.getFragment() != null) {
|
||||||
sb.append('#').append(url.getFragment());
|
sb.append('#').append(url.getFragment());
|
||||||
}
|
}
|
||||||
if (sb.length() == 0) {
|
if (sb.isEmpty()) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
@ -172,12 +172,6 @@ public abstract class DefaultMarkupTemplate extends BaseTemplate {
|
||||||
return uriTemplate.toURL(builder.build()).toString();
|
return uriTemplate.toURL(builder.build()).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String string) {
|
|
||||||
if (string != null) {
|
|
||||||
responseBuilder.write(string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeBytes(byte[] bytes) {
|
public void writeBytes(byte[] bytes) {
|
||||||
responseBuilder.write(DefaultDataBufferFactory.getInstance().wrap(bytes));
|
responseBuilder.write(DefaultDataBufferFactory.getInstance().wrap(bytes));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue