Class AlpineResource

java.lang.Object
alpine.server.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 final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final List<ValidationException>
    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 void
    Wrapper around contOnValidationError(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 void
    failOnValidationError(Set<javax.validation.ConstraintViolation<Object>>... violationsArray)
    Wrapper around contOnValidationError(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 AlpineRequest
    Returns the AlpineRequest object containing pagination, order, filters, and other Alpine-specified aspects of the request.
    protected Principal
    Returns the principal for who initiated the request.
    protected String
    Convenience method that returns the remote IP address that made the request.
    protected String
    Convenience method that returns the remote hostname that made the request.
    protected javax.ws.rs.container.ContainerRequestContext
    Returns the ContainerRequestContext.
    protected javax.ws.rs.core.UriInfo
    Returns the UriInfo.
    protected String
    Convenience method that returns the User-Agent string for the application that made the request.
    protected javax.validation.Validator
    Returns a Validator instance.
    protected boolean
    hasPermission(String permission)
    Convenience method that returns true if the principal has the specified permission, or false if not.
    protected boolean
     
    protected boolean
     
    protected boolean
     
    protected boolean
     
    protected void
    logSecurityEvent(Logger logger, org.slf4j.Marker marker, String message)
    Logs a security event to the security audit log.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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

      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 around contOnValidationError(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 around contOnValidationError(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.
      Returns:
      a Principal object
      Since:
      1.0.0
      See Also:
    • 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_AUDIT SecurityMarkers.SECURITY_SUCCESS SecurityMarkers.SECURITY_FAILURE
      Parameters:
      logger - the logger to use
      marker - the marker to add to the event
      message - the initial content of the event
      Since:
      1.0.0