-
Notifications
You must be signed in to change notification settings - Fork 34
Reactive View Rendering #985
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
Open
sdelamo
wants to merge
17
commits into
5.7.x
Choose a base branch
from
reactiveviewrendering
base: 5.7.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dd09492
Add ReactiveViewsRenderer API
sdelamo 4b86aa0
reactive implementation
sdelamo 33736d4
reactor catalogue
sdelamo 246916f
reactive implementation
sdelamo d68d16b
Merge branch '5.7.x' into reactiveviewrendering
sdelamo 418c179
checkstyle
sdelamo 75cc95b
add docs
sdelamo 17d978e
response type aas generic
sdelamo a4d9ed4
change javadoc
sdelamo b451205
fix checkstyle
sdelamo ad31e99
add @Experimental
sdelamo f4aea0f
remove @Type
sdelamo 94c379e
fix since
sdelamo a8674f0
add final
sdelamo 8fa6034
don’t use start import
sdelamo 1b05426
remove public modifier
sdelamo 8b5261f
Merge branch '5.7.x' into reactiveviewrendering
sdelamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
144 changes: 144 additions & 0 deletions
144
...s-core/src/main/java/io/micronaut/views/reactive/DefaultReactiveViewsRendererLocator.java
This file contains hidden or 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,144 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.reactive; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.BeanContext; | ||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.order.OrderUtil; | ||
import io.micronaut.http.annotation.Produces; | ||
import io.micronaut.inject.BeanDefinition; | ||
import io.micronaut.inject.qualifiers.Qualifiers; | ||
import io.micronaut.views.ViewsRenderer; | ||
import io.micronaut.views.exceptions.ViewNotFoundException; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Default implementation of {@link ReactiveViewsRendererLocator}. | ||
* | ||
* @author Sergio del Amo | ||
* @since 3.0.0 | ||
*/ | ||
@Singleton | ||
@Internal | ||
final class DefaultReactiveViewsRendererLocator implements ReactiveViewsRendererLocator { | ||
|
||
private final Map<ViewsRendererKey, ReactiveViewsRenderer> viewsRendererMap = new ConcurrentHashMap<>(); | ||
|
||
private final BeanContext beanContext; | ||
|
||
DefaultReactiveViewsRendererLocator(ApplicationContext beanContext) { | ||
this.beanContext = beanContext; | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public Optional<ReactiveViewsRenderer> resolveViewsRenderer(@NonNull String view, | ||
@NonNull String contentType, | ||
@Nullable Object body) throws ViewNotFoundException { | ||
Class<?> bodyClass = body != null ? body.getClass() : null; | ||
ViewsRendererKey key = new ViewsRendererKey(view, contentType, bodyClass); | ||
return Optional.ofNullable(viewsRendererMap.computeIfAbsent(key, (viewsRendererKey -> { | ||
List<ReactiveViewsRenderer> viewsRenderers = resolveViewsRenderer(bodyClass, contentType); | ||
if (viewsRenderers.isEmpty()) { | ||
return null; | ||
} | ||
Optional<ReactiveViewsRenderer> result = viewsRenderers.stream() | ||
.filter(viewsRenderer -> viewsRenderer.exists(view)) | ||
.findFirst(); | ||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
throw new ViewNotFoundException("View [" + view + "] does not exist"); | ||
}))); | ||
} | ||
|
||
/** | ||
* | ||
* @param bodyClass Response Body Class | ||
* @param contentType Response Content Type | ||
* @return List of {@link ViewsRenderer} which includes those which do not specify an {@link @Produces} annotation or | ||
* whose {link @Produces} annotation value matches the response content type. The list is sorted. The order is those {@link ViewsRenderer} which | ||
* type argument matches the response body class first and then ordered by {@link OrderUtil#COMPARATOR}. | ||
*/ | ||
@NonNull | ||
private List<ReactiveViewsRenderer> resolveViewsRenderer(Class<?> bodyClass, @NonNull String contentType) { | ||
return reactiveViewsRenderersByBodyClass(bodyClass) | ||
.stream() | ||
.filter(vr -> producesContentType(contentType, vr)) | ||
.sorted((o1, o2) -> { | ||
BeanDefinition o1BeanDefinition = beanDefinitionForViewRenderer(o1); | ||
BeanDefinition o2BeanDefinition = beanDefinitionForViewRenderer(o2); | ||
if (o1BeanDefinition.getTypeArguments().size() != o2BeanDefinition.getTypeArguments().size()) { | ||
return Integer.compare(o1BeanDefinition.getTypeArguments().size(), o2BeanDefinition.getTypeArguments().size()); | ||
} | ||
return OrderUtil.COMPARATOR.compare(o1, o2); | ||
}).toList(); | ||
} | ||
|
||
private Collection<ReactiveViewsRenderer> reactiveViewsRenderersByBodyClass(Class<?> bodyClass) { | ||
if (bodyClass == null) { | ||
return beanContext.getBeansOfType(ReactiveViewsRenderer.class); | ||
} | ||
Collection<ViewsRenderer> viewsRenderers = beanContext.getBeansOfType(ViewsRenderer.class, Qualifiers.byTypeArguments(bodyClass, Object.class)); | ||
return beanContext.getBeansOfType(ReactiveViewsRenderer.class, Qualifiers.byTypeArguments(bodyClass, Object.class, Object.class)) | ||
.stream() | ||
.filter(reactiveViewsRenderer -> { | ||
if (reactiveViewsRenderer instanceof ReactiveViewsRendererAdapter<?, ?> adapter) { | ||
return viewsRenderers.stream() | ||
.map(vr -> vr.getClass()) | ||
.anyMatch(clazz -> clazz == adapter.getDelegateClass()); | ||
} | ||
return true; | ||
|
||
}) | ||
.toList(); | ||
} | ||
|
||
private BeanDefinition beanDefinitionForViewRenderer(ReactiveViewsRenderer reactiveViewsRenderer) { | ||
if (reactiveViewsRenderer instanceof ReactiveViewsRendererAdapter<?, ?> adapter) { | ||
return beanContext.getBeanDefinition(adapter.getDelegateClass()); | ||
} | ||
return beanContext.getBeanDefinition(reactiveViewsRenderer.getClass()); | ||
} | ||
|
||
private boolean producesContentType(@NonNull String contentType, ReactiveViewsRenderer reactiveViewsRenderer) { | ||
BeanDefinition beanDefinition = beanDefinitionForViewRenderer(reactiveViewsRenderer); | ||
return producesContentType(contentType, beanDefinition); | ||
} | ||
|
||
private boolean producesContentType(@NonNull String contentType, BeanDefinition beanDefinition) { | ||
AnnotationValue<Produces> annotation = beanDefinition.getAnnotation(Produces.class); | ||
if (annotation == null) { | ||
return true; | ||
} | ||
return Arrays.asList(annotation.stringValues()).contains(contentType); | ||
} | ||
|
||
record ViewsRendererKey(String viewName, String contentType, Class<?> bodyClass) { | ||
|
||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
views-core/src/main/java/io/micronaut/views/reactive/ReactiveViewsRenderer.java
This file contains hidden or 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,53 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.reactive; | ||
|
||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.async.annotation.SingleResult; | ||
import io.micronaut.core.order.Ordered; | ||
import org.reactivestreams.Publisher; | ||
|
||
/** | ||
* Interface to be implemented by View Engines implementations. | ||
* @param <T> The model type | ||
* @param <R> The request type | ||
* @param <O> The response type | ||
* @author Sergio del Amo | ||
* @since 5.8.0 | ||
*/ | ||
@Experimental | ||
public interface ReactiveViewsRenderer<T, R, O> extends Ordered { | ||
sdelamo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @param viewName view name to be rendered | ||
* @param data response body to render it with a view | ||
* @param request HTTP request | ||
* @return rendered view | ||
*/ | ||
@NonNull | ||
@SingleResult | ||
Publisher<O> render(@NonNull String viewName, | ||
@Nullable T data, | ||
@Nullable R request); | ||
|
||
/** | ||
* @param viewName view name to be rendered | ||
* @return true if a template can be found for the supplied view name. | ||
*/ | ||
boolean exists(@NonNull String viewName); | ||
} |
74 changes: 74 additions & 0 deletions
74
views-core/src/main/java/io/micronaut/views/reactive/ReactiveViewsRendererAdapter.java
This file contains hidden or 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,74 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.reactive; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.async.annotation.SingleResult; | ||
import io.micronaut.core.io.Writable; | ||
import io.micronaut.views.ViewsRenderer; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Adapts from {@link ViewsRenderer} to {@link ReactiveViewsRenderer}. | ||
* @param <T> The model type | ||
* @param <R> The request type | ||
* @author Sergio del Amo | ||
* @since 1.0 | ||
*/ | ||
@Internal | ||
final class ReactiveViewsRendererAdapter<T, R> implements ReactiveViewsRenderer<T, R, Writable> { | ||
|
||
private final ViewsRenderer<T, R> delegate; | ||
|
||
ReactiveViewsRendererAdapter(ViewsRenderer<T, R> viewsRenderer) { | ||
this.delegate = viewsRenderer; | ||
} | ||
|
||
/** | ||
* @param viewName view name to be rendered | ||
* @param data response body to render it with a view | ||
* @param request HTTP request | ||
* @return A writable where the view will be written to. | ||
*/ | ||
@Override | ||
@NonNull | ||
@SingleResult | ||
public Publisher<Writable> render(@NonNull String viewName, | ||
@Nullable T data, | ||
@Nullable R request) { | ||
return Mono.just(delegate.render(viewName, data, request)); | ||
graemerocher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @param viewName view name to be rendered | ||
* @return true if a template can be found for the supplied view name. | ||
*/ | ||
@Override | ||
public boolean exists(@NonNull String viewName) { | ||
return delegate.exists(viewName); | ||
} | ||
|
||
/** | ||
* | ||
* @return The class of the delegate | ||
*/ | ||
public Class<? extends ViewsRenderer> getDelegateClass() { | ||
return delegate.getClass(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think it would be better to remove the generics and add a
supports(Class<?> type)
method to theReactiveViewsRenderer
rather than 2 getBeansOfType calls which looks like a hackThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it is hackish. But we need to call twice because many
ReactiveViewsRenderer
implementations are backed by aViewsRenderer
, the ones usingReactiveViewsRendererAdapter
.For those implementations, we need to know if the delegated class supports the body class.
Note, that the lookup cost for the appropriate view renderer for a particular body class is paid only once as it is cached in a map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok but I am unconvinced wrapping the non-reactive in a reactive API by default is the way to go. The
Writable
part not being shifted to the blocking thread pool is a problemThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be better to leave the
ViewRenderer
stuff as is and add an optional interfaceReactiveViewRenderer
that can be implemented by the view renderers that support it and then adapt the code with differentif/else
blocks the adding reactive overhead to all view renderers and changing the behaviour of WritableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sdelamo can you look at splitting this so we are not adding reactive overhead unnecessary for non-reactive view renderers?