Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support InputStream/byte[] response type #63

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;

import java.io.InputStream;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -278,6 +279,16 @@ default String userAgent() {
*/
Context write(String content);

/**
* Write raw bytes to the response.
*/
Context write(byte[] bytes);

/**
* Write raw inputStream to the response.
*/
Context write(InputStream is);

/**
* Render a template typically as html.
*
Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ThreadFactory threadFactory() {

if (factory == null) {
factory =
Thread.ofVirtual().name("jex-http-", 0).factory();
Thread.ofVirtual().name("avaje-jex-http-", 0).factory();
}

return factory;
Expand Down
7 changes: 3 additions & 4 deletions avaje-jex/src/main/java/io/avaje/jex/TemplateRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ public non-sealed interface TemplateRender extends JexExtension {

/**
* Return the extensions this template renders for by default.
* <p>
* When the template render is not explicitly registered it can be
* automatically registered via ServiceLoader and these are the extensions
* it will register for by default.
*
* <p>When the template render is not explicitly registered, it can be automatically registered
* via ServiceLoader with the provided extensions by default.
*/
String[] defaultExtensions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void close() throws IOException {
stream.flush();
stream.close();
} else {
context.writeBytes(buffer.toByteArray());
context.write(buffer.toByteArray());
}
}
}
34 changes: 25 additions & 9 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,36 @@ public Context html(String content) {

@Override
public Context write(String content) {
try {
writeBytes(content.getBytes(StandardCharsets.UTF_8));
return this;

write(content.getBytes(StandardCharsets.UTF_8));
return this;
}

@Override
public Context write(byte[] bytes) {

try (var os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(statusCode(), bytes.length);

os.write(bytes);
os.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}

void writeBytes(byte[] bytes) throws IOException {
exchange.sendResponseHeaders(statusCode(), bytes.length);
final OutputStream os = exchange.getResponseBody();
os.write(bytes);
os.flush();
os.close();
@Override
public Context write(InputStream is) {

try (is; var os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(statusCode(), 0);
is.transferTo(os);
os.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}

int statusCode() {
Expand Down
11 changes: 6 additions & 5 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana
final ServiceManager manager = new ServiceManager(serviceManager, "http", "");

try {
int port = jex.config().port();
final HttpServer server;

var port = new InetSocketAddress(jex.config().port());
final var sslContext = jex.config().sslContext();
if (sslContext != null) {

var httpsServer = HttpsServer.create(new InetSocketAddress(port), 0);
if (sslContext != null) {
var httpsServer = HttpsServer.create(port, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
server = httpsServer;
} else {
server = HttpServer.create(new InetSocketAddress(port), 0);
server = HttpServer.create(port, 0);
}

var handler = new BaseHandler(routes);
var context = server.createContext("/", handler);
context.getFilters().add(new BaseFilter(routes, manager));
context.getFilters().add(new RoutingFilter(routes, manager));
context.getFilters().addAll(routes.filters());
server.setExecutor(Executors.newThreadPerTaskExecutor(jex.config().threadFactory()));
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import io.avaje.jex.routes.SpiRoutes;
import io.avaje.jex.spi.SpiContext;

class BaseFilter extends Filter {
final class RoutingFilter extends Filter {

private final SpiRoutes routes;
private final ServiceManager mgr;

BaseFilter(SpiRoutes routes, ServiceManager mgr) {
RoutingFilter(SpiRoutes routes, ServiceManager mgr) {
this.mgr = mgr;
this.routes = routes;
}
Expand Down