Skip to content
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
24 changes: 24 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/StreamingOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.avaje.http.api;

import java.io.IOException;
import java.io.OutputStream;

/**
* An avaje {@link Controller} endpoint is able to use an instance of this interface
* as a return type.
*
* <pre>{@code
* @Post("some_endpoint")
* @Produces("application/octet-stream")
* public StreamingOutput thumbnail(
* InputStream inputStream,
* @QueryParam(Constants.KEY_SIZE) @Min(1) @Max(Constants.MAX_SIZE) Integer size
* ) throws IOException {
* return (os) -> os.write(new byte[] { 0x01, 0x02, 0x03 });
* }
* }</pre>
*/

public interface StreamingOutput {
void write(OutputStream outputStream) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ enum ResponseMode {
Jstachio,
Templating,
InputStream,
StreamingOutput,
Other
}

Expand All @@ -117,6 +118,9 @@ ResponseMode responseMode() {
if (isInputStream(method.returnType())) {
return ResponseMode.InputStream;
}
if (isStreamingOutput(method.returnType())) {
return ResponseMode.StreamingOutput;
}
if (producesJson()) {
return ResponseMode.Json;
}
Expand All @@ -136,6 +140,10 @@ private boolean isInputStream(TypeMirror type) {
return isAssignable2Interface(type.toString(), "java.io.InputStream");
}

private boolean isStreamingOutput(TypeMirror type) {
return isAssignable2Interface(type.toString(), "io.avaje.http.api.StreamingOutput");
}

private boolean producesJson() {
return !"byte[]".equals(method.returnType().toString())
&& !useJstachio
Expand Down Expand Up @@ -294,11 +302,19 @@ private void writeContextReturn(ResponseMode responseMode, String resultVariable
case Json -> writeJsonReturn(produces, indent);
case Text -> writer.append("ctx.text(%s);", resultVariable);
case Templating -> writer.append("ctx.html(%s);", resultVariable);
case StreamingOutput -> writeStreamingOutputReturn(produces, resultVariable, indent);
default -> writer.append("ctx.contentType(\"%s\").write(%s);", produces, resultVariable);
}
writer.eol();
}

private void writeStreamingOutputReturn(String produces, String resultVariable, String indent) {
writer.append("ctx.contentType(\"%s\");", produces).eol();
writer.append(indent).append("try (java.io.OutputStream ctxOutputStream = ctx.outputStream()) {").eol();
writer.append(indent).append(indent).append("%s.write(ctxOutputStream);", resultVariable).eol();
writer.append(indent).append("}", resultVariable);
}

private void writeJsonReturn(String produces, String indent) {
var uType = UType.parse(method.returnType());
boolean streaming = useJsonB && streamingContent(uType);
Expand Down
145 changes: 106 additions & 39 deletions tests/test-javalin-jsonb/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,23 @@
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"$ref" : "#/components/schemas/HelloForm"
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
}
}
},
Expand Down Expand Up @@ -579,7 +595,23 @@
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"$ref" : "#/components/schemas/HelloForm"
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
}
}
},
Expand Down Expand Up @@ -1029,6 +1061,57 @@
}
}
},
"/openapi/form" : {
"post" : {
"tags" : [

],
"summary" : "",
"description" : "",
"parameters" : [
{
"name" : "Head-String",
"in" : "header",
"schema" : {
"type" : "string"
}
}
],
"requestBody" : {
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
}
}
}
}
},
"required" : true
},
"responses" : {
"201" : {
"description" : "",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/openapi/get" : {
"get" : {
"tags" : [
Expand Down Expand Up @@ -1653,11 +1736,31 @@
],
"summary" : "",
"description" : "",
"parameters" : [
{
"name" : "Head-String",
"in" : "header",
"schema" : {
"type" : "string"
}
}
],
"requestBody" : {
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"$ref" : "#/components/schemas/MyForm"
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
}
}
}
}
},
Expand Down Expand Up @@ -2241,42 +2344,6 @@
}
}
},
"HelloForm" : {
"required" : [
"name"
],
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
},
"MyForm" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
}
}
},
"Person" : {
"type" : "object",
"properties" : {
Expand Down
58 changes: 34 additions & 24 deletions tests/test-javalin/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,23 @@
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"$ref" : "#/components/schemas/HelloForm"
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
}
}
},
Expand Down Expand Up @@ -549,7 +565,23 @@
"content" : {
"application/x-www-form-urlencoded" : {
"schema" : {
"$ref" : "#/components/schemas/HelloForm"
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
}
}
},
Expand Down Expand Up @@ -874,28 +906,6 @@
"format" : "date-time"
}
}
},
"HelloForm" : {
"required" : [
"name"
],
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"nullable" : false
},
"email" : {
"type" : "string"
},
"url" : {
"type" : "string"
},
"startDate" : {
"type" : "string",
"format" : "date"
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.avaje.http.api.Produces;
import io.avaje.http.api.Put;
import io.avaje.http.api.Valid;
import io.avaje.http.api.StreamingOutput;
import io.avaje.jex.http.Context;

// @Roles(AppRoles.BASIC_USER)
Expand Down Expand Up @@ -77,4 +78,12 @@ String testBigInt(BigInteger val) {
String rawJsonString() {
return "{\"key\": 42 }";
}

@Get("streamBytes")
@Produces(value = "text/html", statusCode = 200)
StreamingOutput streamBytes() {
return outputStream -> outputStream.write(new byte[]{
0x41, 0x76, 0x61, 0x6a, 0x65
});
}
}
Loading