Class JsonNamedBeanHttpService<T extends NamedBean>

Type Parameters:
T - the type supported by this service
Direct Known Subclasses:
JsonBlockHttpService, JsonIdTagHttpService, JsonLightHttpService, JsonMemoryHttpService, JsonOblockHttpService, JsonReporterHttpService, JsonRouteHttpService, JsonSensorHttpService, JsonSignalMastHttpService, JsonTurnoutHttpService

public abstract class JsonNamedBeanHttpService<T extends NamedBean>
extends JsonNonProvidedNamedBeanHttpService<T>
Abstract implementation of JsonHttpService with specific support for NamedBean objects.

Note: services requiring support for multiple classes of NamedBean cannot extend this class.

Note: NamedBeans must be managed by a ProvidingManager for this class to be used.

  • Constructor Details

  • Method Details

    • doGet

      @Nonnull public final com.fasterxml.jackson.databind.JsonNode doGet​(@Nonnull String type, @Nonnull String name, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Respond to an HTTP GET request for the requested name.

      If name is null, return a list of all objects for the given type, if appropriate.

      This method should throw a 500 Internal Server Error if type is not recognized.

      Specified by:
      doGet in class JsonHttpService
      Parameters:
      type - the type of the requested object
      name - the system name of the requested object
      data - JSON data set of attributes of the requested object
      request - the JSON request
      Returns:
      a JSON description of the requested object
      Throws:
      JsonException - if the named object does not exist or other error occurs
    • doPost

      @Nonnull public final com.fasterxml.jackson.databind.JsonNode doPost​(@Nonnull String type, @Nonnull String name, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Respond to an HTTP POST request for the requested name.

      This method should throw a 400 Invalid Request error if the named object does not exist.

      Specified by:
      doPost in class JsonHttpService
      Parameters:
      type - the type of the requested object
      name - the system name of the requested object
      data - JSON data set of attributes of the requested object to be updated
      request - the JSON request
      Returns:
      a JSON description of the requested object after updates have been applied
      Throws:
      JsonException - if the named object does not exist or other error occurs
    • doPut

      public com.fasterxml.jackson.databind.JsonNode doPut​(@Nonnull String type, @Nonnull String name, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Respond to an HTTP PUT request for the requested name.

      Throw an HTTP 405 Method Not Allowed exception if new objects of the type are not intended to be addable. Override if the implementing class needs to prevent PUT methods from functioning or need to perform additional validation prior to creating the NamedBean.

      Overrides:
      doPut in class JsonHttpService
      Parameters:
      type - the type of the requested object
      name - the system name of the requested object
      data - JSON data set of attributes of the requested object to be created or updated
      request - the JSON request
      Returns:
      a JSON description of the requested object
      Throws:
      JsonException - if the method is not allowed or other error occurs
    • doGetList

      @Nonnull public final com.fasterxml.jackson.databind.JsonNode doGetList​(String type, com.fasterxml.jackson.databind.JsonNode data, JsonRequest request) throws JsonException
      Respond to an HTTP GET request for a list of items of type.

      This is called by the JsonServlet to handle get requests for a type, but no name. Services that do not have named objects, such as the JsonTimeHttpService should respond to this with a list containing a single JSON object. Services that can't return a list may throw a 400 Bad Request JsonException in this case.

      Specified by:
      doGetList in class JsonHttpService
      Parameters:
      type - the type of the requested list
      data - JSON data set of attributes of the requested objects
      request - the JSON request
      Returns:
      a JSON list or message containing type "list", the list as data, and the passed in id
      Throws:
      JsonException - may be thrown by concrete implementations
    • doDelete

      public void doDelete​(String type, String name, com.fasterxml.jackson.databind.JsonNode data, JsonRequest request) throws JsonException
      Respond to an HTTP DELETE request for the requested name.

      Throw an HTTP 405 Method Not Allowed exception if the object is not intended to be removable.

      Do not throw an error if the requested object does not exist.

      Overrides:
      doDelete in class JsonHttpService
      Parameters:
      type - the type of the deleted object
      name - the system name of the deleted object
      data - additional data
      request - the JSON request
      Throws:
      JsonException - if this method is not allowed or other error occurs
    • doGet

      @Nonnull protected abstract com.fasterxml.jackson.databind.node.ObjectNode doGet​(T bean, @Nonnull String name, @Nonnull String type, @Nonnull JsonRequest request) throws JsonException
      Respond to an HTTP GET request for the requested name.

      If name is null, return a list of all objects for the given type, if appropriate.

      This method should throw a 500 Internal Server Error if type is not recognized.

      Specified by:
      doGet in class JsonNonProvidedNamedBeanHttpService<T extends NamedBean>
      Parameters:
      bean - the requested object
      name - the name of the requested object
      type - the type of the requested object
      request - the JSON request
      Returns:
      a JSON description of the requested object
      Throws:
      JsonException - if the named object does not exist or other error occurs
    • getNamedBean

      @CheckForNull public T getNamedBean​(@Nonnull String type, @Nonnull String name, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Get the NamedBean matching name and type. If the request has a method other than GET, this may modify or create the NamedBean requested. Note that name or data may be null, but it is an error to have both be null.
      Specified by:
      getNamedBean in class JsonNonProvidedNamedBeanHttpService<T extends NamedBean>
      Parameters:
      type - the type of the requested object
      name - the name of the requested object
      data - the JsonNode containing the JSON representation of the bean to get
      request - the JSON request
      Returns:
      the matching NamedBean or null if there is no match
      Throws:
      JsonException - if the name is invalid for the type
    • doPost

      @Nonnull protected abstract com.fasterxml.jackson.databind.node.ObjectNode doPost​(T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Respond to an HTTP POST request for the requested name.
      Parameters:
      bean - the requested object
      name - the name of the requested object
      type - the type of the requested object
      data - data describing the requested object
      request - the JSON request
      Returns:
      a JSON description of the requested object
      Throws:
      JsonException - if an error occurs
    • doDelete

      protected void doDelete​(@CheckForNull T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Delete the requested bean.

      This method must be overridden to allow a bean to be deleted. The simplest overriding method body is: deleteBean(bean, name, type, data, locale, id);

      Parameters:
      bean - the bean to delete
      name - the named of the bean to delete
      type - the type of the bean to delete
      data - data describing the named bean
      request - the JSON request
      Throws:
      JsonException - if an error occurs
    • doPost

      @Deprecated @Nonnull protected com.fasterxml.jackson.databind.node.ObjectNode doPost​(T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull Locale locale, int id) throws JsonException
      Deprecated.
      Respond to an HTTP POST request for the requested name.
      Parameters:
      bean - the requested object
      name - the name of the requested object
      type - the type of the requested object
      data - data describing the requested object
      locale - the requesting client's Locale
      id - the message id set by the client
      Returns:
      a JSON description of the requested object
      Throws:
      JsonException - if an error occurs
    • doDelete

      @Deprecated protected void doDelete​(@CheckForNull T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull Locale locale, int id) throws JsonException
      Deprecated.
      Delete the requested bean.

      This method must be overridden to allow a bean to be deleted. The simplest overriding method body is: deleteBean(bean, name, type, data, locale, id);

      Parameters:
      bean - the bean to delete
      name - the named of the bean to delete
      type - the type of the bean to delete
      data - data describing the named bean
      locale - the requesting client's Locale
      id - the message id set by the client
      Throws:
      JsonException - if an error occurs
    • deleteBean

      protected final void deleteBean​(@CheckForNull T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull JsonRequest request) throws JsonException
      Delete the requested bean. This is the simplest method to delete a bean, and is likely to become the default implementation of doDelete(NamedBean, String, String, JsonNode, Locale, int) in an upcoming release of JMRI.
      Parameters:
      bean - the bean to delete
      name - the named of the bean to delete
      type - the type of the bean to delete
      data - data describing the named bean
      request - the JSON request
      Throws:
      JsonException - if an error occurs
    • deleteBean

      @Deprecated protected final void deleteBean​(@CheckForNull T bean, @Nonnull String name, @Nonnull String type, @Nonnull com.fasterxml.jackson.databind.JsonNode data, @Nonnull Locale locale, int id) throws JsonException
      Delete the requested bean. This is the simplest method to delete a bean, and is likely to become the default implementation of doDelete(NamedBean, String, String, JsonNode, Locale, int) in an upcoming release of JMRI.
      Parameters:
      bean - the bean to delete
      name - the named of the bean to delete
      type - the type of the bean to delete
      data - data describing the named bean
      locale - the requesting client's Locale
      id - the message id set by the client
      Throws:
      JsonException - if an error occurs
    • getType

      @Nonnull protected abstract String getType()
      Get the JSON type supported by this service.
      Returns:
      the JSON type
    • getManager

      @Nonnull protected abstract ProvidingManager<T> getManager()
      Get the expected manager for the supported JSON type. This should normally be the default manager.
      Returns:
      the manager