public final class WrappedContext extends Object
RenderingContext
to broadcast marshalled entities or state
parameters from a marshaller to other marshaller.
First, create the context, fill it and then use a try-resource statement to ensure the context will be closed.
DocumentModel doc = ...;
RenderingContext ctx = ...;
try (Closeable ctx = ctx.wrap().with(ENTITY_DOCUMENT, doc).open()) {
// the document will only be available in the following statements
// call other marshallers here and put this code the get the doc : DocumentModel contextualDocument = ctx.getParameter(ENTITY_DOCUMENT);
}
Note that if in the try-resource statement, another context is created, the entity will be searched first in this context, if not found, recursively in the parent context: the nearest will be returned.
Modifier and Type | Method and Description |
---|---|
WrappedContext |
controlDepth()
Call this method to avoid an infinite loop while calling a marshaller from another.
|
Map<String,Object> |
flatten()
Provides a flatten map of wrapped contexts.
|
Closeable |
open()
Open the context and make all embedded entities available.
|
String |
toString()
Prints this context.
|
WrappedContext |
with(String key,
Object value)
Push a value in this the context.
|
public final WrappedContext with(String key, Object value)
key
- The string used to get the entity.value
- The value to push.WrappedContext
.public final WrappedContext controlDepth() throws MaxDepthReachedException
This method increases the current number of "marshaller-to-marshaller" calls. And then checks that this number do
not exceed the "depth" parameter. If the "depth" parameter is not provided or if it's not valid, the default
value is "root" (expected valid values are "root", "children" or "max" - see DepthValues
).
Here is the prettiest way to write it:
// This will control infinite loop in this marshaller try (Closeable resource = ctx.wrap().controlDepth().open()) { // call another marshaller to fetch the desired property here } catch (MaxDepthReachedException e) { // do not call the other marshaller }
You can also control the depth before (usefull for list):
try { WrappedContext wrappedCtx = ctx.wrap().controlDepth(); // prepare your calls ... // This will control infinite loop in this marshaller try (Closeable resource = wrappedCtx.open()) { // call another marshaller to fetch the desired property here } } catch (MaxDepthReachedException e) { // manage the case }
MaxDepthReachedException
public final Map<String,Object> flatten()
public final Closeable open()
Closeable
which must be closed at
the end.
Note the same context could be opened and closed several times.
Closeable
instance.Copyright © 2015 Nuxeo SA. All rights reserved.