Package com.composum.sling.core.servlet
Class Status
- java.lang.Object
-
- com.composum.sling.core.servlet.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 thedata(String)
/list(String)
methods. Can be serialized and deserialized withGson
, 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 Summary
Fields Modifier and Type Field Description protected @Nullable Map<String,Map<String,Object>>
data
static String
DATA
Constant for often used argument fordata(String)
.protected com.google.gson.Gson
gson
protected @Nullable Map<String,List<Map<String,Object>>>
list
protected @Nullable org.slf4j.Logger
messageLogger
If set, added messages will be logged here.protected @Nullable MessageContainer
messages
protected org.apache.sling.api.SlingHttpServletRequest
request
protected org.apache.sling.api.SlingHttpServletResponse
response
protected int
status
protected boolean
success
protected @Nullable String
title
protected boolean
warning
-
Constructor Summary
Constructors Constructor Description Status()
Deprecated.Constructor for deserialization with gson onlyStatus(@NotNull com.google.gson.GsonBuilder gsonBuilder, @Nullable org.apache.sling.api.SlingHttpServletRequest request, @Nullable org.apache.sling.api.SlingHttpServletResponse response, @Nullable org.slf4j.Logger messageLogger)
ConstructionStatus(@NotNull com.google.gson.Gson gson, @Nullable org.apache.sling.api.SlingHttpServletRequest request, @Nullable org.apache.sling.api.SlingHttpServletResponse response)
Deprecated.since it's dangerous to forget#initGson(GsonBuilder, SlingHttpServletRequest)
.Status(@Nullable org.apache.sling.api.SlingHttpServletRequest request, @Nullable org.apache.sling.api.SlingHttpServletResponse response)
Constructor without logging messages - considerStatus(SlingHttpServletRequest, SlingHttpServletResponse, Logger)
to automatically log added messages.Status(@Nullable org.apache.sling.api.SlingHttpServletRequest request, @Nullable org.apache.sling.api.SlingHttpServletResponse response, @Nullable org.slf4j.Logger messageLogger)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description @NotNull Message
addMessage(@NotNull Message message)
Adds the message and returns it.@NotNull Message
addValidationMessage(@NotNull Message.Level level, @Nullable String context, @Nullable String label, @NotNull String text, Object... args)
For use with validation messages: add context and label, too.protected void
adjustToMessageLevel(Message.Level level)
Adjusts title, status and success / warning according to message level.@NotNull Map<String,Object>
data(@NotNull String name)
Returns a map that is saved with the given name in the data section of the Status, creating it if necessary.void
error(@NotNull String text, @NotNull Throwable exception)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.void
error(@NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.@NotNull com.google.gson.Gson
getGson()
@NotNull String
getJsonString()
Returns the JSON representation of this status as String, primarily for logging.@NotNull MessageContainer
getMessages()
@Nullable String
getRequiredParameter(@NotNull String paramName, @Nullable Pattern pattern, @NotNull String errorMessage)
Retrieves and validates a required parameter from the request, adding the errorMessage if that fails.List<String>
getRequiredParameters(@NotNull String paramName, @Nullable Pattern pattern, @NotNull String errorMessage)
int
getStatus()
String
getTitle()
boolean
hasTitle()
void
info(@NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.static @NotNull com.google.gson.GsonBuilder
initGson(@NotNull com.google.gson.GsonBuilder gsonBuilder, @NotNull Supplier<org.apache.sling.api.SlingHttpServletRequest> requestProvider)
Registers a type adapter for the JSON serialization ofMessage
andMessageContainer
that performs i18n according to the given request.boolean
isError()
boolean
isSuccess()
boolean
isValid()
boolean
isWarning()
@NotNull List<Map<String,Object>>
list(@NotNull String name)
Returns the list for the given name, creating it if neccesary.void
list(@NotNull String name, Collection<org.apache.sling.api.resource.Resource> items)
Adds a list of resources in a request with common information.void
reference(@NotNull String name, org.apache.sling.api.resource.Resource resource)
Adds information about resource asdata(String)
object.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).void
sendJson()
Serializes the status message and writes it in the response, usinggetStatus()
as HTTP status.void
sendJson(int status)
Serializes the status message and writes it in the response, using the given HTTP status.@NotNull Status
setMessageLogger(@Nullable org.slf4j.Logger messageLogger)
Sets the logger of thegetMessages()
message container to logger.void
setStatus(int status)
void
setTitle(String rawTitle)
void
setWarning(boolean value)
Message
shortMessage(@NotNull Message.Level level, @NotNull String text, Object... args)
Adds a message with minimum information.void
toJson(@NotNull com.google.gson.stream.JsonWriter writer)
Writes this object as JSON usingGson
.String
toString()
void
validationError(@NotNull String context, @NotNull String label, @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.void
validationInfo(@NotNull String context, @NotNull String label, @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.void
validationWarn(@NotNull String context, @NotNull String label, @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.void
warn(@NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.@NotNull Status
withLogging(@NotNull org.slf4j.Logger logger)
Deprecated.rather use the constructorStatus(SlingHttpServletRequest, SlingHttpServletResponse, Logger)
,Status(GsonBuilder, SlingHttpServletRequest, SlingHttpServletResponse, Logger)
or, if need be,setMessageLogger(Logger)
.
-
-
-
Field Detail
-
DATA
public static final String DATA
Constant for often used argument fordata(String)
.- See Also:
- Constant Field Values
-
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
-
messages
@Nullable protected @Nullable MessageContainer messages
-
-
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 - considerStatus(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 ofMessage
andMessageContainer
that performs i18n according to the given request. If there is no request, we can skip this step as these classes are annotated withJsonAdapter
.
-
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)
-
getMessages
@NotNull public @NotNull MessageContainer getMessages()
-
isValid
public boolean isValid()
-
isSuccess
public boolean isSuccess()
-
isWarning
public boolean isWarning()
-
setWarning
public void setWarning(boolean value)
-
isError
public boolean isError()
-
info
public void info(@NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.
-
validationInfo
public void validationInfo(@NotNull @NotNull String context, @NotNull @NotNull String label, @NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.
-
warn
public void warn(@NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.
-
validationWarn
public void validationWarn(@NotNull @NotNull String context, @NotNull @NotNull String label, @NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.
-
error
public void error(@NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
.
-
error
public void error(@NotNull @NotNull String text, @NotNull @NotNull Throwable exception)
For the meaning of arguments - compareshortMessage(Level, String, Object...)
. This adds theThrowable.getLocalizedMessage()
as message argument and logs the exception if a message logger is set.
-
validationError
public void validationError(@NotNull @NotNull String context, @NotNull @NotNull String label, @NotNull @NotNull String text, Object... args)
For the meaning of arguments - compareaddValidationMessage(Level, String, String, String, Object...)
.
-
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 asdata(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 leveltext
- non prepared text message, may contain {} placeholdersargs
- 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 levelcontext
- non prepared context keylabel
- 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 usingGson
.- 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, usinggetStatus()
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()
-
setMessageLogger
@NotNull public @NotNull Status setMessageLogger(@Nullable @Nullable org.slf4j.Logger messageLogger)
Sets the logger of thegetMessages()
message container to logger. Thus, new added messages will also be logged with this logger. Please consider using the constructorStatus(SlingHttpServletRequest, SlingHttpServletResponse, Logger)
orStatus(GsonBuilder, SlingHttpServletRequest, SlingHttpServletResponse, Logger)
.- Returns:
- this in builder-style
-
withLogging
@NotNull @Deprecated public @NotNull Status withLogging(@NotNull @NotNull org.slf4j.Logger logger)
Deprecated.rather use the constructorStatus(SlingHttpServletRequest, SlingHttpServletResponse, Logger)
,Status(GsonBuilder, SlingHttpServletRequest, SlingHttpServletResponse, Logger)
or, if need be,setMessageLogger(Logger)
. This is kept for backwards compatibility for a while.Sets the logger of thegetMessages()
message container to logger. Thus, new added messages will also be logged with this logger.- Returns:
- this in builder-style
-
-