Package org.apache.heron.spi.utils
Class NetworkUtils
- java.lang.Object
-
- org.apache.heron.spi.utils.NetworkUtils
-
public final class NetworkUtils extends Object
Utilities related to network.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NetworkUtils.HeronSystem
static class
NetworkUtils.TunnelConfig
static class
NetworkUtils.TunnelType
-
Field Summary
Fields Modifier and Type Field Description static String
JSON_TYPE
static String
URL_ENCODE_TYPE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
checkHttpResponseCode(HttpURLConnection connection, int expectedCode)
static org.apache.heron.common.basics.Pair<InetSocketAddress,Process>
establishSSHTunnelIfNeeded(InetSocketAddress endpoint, NetworkUtils.TunnelConfig tunnelConfig, NetworkUtils.TunnelType tunnelType)
static String
getHostName()
static HttpURLConnection
getHttpConnection(String endpoint)
static InetSocketAddress
getInetSocketAddress(String endpoint)
Convert an endpoint from String (host:port) to InetSocketAddressstatic HttpURLConnection
getProxiedHttpConnectionIfNeeded(URL endpoint, NetworkUtils.TunnelConfig tunnelConfig)
static boolean
isLocationReachable(InetSocketAddress endpoint, Duration timeout, int retryCount, Duration retryInterval)
Tests if a network location is reachable.static byte[]
readHttpRequestBody(HttpExchange exchange)
Read the request body of HTTP request from a given HttpExchangestatic byte[]
readHttpResponse(HttpURLConnection connection)
Read http response from a given http connectionstatic boolean
sendHttpDeleteRequest(HttpURLConnection connection)
static boolean
sendHttpGetRequest(HttpURLConnection connection)
static boolean
sendHttpPostRequest(HttpURLConnection connection, String contentType, byte[] data)
Send Http POST Request to a connection with given data in request bodystatic boolean
sendHttpResponse(boolean isSuccess, HttpExchange exchange, byte[] response)
Send a http response with HTTP_OK return code and response bodystatic boolean
sendHttpResponse(HttpExchange exchange, byte[] response)
-
-
-
Field Detail
-
JSON_TYPE
public static final String JSON_TYPE
- See Also:
- Constant Field Values
-
URL_ENCODE_TYPE
public static final String URL_ENCODE_TYPE
- See Also:
- Constant Field Values
-
-
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_UNAVAILABLEexchange
- the HttpExchange to send responseresponse
- 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 tocontentType
- the type of the content to be sentdata
- 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
-
getHttpConnection
public static HttpURLConnection getHttpConnection(String endpoint)
-
getProxiedHttpConnectionIfNeeded
public static HttpURLConnection getProxiedHttpConnectionIfNeeded(URL endpoint, NetworkUtils.TunnelConfig tunnelConfig)
-
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 totimeout
- 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
-
establishSSHTunnelIfNeeded
public static org.apache.heron.common.basics.Pair<InetSocketAddress,Process> establishSSHTunnelIfNeeded(InetSocketAddress endpoint, NetworkUtils.TunnelConfig tunnelConfig, NetworkUtils.TunnelType tunnelType)
-
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
-
-