Package org.kissweb

Class RestServerBase

java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.kissweb.RestServerBase
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public abstract class RestServerBase extends javax.servlet.http.HttpServlet
This class provides the base functionality for creating non-Kiss REST servers. See org.kissweb.RestServer for an example.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static boolean
    basicAuthenticate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String relm, String userName, String password)
    Support for Basic Authentication.
    protected static byte[]
    getBodyBinary(javax.servlet.http.HttpServletRequest request)
    Get the body of the request as a binary array.
    protected static org.json.JSONObject
    getBodyJson(javax.servlet.http.HttpServletRequest request)
    Get the body of the request as a JSON object.
    protected static String
    getBodyString(javax.servlet.http.HttpServletRequest request)
    Get the body of the request as a string.
    static boolean
    Determines if the given HTTP header ContentType is binary.
    static boolean
    Determines if the given HTTP header ContentType is text.
    protected static void
    setResponse(javax.servlet.http.HttpServletResponse response, String responseType, byte[] resp)
    Set the response to a string.
    protected static void
    setResponse(javax.servlet.http.HttpServletResponse response, String responseType, String resp)
    Set the response to a string.
    protected static void
    setResponse(javax.servlet.http.HttpServletResponse response, org.json.JSONObject json)
    Set the response to a JSON object.

    Methods inherited from class javax.servlet.http.HttpServlet

    doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service

    Methods inherited from class javax.servlet.GenericServlet

    destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log

    Methods inherited from class java.lang.Object

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

    • RestServerBase

      public RestServerBase()
  • Method Details

    • basicAuthenticate

      protected static boolean basicAuthenticate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String relm, String userName, String password)
      Support for Basic Authentication.

      The way basic authentication works is that the client uses a URL with the following format:
      https://[username]:[password]@[full-URL]
      The username and password are validated against what is passed in.
      Parameters:
      request -
      response -
      relm - whatever the client may require or null
      userName -
      password -
      Returns:
      true means they passed authentication and false if they didn't
    • getBodyString

      protected static String getBodyString(javax.servlet.http.HttpServletRequest request) throws IOException
      Get the body of the request as a string.
      Parameters:
      request -
      Returns:
      Throws:
      IOException
    • getBodyJson

      protected static org.json.JSONObject getBodyJson(javax.servlet.http.HttpServletRequest request) throws IOException
      Get the body of the request as a JSON object. If it's not JSON, return NULL.
      Parameters:
      request -
      Returns:
      Throws:
      IOException
    • getBodyBinary

      protected static byte[] getBodyBinary(javax.servlet.http.HttpServletRequest request) throws IOException
      Get the body of the request as a binary array.
      Parameters:
      request -
      Returns:
      Throws:
      IOException
    • setResponse

      protected static void setResponse(javax.servlet.http.HttpServletResponse response, org.json.JSONObject json) throws IOException
      Set the response to a JSON object.
      Parameters:
      response -
      json -
      Throws:
      IOException
    • setResponse

      protected static void setResponse(javax.servlet.http.HttpServletResponse response, String responseType, String resp) throws IOException
      Set the response to a string.
      Parameters:
      response -
      responseType - like "text/plain", "text/html", "application/xml", etc.
      resp -
      Throws:
      IOException
    • setResponse

      protected static void setResponse(javax.servlet.http.HttpServletResponse response, String responseType, byte[] resp) throws IOException
      Set the response to a string.
      Parameters:
      response -
      responseType - like "application/octet-stream", "image/jpeg", "audio/mpeg", "video/mp4", etc.
      resp -
      Throws:
      IOException
    • isTextContentType

      public static boolean isTextContentType(String contentType)
      Determines if the given HTTP header ContentType is text. This check is not conclusive. It is merely likely.
      Parameters:
      contentType - the content type indicates the associated content is text
      Returns:
      true if the content type is text, false otherwise
    • isBinaryContentType

      public static boolean isBinaryContentType(String contentType)
      Determines if the given HTTP header ContentType is binary. This check is not conclusive. It is merely a best-guess.
      Parameters:
      contentType - the content type indicates the associated content is binary
      Returns:
      true if the content type is binary, false otherwise