Class ExchangeHelper


  • public class ExchangeHelper
    extends Object
    Simple class to wrap HttpServerExchange instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExchangeHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Optional<String> getOptionalQueryParam​(io.undertow.server.HttpServerExchange exchange, String name)
      Static helper that can be used to get an optional param from query for the specified name.
      static String getQueryParam​(io.undertow.server.HttpServerExchange exchange, String name)
      Static helper that can be used to get an mandatory param from query for the specified name.
      static com.fasterxml.jackson.databind.JsonNode readJsonRequest​(io.undertow.server.HttpServerExchange exchange)
      Static helper that can be used to read JSON object from HTTP-request payload.
      static <T> T readJsonRequest​(io.undertow.server.HttpServerExchange exchange, Class<T> type)
      Static helper that can be used to read JSON object from HTTP-request payload.
      static void sendJsonResponse​(io.undertow.server.HttpServerExchange exchange, Object response)
      Static helper that can be used to write a JSON object to HTTP-response with StatusCodes.OK.
      static void sendJsonResponseWithCode​(io.undertow.server.HttpServerExchange exchange, Object response, int statusCode)
      Static helper that can be used to write a JSON object to HTTP-response with the specified status code.
    • Constructor Detail

      • ExchangeHelper

        public ExchangeHelper()
    • Method Detail

      • getOptionalQueryParam

        public static Optional<String> getOptionalQueryParam​(io.undertow.server.HttpServerExchange exchange,
                                                             String name)
        Static helper that can be used to get an optional param from query for the specified name.
        Parameters:
        exchange - the HttpServerExchange instance.
        name - the query param name.
        Returns:
        an Optional.
      • getQueryParam

        public static String getQueryParam​(io.undertow.server.HttpServerExchange exchange,
                                           String name)
        Static helper that can be used to get an mandatory param from query for the specified name.
        Parameters:
        exchange - the HttpServerExchange instance.
        name - the query param name.
        Returns:
        a param as a string.
        Throws:
        InvalidHttpQueryParamException - if param is missing.
      • readJsonRequest

        public static com.fasterxml.jackson.databind.JsonNode readJsonRequest​(io.undertow.server.HttpServerExchange exchange)
                                                                       throws SerializationException
        Static helper that can be used to read JSON object from HTTP-request payload.
        Parameters:
        exchange - the HttpServerExchange instance.
        Returns:
        an JsonNode instance.
        Throws:
        SerializationException - if an error happens while de-serializing.
      • readJsonRequest

        public static <T> T readJsonRequest​(io.undertow.server.HttpServerExchange exchange,
                                            Class<T> type)
                                     throws SerializationException
        Static helper that can be used to read JSON object from HTTP-request payload.
        Parameters:
        exchange - the HttpServerExchange instance.
        type - the type.
        Returns:
        an JsonNode instance.
        Throws:
        SerializationException - if an error happens while de-serializing.
      • sendJsonResponse

        public static void sendJsonResponse​(io.undertow.server.HttpServerExchange exchange,
                                            Object response)
        Static helper that can be used to write a JSON object to HTTP-response with StatusCodes.OK.
        Parameters:
        exchange - the HttpServerExchange instance.
        response - the Object to serialize.
      • sendJsonResponseWithCode

        public static void sendJsonResponseWithCode​(io.undertow.server.HttpServerExchange exchange,
                                                    Object response,
                                                    int statusCode)
        Static helper that can be used to write a JSON object to HTTP-response with the specified status code.
        Parameters:
        exchange - the HttpServerExchange instance.
        response - the Object to serialize.
        statusCode - the status code.