public final class NetworkUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CONTENT_LENGTH |
static java.lang.String |
CONTENT_TYPE |
static java.lang.String |
LOCAL_HOST |
Modifier and Type | Method and Description |
---|---|
static Pair<java.net.InetSocketAddress,java.lang.Process> |
establishSSHTunnelIfNeeded(java.net.InetSocketAddress endpoint,
java.lang.String tunnelHost,
int timeout,
int retryCount,
int retryIntervalMs,
int verifyCount,
boolean isVerbose)
Tests if a network location is reachable.
|
static java.net.HttpURLConnection |
getConnection(java.lang.String endpoint) |
static java.lang.String |
getHostName() |
static java.net.InetSocketAddress |
getInetSocketAddress(java.lang.String endpoint)
Convert an endpoint from String (host:port) to InetSocketAddress
|
static boolean |
isLocationReachable(java.net.InetSocketAddress endpoint,
int timeout,
int retryCount,
int retryIntervalMs)
Tests if a network location is reachable.
|
static byte[] |
readHttpRequestBody(com.sun.net.httpserver.HttpExchange exchange)
Read the request body of HTTP request from a given HttpExchange
|
static byte[] |
readHttpResponse(java.net.HttpURLConnection connection)
Read http response from a given http connection
|
static boolean |
sendHttpGetRequest(java.net.HttpURLConnection connection) |
static boolean |
sendHttpPostRequest(java.net.HttpURLConnection connection,
byte[] data)
Send Http POST Request to a connection with given data in request body
|
static boolean |
sendHttpResponse(boolean isSuccess,
com.sun.net.httpserver.HttpExchange exchange,
byte[] response)
Send a http response with HTTP_OK return code and response body
|
static boolean |
sendHttpResponse(com.sun.net.httpserver.HttpExchange exchange,
byte[] response) |
public static final java.lang.String CONTENT_LENGTH
public static final java.lang.String CONTENT_TYPE
public static final java.lang.String LOCAL_HOST
public static byte[] readHttpRequestBody(com.sun.net.httpserver.HttpExchange exchange)
exchange
- the HttpExchange to read frompublic static boolean sendHttpResponse(boolean isSuccess, com.sun.net.httpserver.HttpExchange exchange, byte[] response)
isSuccess
- send back HTTP_OK if it is true, otherwise send back HTTP_UNAVAILABLEexchange
- the HttpExchange to send responseresponse
- the response the sent back in response bodypublic static boolean sendHttpResponse(com.sun.net.httpserver.HttpExchange exchange, byte[] response)
public static boolean sendHttpPostRequest(java.net.HttpURLConnection connection, byte[] data)
connection
- the connection to send post request todata
- the data to send in post request bodypublic static boolean sendHttpGetRequest(java.net.HttpURLConnection connection)
public static byte[] readHttpResponse(java.net.HttpURLConnection connection)
connection
- the connection to read responsepublic static java.net.HttpURLConnection getConnection(java.lang.String endpoint) throws java.io.IOException
java.io.IOException
public static java.lang.String getHostName()
public static boolean isLocationReachable(java.net.InetSocketAddress endpoint, int timeout, int retryCount, int retryIntervalMs)
endpoint
- the endpoint to connect totimeout
- Open connection will wait for this timeout in ms.retryCount
- In case of connection timeout try retry times.retryIntervalMs
- the interval in ms to retrypublic static Pair<java.net.InetSocketAddress,java.lang.Process> establishSSHTunnelIfNeeded(java.net.InetSocketAddress endpoint, java.lang.String tunnelHost, int timeout, int retryCount, int retryIntervalMs, int verifyCount, boolean isVerbose)
endpoint
- the endpoint to connect totimeout
- Open connection will wait for this timeout in ms.retryCount
- In case of connection timeout try retry times.retryIntervalMs
- the interval in ms to retrytunnelHost
- the host used to tunnelverifyCount
- In case of longer tunnel setup, try verify times to waitisVerbose
- prints verbose info or notpublic static java.net.InetSocketAddress getInetSocketAddress(java.lang.String endpoint)
endpoint
- a String in (host:port) format