Class Status


  • public class Status
    extends Object
    The standardised answer object of a servlet request to fill the response output. It allows adding generic objects and lists with the data(String) / list(String) methods. Can be serialized and deserialized with Gson, so it is possible to extend this and add your own attributes, too. (Caution: for deserializing it with Gson you might run into trouble if you put arbitrary objects into the data / list - it's probably easier to extend Status and have attributes with the specific types needed.)
    • Field Detail

      • gson

        protected final transient com.google.gson.Gson gson
      • request

        protected final transient org.apache.sling.api.SlingHttpServletRequest request
      • response

        protected final transient org.apache.sling.api.SlingHttpServletResponse response
      • status

        protected int status
      • success

        protected boolean success
      • warning

        protected boolean warning
      • messageLogger

        @Nullable
        protected transient @Nullable org.slf4j.Logger messageLogger
        If set, added messages will be logged here.
      • title

        @Nullable
        protected @Nullable String title
    • Constructor Detail

      • Status

        public Status​(@Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletRequest request,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletResponse response,
                      @Nullable
                      @Nullable org.slf4j.Logger messageLogger)
      • Status

        public Status​(@Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletRequest request,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletResponse response)
        Constructor without logging messages - consider Status(SlingHttpServletRequest, SlingHttpServletResponse, Logger) to automatically log added messages.
      • Status

        public Status​(@NotNull
                      @NotNull com.google.gson.GsonBuilder gsonBuilder,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletRequest request,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletResponse response,
                      @Nullable
                      @Nullable org.slf4j.Logger messageLogger)
        Construction
      • Status

        @Deprecated
        public Status​(@NotNull
                      @NotNull com.google.gson.Gson gson,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletRequest request,
                      @Nullable
                      @Nullable org.apache.sling.api.SlingHttpServletResponse response)
        Deprecated.
        since it's dangerous to forget #initGson(GsonBuilder, SlingHttpServletRequest).
        Construct with a specific Gson instance. CAUTION: you need to have called #initGson(GsonBuilder, SlingHttpServletRequest) for proper message translation. Perhaps rather use #Status(GsonBuilder, SlingHttpServletRequest, SlingHttpServletResponse).
      • Status

        @Deprecated
        public Status()
        Deprecated.
        Constructor for deserialization with gson only
    • Method Detail

      • initGson

        @NotNull
        public static @NotNull com.google.gson.GsonBuilder initGson​(@NotNull
                                                                    @NotNull com.google.gson.GsonBuilder gsonBuilder,
                                                                    @NotNull
                                                                    @NotNull Supplier<org.apache.sling.api.SlingHttpServletRequest> requestProvider)
        Registers a type adapter for the JSON serialization of Message and MessageContainer that performs i18n according to the given request. If there is no request, we can skip this step as these classes are annotated with JsonAdapter.
      • getStatus

        public int getStatus()
      • setStatus

        public void setStatus​(int status)
      • getRequiredParameter

        @Nullable
        public @Nullable String getRequiredParameter​(@NotNull
                                                     @NotNull String paramName,
                                                     @Nullable
                                                     @Nullable Pattern pattern,
                                                     @NotNull
                                                     @NotNull String errorMessage)
        Retrieves and validates a required parameter from the request, adding the errorMessage if that fails.
        Returns:
        the validated parameter value, or null if that fails
      • getRequiredParameters

        public List<String> getRequiredParameters​(@NotNull
                                                  @NotNull String paramName,
                                                  @Nullable
                                                  @Nullable Pattern pattern,
                                                  @NotNull
                                                  @NotNull String errorMessage)
      • getTitle

        public String getTitle()
      • hasTitle

        public boolean hasTitle()
      • setTitle

        public void setTitle​(String rawTitle)
      • isValid

        public boolean isValid()
      • isSuccess

        public boolean isSuccess()
      • isWarning

        public boolean isWarning()
      • setWarning

        public void setWarning​(boolean value)
      • isError

        public boolean isError()
      • data

        @NotNull
        public @NotNull Map<String,​Object> data​(@NotNull
                                                      @NotNull String name)
        Returns a map that is saved with the given name in the data section of the Status, creating it if necessary.
        Parameters:
        name - the key of the data element to insert in the status response
        Returns:
        a Map for the data values of the added status object, created if neccesary.
      • reference

        public void reference​(@NotNull
                              @NotNull String name,
                              org.apache.sling.api.resource.Resource resource)
        Adds information about resource as data(String) object.
        Parameters:
        name - the key of the data element to insert in the status response
        See Also:
        reference(Map, Resource)
      • reference

        public void reference​(Map<String,​Object> object,
                              org.apache.sling.api.resource.Resource resource)
        Adds general information about the resource into the object: name, path, type (Sling resourcetype), prim (primary type), synthetic (whether it is a synthetic resource).
      • list

        @NotNull
        public @NotNull List<Map<String,​Object>> list​(@NotNull
                                                            @NotNull String name)
        Returns the list for the given name, creating it if neccesary.
        Parameters:
        name - the key of the data element to insert in the status response
        Returns:
        a new list of Map items for the data values of the added status object
      • list

        public void list​(@NotNull
                         @NotNull String name,
                         Collection<org.apache.sling.api.resource.Resource> items)
        Adds a list of resources in a request with common information. If there was already a list with that name, the items are added to it.
        Parameters:
        name - the key of the data element to insert in the status response
      • shortMessage

        public Message shortMessage​(@NotNull
                                    @NotNull Message.Level level,
                                    @NotNull
                                    @NotNull String text,
                                    Object... args)
        Adds a message with minimum information.
        Parameters:
        level - the message level
        text - non prepared text message, may contain {} placeholders
        args - argument objects for the log like message preparation of text
        Returns:
        the created and added message, if you need to add more attributes
      • addValidationMessage

        @NotNull
        public @NotNull Message addValidationMessage​(@NotNull
                                                     @NotNull Message.Level level,
                                                     @Nullable
                                                     @Nullable String context,
                                                     @Nullable
                                                     @Nullable String label,
                                                     @NotNull
                                                     @NotNull String text,
                                                     Object... args)
        For use with validation messages: add context and label, too. Validation messages are logged at level debug.
        Parameters:
        level - the message level
        context - non prepared context key
        label - non prepared aspect label (validation label)
        text - non prepared text message, can contain placeholders {}
        Returns:
        the created and added message, if you need to add more attributes
        See Also:
        MessageFormat
      • addMessage

        @NotNull
        public @NotNull Message addMessage​(@NotNull
                                           @NotNull Message message)
        Adds the message and returns it.
        Returns:
        the created and added message, if you need to add more attributes
      • adjustToMessageLevel

        protected void adjustToMessageLevel​(Message.Level level)
        Adjusts title, status and success / warning according to message level.
      • toJson

        public void toJson​(@NotNull
                           @NotNull com.google.gson.stream.JsonWriter writer)
                    throws IOException
        Writes this object as JSON using Gson.
        Throws:
        IOException
      • getJsonString

        @NotNull
        public @NotNull String getJsonString()
        Returns the JSON representation of this status as String, primarily for logging. Usable for logging: never throws up even if JSON generation fails, though it's obviously lacking data then.
      • sendJson

        public void sendJson()
                      throws IOException
        Serializes the status message and writes it in the response, using getStatus() as HTTP status.
        Throws:
        IOException
      • sendJson

        public void sendJson​(int status)
                      throws IOException
        Serializes the status message and writes it in the response, using the given HTTP status.
        Throws:
        IOException
      • getGson

        @NotNull
        public @NotNull com.google.gson.Gson getGson()