-
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.
Provider tighter integration of Qute into RESTEasy Reactive
This change uses the ServerRestHandler mechanism instead of a response filter to convert a template into a response. This also paves the way to implement returning template responses as chunks (using Qute's createMulti)
- Loading branch information
Showing
9 changed files
with
221 additions
and
50 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
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
41 changes: 41 additions & 0 deletions
41
...e/src/main/java/io/quarkus/resteasy/reactive/qute/runtime/TemplateResponseUniHandler.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,41 @@ | ||
package io.quarkus.resteasy.reactive.qute.runtime; | ||
|
||
import static io.quarkus.resteasy.reactive.qute.runtime.Util.*; | ||
import static io.quarkus.resteasy.reactive.qute.runtime.Util.toUni; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.qute.Engine; | ||
import io.quarkus.qute.TemplateInstance; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class TemplateResponseUniHandler implements ServerRestHandler { | ||
|
||
private volatile Engine engine; | ||
|
||
@Override | ||
public void handle(ResteasyReactiveRequestContext requestContext) { | ||
Object result = requestContext.getResult(); | ||
if (!(result instanceof TemplateInstance)) { | ||
return; | ||
} | ||
|
||
if (engine == null) { | ||
synchronized (this) { | ||
if (engine == null) { | ||
engine = Arc.container().instance(Engine.class).get(); | ||
} | ||
} | ||
} | ||
requestContext.setResult(createUni(requestContext, (TemplateInstance) result, engine)); | ||
} | ||
|
||
private Uni<String> createUni(ResteasyReactiveRequestContext requestContext, TemplateInstance result, Engine engine) { | ||
setSelectedVariant(result, requestContext.getRequest(), | ||
requestContext.getHttpHeaders().getAcceptableLanguages()); | ||
return toUni(result, engine); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...y-reactive-qute/runtime/src/main/java/io/quarkus/resteasy/reactive/qute/runtime/Util.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,62 @@ | ||
package io.quarkus.resteasy.reactive.qute.runtime; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Request; | ||
|
||
import io.quarkus.qute.Engine; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.qute.TemplateInstance; | ||
import io.quarkus.qute.Variant; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
final class Util { | ||
|
||
private Util() { | ||
} | ||
|
||
static Uni<String> toUni(TemplateInstance instance, Engine engine) { | ||
Uni<String> uni = instance.createUni(); | ||
if (!engine.useAsyncTimeout()) { | ||
// Make sure the timeout is always used | ||
long timeout = instance.getTimeout(); | ||
uni = uni.ifNoItem().after(Duration.ofMillis(timeout)) | ||
.failWith(() -> new TemplateException(instance + " rendering timeout [" + timeout + "ms] occured")); | ||
} | ||
return uni; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static MediaType setSelectedVariant(TemplateInstance result, | ||
Request request, List<Locale> acceptableLanguages) { | ||
Object variantsAttr = result.getAttribute(TemplateInstance.VARIANTS); | ||
if (variantsAttr != null) { | ||
List<Variant> quteVariants = (List<Variant>) variantsAttr; | ||
List<javax.ws.rs.core.Variant> jaxRsVariants = new ArrayList<>(quteVariants.size()); | ||
for (Variant variant : quteVariants) { | ||
jaxRsVariants.add(new javax.ws.rs.core.Variant(MediaType.valueOf(variant.getMediaType()), variant.getLocale(), | ||
variant.getEncoding())); | ||
} | ||
javax.ws.rs.core.Variant selected = request | ||
.selectVariant(jaxRsVariants); | ||
|
||
if (selected != null) { | ||
Locale selectedLocale = selected.getLanguage(); | ||
if (selectedLocale == null) { | ||
if (!acceptableLanguages.isEmpty()) { | ||
selectedLocale = acceptableLanguages.get(0); | ||
} | ||
} | ||
result.setAttribute(TemplateInstance.SELECTED_VARIANT, | ||
new Variant(selectedLocale, selected.getMediaType().toString(), | ||
selected.getEncoding())); | ||
return selected.getMediaType(); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
Oops, something went wrong.