Klasse PathHandler

java.lang.Object
de.srsoftware.tools.PathHandler
Alle implementierten Schnittstellen:
HttpHandler

public abstract class PathHandler extends Object implements HttpHandler
implementation of HttpHandler that attaches to a given path
  • Felddetails

  • Konstruktordetails

    • PathHandler

      public PathHandler()
      Creates a new instance
  • Methodendetails

    • allowOrigin

      public static HttpExchange allowOrigin(HttpExchange ex, String origin)
      this method sets the 'Access-Control-Allow-Origin' header value
      Parameter:
      ex - the HttpExchange, whose response headers are to be modified
      origin - the value for the header
      Gibt zurück:
      the HttpExchange
    • allowCredentials

      public static HttpExchange allowCredentials(HttpExchange ex)
      this method sets the 'Access-Control-Allow-Credentials' header value to true
      Parameter:
      ex - the HttpExchange, whose response headers are to be modified
      Gibt zurück:
      the HttpExchange
    • badRequest

      public boolean badRequest(HttpExchange ex, byte[] bytes) throws IOException
      create a response with status code 400, send payload
      Parameter:
      ex - the HttpExchange to write to
      bytes - the payload
      Gibt zurück:
      true – result is only created to allow return badRequest(…)
      Löst aus:
      IOException - if writing to the HttpEchange object fails
    • badRequest

      public boolean badRequest(HttpExchange ex, Object o) throws IOException
      create a response with status code 400, send payload
      Parameter:
      ex - the HttpExchange to write to
      o - the payload
      Gibt zurück:
      true – result is only created to allow return badRequest(…)
      Löst aus:
      IOException - if writing to the HttpEchange object fails
    • bindPath

      public PathHandler.Bond bindPath(String... path)
      Bind the PathHandler object to one/several paths
      Parameter:
      path - the paths to bind to
      Gibt zurück:
      a bond, that can be used to create a context on a HttpServer
    • doDelete

      public boolean doDelete(Path path, HttpExchange ex) throws IOException
      "not found" default implementation
      Parameter:
      path - ignored
      ex - HttpExchange used to return the not-implemented notification
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • doGet

      public boolean doGet(Path path, HttpExchange ex) throws IOException
      "not found" default implementation
      Parameter:
      path - ignored
      ex - HttpExchange used to return the not-implemented notification
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • doOptions

      public boolean doOptions(Path path, HttpExchange ex) throws IOException
      "not found" default implementation
      Parameter:
      path - ignored
      ex - HttpExchange used to return the not-implemented notification
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • doPatch

      public boolean doPatch(Path path, HttpExchange ex) throws IOException
      "not found" default implementation
      Parameter:
      path - ignored
      ex - HttpExchange used to return the not-implemented notification
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • doPost

      public boolean doPost(Path path, HttpExchange ex) throws IOException
      "not found" default implementation
      Parameter:
      path - ignored
      ex - HttpExchange used to return the not-implemented notification
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • handle

      public void handle(HttpExchange ex) throws IOException
      Angegeben von:
      handle in Schnittstelle HttpHandler
      Löst aus:
      IOException
    • handleMethod

      public boolean handleMethod(String method, Path path, HttpExchange ex) throws IOException
      Default implementation, defaulting to notFound(ex). You can override this method to implement own httpRequest method handlers
      Parameter:
      method - method from the request header
      path - path from the request header
      ex - http exchange to process
      Gibt zurück:
      a boolean that is ignored
      Löst aus:
      IOException - if the transmission fails
    • relativePath

      public Path relativePath(HttpExchange ex)
      convert the request URI to a path object
      Parameter:
      ex - the http exchange to process
      Gibt zurück:
      the path object
    • body

      public static String body(HttpExchange ex) throws IOException
      extracts the body of an HttpExchange
      Parameter:
      ex - the exchange to process
      Gibt zurück:
      the content of the HttpExchange
      Löst aus:
      IOException - if reading the body failed
    • contentType

      public static Optional<String> contentType(HttpExchange ex)
      extracts the request content type of a given HttpExchange
      Parameter:
      ex - the exchange whose content type is to be investigated
      Gibt zurück:
      an Optional containing the content type or empty if no content type was submitted
    • formData

      public static Map<String,Object> formData(HttpExchange ex) throws IOException
      fetches data from an form-urlencoded message body
      Parameter:
      ex - the exchange whose content type is to be evaluated
      Gibt zurück:
      a data map representing the form data
      Löst aus:
      IOException - if the data cannot be read
    • getAuthToken

      public static Optional<String> getAuthToken(HttpExchange ex)
      extract the value of an Authorization header, if present
      Parameter:
      ex - the HttpExchange to extract from
      Gibt zurück:
      an optional, carrying the first value of an Authorization header, of present. empty, otherwise.
    • getBasicAuth

      public static Optional<PathHandler.BasicAuth> getBasicAuth(HttpExchange ex)
      retrieve and decode a basic auth header
      Parameter:
      ex - the HttpExchange to process
      Gibt zurück:
      an optional containing the basic auth data, of such a header is present. empty otherwise.
    • getBearer

      public static Optional<String> getBearer(HttpExchange ex)
      try to extract a bearer token from the headers of the HttpExchange
      Parameter:
      ex - the HttpExchange to process
      Gibt zurück:
      an optional carrying the token without the 'Bearer' prefix or empty, if no such token is given
    • getHeader

      public static Optional<String> getHeader(HttpExchange ex, String key)
      get the value of a specific header field
      Parameter:
      ex - the HttpExchange to extract from
      key - the key to search for
      Gibt zurück:
      an Optional carrying the value belonging to the key, or empty, if no value is set
    • hostname

      public static String hostname(HttpExchange ex)
      get the hostname requested by the client from the host header or – if set – the hostname submitted in the x-forwarded-host header
      Parameter:
      ex - the HttpExchange to process
      Gibt zurück:
      the host url, e.g. https://example.com
    • json

      public static org.json.JSONObject json(HttpExchange ex) throws IOException
      Try to parse the body of this HttpExchange as JSON object
      Parameter:
      ex - the HttpExchange
      Gibt zurück:
      a json object build from the contents of this exchange
      Löst aus:
      IOException - if anything bad happens
    • languages

      public static List<String> languages(HttpExchange ex)
      request the Accept-Language header
      Parameter:
      ex - the HttpExchange to prompt
      Gibt zurück:
      the set of languages passed via header
    • notFound

      public boolean notFound(HttpExchange ex) throws IOException
      sends a "not found" response
      Parameter:
      ex - the HttpExchange to act upon
      Gibt zurück:
      false
      Löst aus:
      IOException - if sending the response fails
    • queryParam

      public static Map<String,Object> queryParam(HttpExchange ex)
      map the query from the Request URI to a map
      Parameter:
      ex - the HttpExchange to read the entries from
      Gibt zurück:
      the query parameters as key → value map
    • sendEmptyResponse

      public static boolean sendEmptyResponse(int statusCode, HttpExchange ex) throws IOException
      send a response without body
      Parameter:
      statusCode - send this status code
      ex - the HttpExchange to act on
      Gibt zurück:
      false
      Löst aus:
      IOException - if the response cannot be sent
    • sendRedirect

      public static boolean sendRedirect(HttpExchange ex, String url) throws IOException
      send a redirect to the client
      Parameter:
      ex - the HttpExchange to use
      url - the targeted location
      Gibt zurück:
      false
      Löst aus:
      IOException - if the anser cannot be sent
    • sendContent

      public boolean sendContent(HttpExchange ex, int status, byte[] bytes) throws IOException
      create a response given status code, send payload
      Parameter:
      ex - the HttpExchange to write to
      status - the status code
      bytes - the payload
      Gibt zurück:
      true – result is only created to allow return badRequest(…)
      Löst aus:
      IOException - if writing to the HttpEchange object fails
    • sendContent

      public boolean sendContent(HttpExchange ex, int status, Object o) throws IOException
      create a response given status code, send payload
      Parameter:
      ex - the HttpExchange to write to
      status - the status code
      o - the payload
      Gibt zurück:
      true – result is only created to allow return badRequest(…)
      Löst aus:
      IOException - if writing to the HttpEchange object fails
    • sendContent

      public boolean sendContent(HttpExchange ex, byte[] bytes) throws IOException
      send an array of bytes
      Parameter:
      ex - the http exchange to respond to
      bytes - the payload to send
      Gibt zurück:
      a boolean that is ignored
      Löst aus:
      IOException - if writing to the HttpExchange object fails
    • sendContent

      public boolean sendContent(HttpExchange ex, Object o) throws IOException
      send an object, thereby trying to guess the correct mime and transmission type.
      Parameter:
      ex - the http exchange to respond to
      o - the payload to send
      Gibt zurück:
      a boolean that is ignored
      Löst aus:
      IOException - if writing to the HttpExchange object fails
    • serverError

      public boolean serverError(HttpExchange ex, Object o) throws IOException
      create a "internal server error" response
      Parameter:
      ex - the HttpExchange object
      o - an additional payload
      Gibt zurück:
      false
      Löst aus:
      IOException - if the content cannot be sent
    • url

      public static String url(HttpExchange ex)
      recover the URL from an HttpExchange object
      Parameter:
      ex - the HttpExchange object
      Gibt zurück:
      the url of this object