Class RestUtils

java.lang.Object
org.opencastproject.index.service.util.RestUtils

public final class RestUtils extends Object
Utils method for the Rest Endpoint implementation
  • Method Summary

    Modifier and Type
    Method
    Description
    static javax.ws.rs.core.Response
    conflictJson(com.entwinemedia.fn.data.json.JValue json)
    Create an CONFLICT (409) response with the given JSON as body
    static org.opencastproject.util.data.Tuple<Date,Date>
    getFromAndToDateRange(String fromToDateRange)
    Parse the UTC format date range string to two Date objects to represent a range of dates.
    static String
    getJsonString(com.entwinemedia.fn.data.json.JValue json)
     
    static String
    getJsonStringSilent(com.entwinemedia.fn.data.json.JValue json)
    Get a String value from a JValue ignoring errors.
    static javax.ws.rs.core.Response
    notFound(String msg, Object... args)
    Create a NOT FOUND (404) response with the given messages and arguments
    static javax.ws.rs.core.Response
    notFoundJson(com.entwinemedia.fn.data.json.JValue json)
    Create a NOT FOUND (404) response with the given JSON as body
    static javax.ws.rs.core.Response
    okJson(com.entwinemedia.fn.data.json.JValue json)
    Create an OK (200) response with the given JSON as body
    static javax.ws.rs.core.Response
    okJsonList(List<com.entwinemedia.fn.data.json.JValue> jsonList, int offset, int limit, long total)
    Return the given list of value with the standard format for JSON list value with offset, limit and total information.
    static javax.ws.rs.core.Response
    okJsonList(List<com.entwinemedia.fn.data.json.JValue> jsonList, Optional<Integer> optOffset, Optional<Integer> optLimit, long total)
    Return the given list of value with the standard format for JSON list value with offset, limit and total information.
    static Map<String,String>
    Parse the filter to a Map
    static ArrayList<org.opencastproject.util.requests.SortCriterion>
    Parse a sort query parameter to a set of SortCriterion.
    static javax.ws.rs.core.Response
    serverErrorJson(com.entwinemedia.fn.data.json.JValue json)
    Create an INTERNAL SERVER ERROR (500) response with the given JSON as body
    static javax.ws.rs.core.StreamingOutput
    stream(com.entwinemedia.fn.Fx<OutputStream> out)
    Create a streaming response entity.

    Methods inherited from class java.lang.Object

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

    • okJson

      public static javax.ws.rs.core.Response okJson(com.entwinemedia.fn.data.json.JValue json)
      Create an OK (200) response with the given JSON as body
      Parameters:
      json - the JSON string to add to the response body.
      Returns:
      an OK response
    • conflictJson

      public static javax.ws.rs.core.Response conflictJson(com.entwinemedia.fn.data.json.JValue json)
      Create an CONFLICT (409) response with the given JSON as body
      Parameters:
      json - the JSON string to add to the response body.
      Returns:
      an OK response
    • notFound

      public static javax.ws.rs.core.Response notFound(String msg, Object... args)
      Create a NOT FOUND (404) response with the given messages and arguments
      Parameters:
      msg -
      args -
      Returns:
      a NOT FOUND response
    • notFoundJson

      public static javax.ws.rs.core.Response notFoundJson(com.entwinemedia.fn.data.json.JValue json)
      Create a NOT FOUND (404) response with the given JSON as body
      Parameters:
      json - the JSON string to add to the response body.
      Returns:
      a NOT FOUND response
    • serverErrorJson

      public static javax.ws.rs.core.Response serverErrorJson(com.entwinemedia.fn.data.json.JValue json)
      Create an INTERNAL SERVER ERROR (500) response with the given JSON as body
      Parameters:
      json - the JSON string to add to the response body.
      Returns:
      an INTERNAL SERVER ERROR response
    • okJsonList

      public static javax.ws.rs.core.Response okJsonList(List<com.entwinemedia.fn.data.json.JValue> jsonList, int offset, int limit, long total)
      Return the given list of value with the standard format for JSON list value with offset, limit and total information. See also okJsonList(List, Optional, Optional, long).
      Parameters:
      jsonList - The list of value to return
      offset - The result offset
      limit - The maximal list size
      total - The amount of available items in the system
      Returns:
      a Response with an JSON object formatted like above as body.
      Throws:
      IllegalArgumentException - if the value list is null
    • okJsonList

      public static javax.ws.rs.core.Response okJsonList(List<com.entwinemedia.fn.data.json.JValue> jsonList, Optional<Integer> optOffset, Optional<Integer> optLimit, long total)
      Return the given list of value with the standard format for JSON list value with offset, limit and total information. The JSON object in the response body has the following format:
       {
        results: [
          // array containing all the object from the given list
        ],
        count: 12, // The number of item returned (size of the given list)
        offset: 2, // The result offset (given parameter)
        limit: 12, // The maximal size of the list (given parameter)
        total: 123 // The total number of items available in the system (given parameter)
       }
       
      Limit and offset are optional.
      Parameters:
      jsonList - The list of value to return
      optOffset - The result offset (optional)
      optLimit - The maximal list size (optional)
      total - The amount of available items in the system
      Returns:
      a Response with an JSON object formatted like above as body.
      Throws:
      IllegalArgumentException - if the value list is null
    • stream

      public static javax.ws.rs.core.StreamingOutput stream(com.entwinemedia.fn.Fx<OutputStream> out)
      Create a streaming response entity. Pass it as an entity parameter to one of the response builder methods like RestUtil.R.ok(Object).
    • parseSortQueryParameter

      public static ArrayList<org.opencastproject.util.requests.SortCriterion> parseSortQueryParameter(String sort) throws javax.ws.rs.WebApplicationException
      Parse a sort query parameter to a set of SortCriterion. The parameter has to be of the following form: <field name>:ASC|DESC
      Parameters:
      sort - the parameter string to parse (will be checked if blank)
      Returns:
      a set of sort criterion, never null
      Throws:
      javax.ws.rs.WebApplicationException
    • getFromAndToDateRange

      public static org.opencastproject.util.data.Tuple<Date,Date> getFromAndToDateRange(String fromToDateRange)
      Parse the UTC format date range string to two Date objects to represent a range of dates.

      Sample UTC date range format string:
      i.e. yyyy-MM-ddTHH:mm:ssZ/yyyy-MM-ddTHH:mm:ssZ e.g. 2014-09-27T16:25Z/2014-09-27T17:55Z

      Parameters:
      fromToDateRange - The string that represents the UTC formed date range.
      Returns:
      A Tuple with the two Dates
      Throws:
      IllegalArgumentException - Thrown if the input string is malformed
    • parseFilter

      public static Map<String,String> parseFilter(String filter)
      Parse the filter to a Map
      Parameters:
      filter - the filters
      Returns:
      the map of filter name and values
    • getJsonString

      public static String getJsonString(com.entwinemedia.fn.data.json.JValue json) throws javax.ws.rs.WebApplicationException, IOException
      Throws:
      javax.ws.rs.WebApplicationException
      IOException
    • getJsonStringSilent

      public static String getJsonStringSilent(com.entwinemedia.fn.data.json.JValue json)
      Get a String value from a JValue ignoring errors.
      Parameters:
      json - The JValue to convert to a String
      Returns:
      The String representation of the JValue or an empty string if there was an error.