Package alpine.resources
Class AlpineResource
- java.lang.Object
-
- alpine.resources.AlpineResource
-
public abstract class AlpineResource extends Object
A value-added resource that all Alpine resources should extend from. This resource provides access to pagination, ordering, filtering, convenience methods for obtaining specific HTTP request information, along with the ability to perform input validation and automatically fail requests (with HTTP status 400) if validation failure occurs.- Since:
- 1.0.0
- Author:
- Steve Springett
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringTOTAL_COUNT_HEADER
-
Constructor Summary
Constructors Constructor Description AlpineResource()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected List<ValidationException>contOnValidationError(ValidationTask... validationTasks)Given one or mote ValidationTasks, this method will return a List of ValidationErrors.protected List<org.glassfish.jersey.server.validation.ValidationError>contOnValidationError(Set<javax.validation.ConstraintViolation<Object>>... violationsArray)Accepts the result from one of the many validation methods available and returns a List of ValidationErrors.protected voidfailOnValidationError(ValidationTask... validationTasks)Wrapper aroundcontOnValidationError(ValidationTask[])but instead of returning a list of errors, this method will halt processing of the request by throwing a BadRequestException, setting the HTTP status to 400 (BAD REQUEST) and providing a full list of validation errors in the body of the response.protected voidfailOnValidationError(Set<javax.validation.ConstraintViolation<Object>>... violationsArray)Wrapper aroundcontOnValidationError(Set[])but instead of returning a list of errors, this method will halt processing of the request by throwing a BadRequestException, setting the HTTP status to 400 (BAD REQUEST) and providing a full list of validation errors in the body of the response.protected AlpineRequestgetAlpineRequest()Returns the AlpineRequest object containing pagination, order, filters, and other Alpine-specified aspects of the request.protected PrincipalgetPrincipal()Returns the principal for who initiated the request.protected StringgetRemoteAddress()Convenience method that returns the remote IP address that made the request.protected StringgetRemoteHost()Convenience method that returns the remote hostname that made the request.protected javax.ws.rs.container.ContainerRequestContextgetRequestContext()Returns the ContainerRequestContext.protected javax.ws.rs.core.UriInfogetUriInfo()Returns the UriInfo.protected StringgetUserAgent()Convenience method that returns the User-Agent string for the application that made the request.protected javax.validation.ValidatorgetValidator()Returns a Validator instance.protected booleanhasPermission(String permission)Convenience method that returns true if the principal has the specified permission, or false if not.protected booleanisApiKey()protected booleanisLdapUser()protected booleanisManagedUser()protected booleanisOidcUser()protected voidlogSecurityEvent(Logger logger, org.slf4j.Marker marker, String message)Logs a security event to the security audit log.
-
-
-
Field Detail
-
TOTAL_COUNT_HEADER
protected static final String TOTAL_COUNT_HEADER
- See Also:
- Constant Field Values
-
-
Method Detail
-
getRequestContext
protected javax.ws.rs.container.ContainerRequestContext getRequestContext()
Returns the ContainerRequestContext. This is automatically injected into every instance of an AlpineResource.- Returns:
- the ContainerRequestContext
- Since:
- 1.0.0
-
getUriInfo
protected javax.ws.rs.core.UriInfo getUriInfo()
Returns the UriInfo. This is automatically injected into every instance of an AlpineResource.- Returns:
- the UriInfo
- Since:
- 1.0.0
-
getAlpineRequest
protected AlpineRequest getAlpineRequest()
Returns the AlpineRequest object containing pagination, order, filters, and other Alpine-specified aspects of the request.- Returns:
- a AlpineRequest object
- Since:
- 1.0.0
-
getRemoteAddress
protected String getRemoteAddress()
Convenience method that returns the remote IP address that made the request.- Returns:
- the remote IP address as a String
- Since:
- 1.0.0
-
getRemoteHost
protected String getRemoteHost()
Convenience method that returns the remote hostname that made the request.- Returns:
- the remote hostname as a String
- Since:
- 1.0.0
-
getUserAgent
protected String getUserAgent()
Convenience method that returns the User-Agent string for the application that made the request.- Returns:
- the User-Agent as a String
- Since:
- 1.0.0
-
getValidator
protected javax.validation.Validator getValidator()
Returns a Validator instance. Internally, this uses Validation.buildDefaultValidatorFactory().getValidator() so only call this method sparingly and keep a reference to the Validator if possible.- Returns:
- a Validator object
- Since:
- 1.0.0
-
contOnValidationError
@SafeVarargs protected final List<org.glassfish.jersey.server.validation.ValidationError> contOnValidationError(Set<javax.validation.ConstraintViolation<Object>>... violationsArray)
Accepts the result from one of the many validation methods available and returns a List of ValidationErrors. If the size of the List is 0, no errors were encounter during validation. Usage:Validator validator = getValidator(); List<ValidationError> errors = contOnValidationError( validator.validateProperty(myObject, "uuid"), validator.validateProperty(myObject, "name") ); // If validation fails, this line will be reached.- Parameters:
violationsArray- a Set of one or more ConstraintViolations- Returns:
- a List of zero or more ValidationErrors
- Since:
- 1.0.0
-
failOnValidationError
@SafeVarargs protected final void failOnValidationError(Set<javax.validation.ConstraintViolation<Object>>... violationsArray)
Wrapper aroundcontOnValidationError(Set[])but instead of returning a list of errors, this method will halt processing of the request by throwing a BadRequestException, setting the HTTP status to 400 (BAD REQUEST) and providing a full list of validation errors in the body of the response. Usage:Validator validator = getValidator(); failOnValidationError( validator.validateProperty(myObject, "uuid"), validator.validateProperty(myObject, "name") ); // If validation fails, this line will not be reached.- Parameters:
violationsArray- a Set of one or more ConstraintViolations- Since:
- 1.0.0
-
contOnValidationError
protected final List<ValidationException> contOnValidationError(ValidationTask... validationTasks)
Given one or mote ValidationTasks, this method will return a List of ValidationErrors. If the size of the List is 0, no errors were encountered during validation. Usage:List<ValidationException> errors = contOnValidationError( new ValidationTask(pattern, input, errorMessage), new ValidationTask(pattern, input, errorMessage) ); // If validation fails, this line will be reached.- Parameters:
validationTasks- an array of one or more ValidationTasks- Returns:
- a List of zero or more ValidationException
- Since:
- 1.0.0
-
failOnValidationError
protected final void failOnValidationError(ValidationTask... validationTasks)
Wrapper aroundcontOnValidationError(ValidationTask[])but instead of returning a list of errors, this method will halt processing of the request by throwing a BadRequestException, setting the HTTP status to 400 (BAD REQUEST) and providing a full list of validation errors in the body of the response. Usage:List<ValidationException> errors = failOnValidationError( new ValidationTask(pattern, input, errorMessage), new ValidationTask(pattern, input, errorMessage) ); // If validation fails, this line will not be reached.- Parameters:
validationTasks- an array of one or more ValidationTasks- Since:
- 1.0.0
-
getPrincipal
protected Principal getPrincipal()
Returns the principal for who initiated the request.
-
isOidcUser
protected boolean isOidcUser()
-
isLdapUser
protected boolean isLdapUser()
- Returns:
- true is the current Principal is an instance of LdapUser. False if not.
- Since:
- 1.0.0
-
isManagedUser
protected boolean isManagedUser()
- Returns:
- true is the current Principal is an instance of ManagedUser. False if not.
- Since:
- 1.0.0
-
isApiKey
protected boolean isApiKey()
- Returns:
- true is the current Principal is an instance of ApiKey. False if not.
- Since:
- 1.0.0
-
hasPermission
protected boolean hasPermission(String permission)
Convenience method that returns true if the principal has the specified permission, or false if not.- Parameters:
permission- the permission to check- Returns:
- true if principal has permission assigned, false if not
- Since:
- 1.2.0
-
logSecurityEvent
protected void logSecurityEvent(Logger logger, org.slf4j.Marker marker, String message)
Logs a security event to the security audit log. Expects one of:SecurityMarkers.SECURITY_AUDITSecurityMarkers.SECURITY_SUCCESSSecurityMarkers.SECURITY_FAILURE- Parameters:
logger- the logger to usemarker- the marker to add to the eventmessage- the initial content of the event- Since:
- 1.0.0
-
-