-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support the Mutiny AsyncFile type as a return type
- Loading branch information
Showing
4 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...a/io/quarkus/resteasy/reactive/server/runtime/ServerMutinyAsyncFileMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.quarkus.resteasy.reactive.server.runtime; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; | ||
import org.jboss.resteasy.reactive.server.spi.ServerHttpResponse; | ||
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; | ||
|
||
import io.vertx.mutiny.core.file.AsyncFile; | ||
|
||
@Provider | ||
public class ServerMutinyAsyncFileMessageBodyWriter implements ServerMessageBodyWriter<AsyncFile> { | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) { | ||
return isWritable(type); | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return isWritable(type); | ||
} | ||
|
||
private boolean isWritable(Class<?> type) { | ||
// allow for subtypes, such as AsyncFileImpl | ||
return AsyncFile.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public void writeResponse(AsyncFile file, Type genericType, ServerRequestContext context) throws WebApplicationException { | ||
ResteasyReactiveRequestContext ctx = ((ResteasyReactiveRequestContext) context); | ||
ctx.suspend(); | ||
ServerHttpResponse response = context.serverResponse(); | ||
// this is only set by nice people, unfortunately | ||
if (file.getReadLength() != Long.MAX_VALUE) { | ||
response.setResponseHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.getReadLength())); | ||
} else { | ||
response.setChunked(true); | ||
} | ||
file.handler(buffer -> { | ||
try { | ||
response.write(buffer.getBytes()); | ||
} catch (Exception x) { | ||
// believe it or not, this throws | ||
ctx.resume(x); | ||
return; | ||
} | ||
if (response.isWriteQueueFull()) { | ||
file.pause(); | ||
response.addDrainHandler(file::resume); | ||
} | ||
}); | ||
|
||
file.endHandler(new Runnable() { | ||
@Override | ||
public void run() { | ||
file.close(); | ||
response.end(); | ||
// Not sure if I need to resume, actually | ||
ctx.resume(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void writeTo(AsyncFile asyncFile, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, | ||
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | ||
throw new UnsupportedOperationException("Returning an AsyncFile is not supported with WriterInterceptors"); | ||
} | ||
} |