Class NetworkUtils


  • public final class NetworkUtils
    extends Object
    Utilities related to network.
    • Method Detail

      • readHttpRequestBody

        public static byte[] readHttpRequestBody​(HttpExchange exchange)
        Read the request body of HTTP request from a given HttpExchange
        Parameters:
        exchange - the HttpExchange to read from
        Returns:
        the byte[] in request body, or new byte[0] if failed to read
      • sendHttpResponse

        public static boolean sendHttpResponse​(boolean isSuccess,
                                               HttpExchange exchange,
                                               byte[] response)
        Send a http response with HTTP_OK return code and response body
        Parameters:
        isSuccess - send back HTTP_OK if it is true, otherwise send back HTTP_UNAVAILABLE
        exchange - the HttpExchange to send response
        response - the response the sent back in response body
        Returns:
        true if we send the response successfully
      • sendHttpResponse

        public static boolean sendHttpResponse​(HttpExchange exchange,
                                               byte[] response)
      • sendHttpPostRequest

        public static boolean sendHttpPostRequest​(HttpURLConnection connection,
                                                  String contentType,
                                                  byte[] data)
        Send Http POST Request to a connection with given data in request body
        Parameters:
        connection - the connection to send post request to
        contentType - the type of the content to be sent
        data - the data to send in post request body
        Returns:
        true if success
      • sendHttpGetRequest

        public static boolean sendHttpGetRequest​(HttpURLConnection connection)
      • sendHttpDeleteRequest

        public static boolean sendHttpDeleteRequest​(HttpURLConnection connection)
      • readHttpResponse

        public static byte[] readHttpResponse​(HttpURLConnection connection)
        Read http response from a given http connection
        Parameters:
        connection - the connection to read response
        Returns:
        the byte[] in response body, or new byte[0] if failed to read
      • checkHttpResponseCode

        public static boolean checkHttpResponseCode​(HttpURLConnection connection,
                                                    int expectedCode)
      • getHostName

        public static String getHostName()
      • isLocationReachable

        public static boolean isLocationReachable​(InetSocketAddress endpoint,
                                                  Duration timeout,
                                                  int retryCount,
                                                  Duration retryInterval)
        Tests if a network location is reachable. This is best effort and may give false not reachable.
        Parameters:
        endpoint - the endpoint to connect to
        timeout - Open connection will wait for this timeout.
        retryCount - In case of connection timeout try retryCount times.
        retryInterval - the interval to retryCount
        Returns:
        true if the network location is reachable
      • getInetSocketAddress

        public static InetSocketAddress getInetSocketAddress​(String endpoint)
        Convert an endpoint from String (host:port) to InetSocketAddress
        Parameters:
        endpoint - a String in (host:port) format
        Returns:
        an InetSocketAddress representing the endpoint