Class ResponseService

java.lang.Object
org.trustdeck.service.ResponseService

@Service public class ResponseService extends Object
Handles the responses depending on the response type requested by the user.
Author:
Eric Wündisch and Armin Müller
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Set<String>
    Defines a list of media types that the services is able to respond with.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> org.springframework.http.ResponseEntity<T>
    badRequest(String mediaType)
    Bad request (400) response entity.
    Builds the content type header string for the response.
    <T> org.springframework.http.ResponseEntity<T>
    conflict(String mediaType)
    Conflict (409) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    created(String mediaType)
    Created (201) response entity without a body and without a location.
    <T> org.springframework.http.ResponseEntity<T>
    created(String mediaType, URI location)
    Created (201) response entity without a body and with a location.
    <T> org.springframework.http.ResponseEntity<T>
    created(String mediaType, URI location, T body)
    Created (201) response entity with a body and with a location.
    <T> org.springframework.http.ResponseEntity<T>
    created(String mediaType, T body)
    Created (201) response entity with a body and without a location.
    createHttpStatusDtoFromMediaType(org.springframework.http.HttpStatus status, String mediaType)
    Create a HTTP status DTO taking into account which media type the response should have.
    createHttpStatusDtoFromRequest(org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
    Create a HTTP status DTO taking the request object into account.
    <T> org.springframework.http.ResponseEntity<T>
    forbidden(String mediaType)
    Forbidden (403) response entity.
    getMediaTypeFromRequest(org.springframework.web.context.request.WebRequest request)
    Retrieves the media type value from the request.
    Checks whether the given media type is valid.
    <T> org.springframework.http.ResponseEntity<T>
    gone(String mediaType)
    Gone (410) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    Insufficient storage (507) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    Internal server error (500) response entity.
    boolean
    Checks whether or not the provided media type is in the list of supported media types.
    <T> org.springframework.http.ResponseEntity<T>
    noContent(String mediaType)
    No content (204) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    notAcceptable(String mediaType)
    Not acceptable (406) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    notFound(String mediaType)
    Not found (404) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    ok(String mediaType)
    Ok (200) response entity without a body.
    org.springframework.http.ResponseEntity<byte[]>
    ok(String mimeType, byte[] image)
    Ok (200) response entity with an image in the body.
    <T> org.springframework.http.ResponseEntity<T>
    ok(String mediaType, T body)
    Ok (200) response entity with a body.
    <T> org.springframework.http.ResponseEntity<T>
    partialContent(String mediaType, T body)
    Partial Content (206) response entity with a body.
    <T> org.springframework.http.ResponseEntity<T>
    Payload too large (413) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    Unprocessable entity (422) response entity.
    <T> org.springframework.http.ResponseEntity<T>
    Unsupported media type (415) response entity.

    Methods inherited from class java.lang.Object

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

    • supportedMediaTypes

      public static final Set<String> supportedMediaTypes
      Defines a list of media types that the services is able to respond with.
  • Constructor Details

    • ResponseService

      public ResponseService()
  • Method Details

    • isMediaTypeSupportedByService

      public boolean isMediaTypeSupportedByService(String mediaType)
      Checks whether or not the provided media type is in the list of supported media types.
      Parameters:
      mediaType - the media type
      Returns:
      whether the media type is supported or not
    • getValidMediaType

      public String getValidMediaType(String mediaType)
      Checks whether the given media type is valid. If not the default preferred media type is returned.
      Parameters:
      mediaType - the media type to validate
      Returns:
      the given media type if it's valid, the preferred media type if not
    • getMediaTypeFromRequest

      public String getMediaTypeFromRequest(org.springframework.web.context.request.WebRequest request)
      Retrieves the media type value from the request.
      Parameters:
      request - the request
      Returns:
      the media type value from the request
    • createHttpStatusDtoFromRequest

      public Object createHttpStatusDtoFromRequest(org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
      Create a HTTP status DTO taking the request object into account.
      Parameters:
      status - the HTTP status
      request - the request
      Returns:
      the HTTP status response DTO or an empty string
    • createHttpStatusDtoFromMediaType

      public Object createHttpStatusDtoFromMediaType(org.springframework.http.HttpStatus status, String mediaType)
      Create a HTTP status DTO taking into account which media type the response should have.
      Parameters:
      status - the HTTP status
      mediaType - the media type the response should have
      Returns:
      the HTTP status response DTO or an (empty) string
    • buildContentTypeHeaderStringForResponse

      public String buildContentTypeHeaderStringForResponse(String mediaType)
      Builds the content type header string for the response.
      Parameters:
      mediaType - the media type
      Returns:
      the content type header string for the response
    • notFound

      public <T> org.springframework.http.ResponseEntity<T> notFound(String mediaType)
      Not found (404) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • badRequest

      public <T> org.springframework.http.ResponseEntity<T> badRequest(String mediaType)
      Bad request (400) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • notAcceptable

      public <T> org.springframework.http.ResponseEntity<T> notAcceptable(String mediaType)
      Not acceptable (406) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • unprocessableEntity

      public <T> org.springframework.http.ResponseEntity<T> unprocessableEntity(String mediaType)
      Unprocessable entity (422) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • internalServerError

      public <T> org.springframework.http.ResponseEntity<T> internalServerError(String mediaType)
      Internal server error (500) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • noContent

      public <T> org.springframework.http.ResponseEntity<T> noContent(String mediaType)
      No content (204) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • conflict

      public <T> org.springframework.http.ResponseEntity<T> conflict(String mediaType)
      Conflict (409) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • insufficientStorage

      public <T> org.springframework.http.ResponseEntity<T> insufficientStorage(String mediaType)
      Insufficient storage (507) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • ok

      public <T> org.springframework.http.ResponseEntity<T> ok(String mediaType)
      Ok (200) response entity without a body.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • ok

      public <T> org.springframework.http.ResponseEntity<T> ok(String mediaType, T body)
      Ok (200) response entity with a body.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      body - the body
      Returns:
      the response entity
    • ok

      public org.springframework.http.ResponseEntity<byte[]> ok(String mimeType, byte[] image)
      Ok (200) response entity with an image in the body.
      Parameters:
      mimeType - the mime type of the image
      image - the image in byte-array representation
      Returns:
      the response entity
    • created

      public <T> org.springframework.http.ResponseEntity<T> created(String mediaType)
      Created (201) response entity without a body and without a location.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • created

      public <T> org.springframework.http.ResponseEntity<T> created(String mediaType, T body)
      Created (201) response entity with a body and without a location.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      body - the body
      Returns:
      the response entity
    • created

      public <T> org.springframework.http.ResponseEntity<T> created(String mediaType, URI location)
      Created (201) response entity without a body and with a location.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      location - the location
      Returns:
      the response entity
    • created

      public <T> org.springframework.http.ResponseEntity<T> created(String mediaType, URI location, T body)
      Created (201) response entity with a body and with a location.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      location - the location
      body - the body
      Returns:
      the response entity
    • forbidden

      public <T> org.springframework.http.ResponseEntity<T> forbidden(String mediaType)
      Forbidden (403) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • partialContent

      public <T> org.springframework.http.ResponseEntity<T> partialContent(String mediaType, T body)
      Partial Content (206) response entity with a body.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      body - the body
      Returns:
      the response entity
    • gone

      public <T> org.springframework.http.ResponseEntity<T> gone(String mediaType)
      Gone (410) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • payloadTooLarge

      public <T> org.springframework.http.ResponseEntity<T> payloadTooLarge(String mediaType)
      Payload too large (413) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity
    • unsupportedMediaType

      public <T> org.springframework.http.ResponseEntity<T> unsupportedMediaType(String mediaType)
      Unsupported media type (415) response entity.
      Type Parameters:
      T - the type parameter
      Parameters:
      mediaType - the media type
      Returns:
      the response entity