Package alpine.server.resources
Class AlpineResource
java.lang.Object
alpine.server.resources.AlpineResource
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final List<ValidationException>contOnValidationError(ValidationTask... validationTasks) Given one or mote ValidationTasks, this method will return a List of ValidationErrors.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.protected final 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 final 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 AlpineRequestReturns the AlpineRequest object containing pagination, order, filters, and other Alpine-specified aspects of the request.protected PrincipalReturns the principal for who initiated the request.protected StringConvenience method that returns the remote IP address that made the request.protected StringConvenience method that returns the remote hostname that made the request.protected javax.ws.rs.container.ContainerRequestContextReturns the ContainerRequestContext.protected javax.ws.rs.core.UriInfoReturns the UriInfo.protected StringConvenience method that returns the User-Agent string for the application that made the request.protected javax.validation.ValidatorReturns 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 booleanprotected booleanprotected booleanprotected voidlogSecurityEvent(Logger logger, org.slf4j.Marker marker, String message) Logs a security event to the security audit log.
-
Field Details
-
TOTAL_COUNT_HEADER
- See Also:
-
-
Constructor Details
-
AlpineResource
public AlpineResource()
-
-
Method Details
-
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
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
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
Convenience method that returns the remote hostname that made the request.- Returns:
- the remote hostname as a String
- Since:
- 1.0.0
-
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
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
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
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
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
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
-