Class WrappedContext


  • public final class WrappedContext
    extends Object
    Provides a way to create sub contexts of 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.

    Since:
    7.2
    • Method Detail

      • with

        public WrappedContext with​(String key,
                                   Object value)
        Push a value in this the context.
        Parameters:
        key - The string used to get the entity.
        value - The value to push.
        Returns:
        this WrappedContext.
        Since:
        7.2
      • controlDepth

        public WrappedContext controlDepth()
                                    throws MaxDepthReachedException
        Call this method to avoid an infinite loop while calling a marshaller from another.

        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
         }
         
        Throws:
        MaxDepthReachedException
        Since:
        7.2
      • flatten

        public Map<String,​Object> flatten()
        Provides a flatten map of wrapped contexts. If a same entity type is stored in multiple contexts, the nearest one will be returned.
        Since:
        7.2
      • open

        public Closeable open()
        Open the context and make all embedded entities available. Returns a Closeable which must be closed at the end.

        Note the same context could be opened and closed several times.

        Returns:
        A Closeable instance.
        Since:
        7.2