Interface ModelFactory


  • public interface ModelFactory
    The ModelFactory instantiates Sling Model classes similar to #adaptTo but will throw an exception in case instantiation fails for some reason.
    • Method Detail

      • canCreateFromAdaptable

        boolean canCreateFromAdaptable​(@NotNull
                                       @NotNull Object adaptable,
                                       @NotNull
                                       @NotNull Class<?> type)
        Parameters:
        adaptable - the adaptable to check
        type - the class to check
        Returns:
        true in case the given class can be created from the given adaptable, otherwise false
      • isModelClass

        @Deprecated
        boolean isModelClass​(@NotNull
                             @NotNull Object adaptable,
                             @NotNull
                             @NotNull Class<?> type)
        Deprecated.
        Use isModelClass(Class) instead!
        Parameters:
        adaptable - the adaptable to check
        type - the class to check
        Returns:
        false in case no class with the Model annotation adapts to the requested type
        See Also:
        Model
      • isModelClass

        boolean isModelClass​(@NotNull
                             @NotNull Class<?> type)
        Checks if a given type can be instantiated though Sling Models. This checks that
        • there is a class annotated with Model which adapts to the given type
        • this class is registered as Sling Model (i.e. the package is listed in the "Sling-Model-Packages" header from the bundles manifest and has been picked up already by the bundle listener)
        Only if both conditions are fulfilled this method will return true.
        Parameters:
        type - the class to check
        Returns:
        true in case the given type can be instantiated though Sling Models.
      • isModelAvailableForResource

        boolean isModelAvailableForResource​(@NotNull
                                            @NotNull Resource resource)
        Determine is a model class is available for the resource's resource type.
        Parameters:
        resource - a resource
        Returns:
        true if a model class is mapped to the resource type
      • isModelAvailableForRequest

        boolean isModelAvailableForRequest​(@NotNull
                                           @NotNull SlingHttpServletRequest request)
        Determine is a model class is available for the request's resource's resource type.
        Parameters:
        request - a request
        Returns:
        true if a model class is mapped to the resource type
      • exportModel

        @NotNull
        <T> T exportModel​(@NotNull
                          @NotNull Object model,
                          @NotNull
                          @NotNull String exporterName,
                          @NotNull
                          @NotNull Class<T> targetClass,
                          @NotNull
                          @NotNull Map<String,​String> options)
                   throws ExportException,
                          MissingExporterException
        Export the model object using the defined target class using the named exporter.
        Type Parameters:
        T - the target class
        Parameters:
        model - the model object
        exporterName - the exporter name
        targetClass - the target class
        options - any exporter options
        Returns:
        an instance of the target class
        Throws:
        ExportException - if the export fails
        MissingExporterException - if the named exporter can't be found
      • getModelFromWrappedRequest

        @Nullable
        <T> T getModelFromWrappedRequest​(@NotNull
                                         @NotNull SlingHttpServletRequest request,
                                         @NotNull
                                         @NotNull Resource resource,
                                         @NotNull
                                         @NotNull Class<T> targetClass)
        Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context.
        Type Parameters:
        T - the target adapter class
        Parameters:
        request - the current request
        resource - the resource to set as the wrapped request's resource
        targetClass - the target adapter class
        Returns:
        an instance of the target class or null if the adaptation could not be done