Interface BeanContext

  • All Superinterfaces:
    org.apache.sling.api.adapter.Adaptable
    All Known Implementing Classes:
    BeanContext.AbstractContext, BeanContext.AbstractScriptContext, BeanContext.Map, BeanContext.Page, BeanContext.Service, BeanContext.Servlet, BeanContext.Wrapper

    @Adaptable(adaptableClass=BeanContext.class,
               adapters=@Adapter(condition="If the context contains an entity of the requested type",value={org.apache.sling.api.resource.Resource.class,org.apache.sling.api.resource.ResourceResolver.class,org.apache.sling.api.SlingHttpServletRequest.class,org.apache.sling.api.SlingHttpServletResponse.class}))
    public interface BeanContext
    extends org.apache.sling.api.adapter.Adaptable
    The interface for the different scripting contexts (JSP, Groovy, ...) and the basic implementations for this interface. This serves as a container for the basic objects often used to initialize models.
    • Method Detail

      • getResource

        org.apache.sling.api.resource.Resource getResource()
        Returns the resource declared in the context.
      • getResolver

        org.apache.sling.api.resource.ResourceResolver getResolver()
        Returns the resolver declared in the context.
      • getRequest

        org.apache.sling.api.SlingHttpServletRequest getRequest()
        Returns the request declared in the context.
      • getResponse

        org.apache.sling.api.SlingHttpServletResponse getResponse()
        Returns the response declared in the context.
      • getAttribute

        <T> T getAttribute​(String name,
                           Class<T> T)
        Returns an attribute value from the context.
      • setAttribute

        void setAttribute​(String name,
                          Object value,
                          BeanContext.Scope scope)
        Stores an attribute in the context in th given scope.
      • getService

        <T> T getService​(Class<T> type)
        retrieves a service implementation using the 'sling' script helper
      • getServices

        <T> T[] getServices​(Class<T> serviceType,
                            String filter)
                     throws org.osgi.framework.InvalidSyntaxException
        retrieves a set of services appropriate to the filter
        Throws:
        org.osgi.framework.InvalidSyntaxException
      • adaptTo

        <AdapterType> AdapterType adaptTo​(@NotNull
                                          @NotNull Class<AdapterType> type)

        Adapts to the components Resource, ResourceResolver, SlingHttpServletRequest, SlingHttpServletResponse, Locale, BeanContext itself, a ValueMap for the request, SlingBeans or possibly more if defined in Sling.

        In case of SlingBean we try the basic sling mechanism (possibly calling Sling-Models) and, failing that, try to instantiate and SlingBean.initialize(BeanContext) it ourselves, if it is instantiable and the Sling-Models @Model annotation is not present.

        Cached - multiple calls will always return the same object, except for ValueMap and SlingBeans.

        Specified by:
        adaptTo in interface org.apache.sling.api.adapter.Adaptable
        Parameters:
        type - not null, the type to be adapted to
        Returns:
        the component of type or whatever Sling has adapters for, or null if there is nothing.
        See Also:
        SlingAdaptable.adaptTo(Class)
      • withResource

        BeanContext withResource​(org.apache.sling.api.resource.Resource resource)
        Returns a clone of this context with the resource overridden, or this if it already had this resource. All other internal structures of this will be referenced by the copy, too.
        Parameters:
        resource - the resource
        Returns:
        a context with the getResource() resource, everything else (except possibly resolver) unchanged. Might be this.
      • withLocale

        BeanContext withLocale​(Locale locale)
        Returns a clone of this context with the locale overridden, or this if it already had this locale. All other internal structures of this will be referenced by the copy, too.
        Parameters:
        locale - the locale; if this is null getLocale() will take this from the attributes.
        Returns:
        a context with this locale, otherwise sharing everything else. Might be this.