Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

intsafePort(final String port)
Validate the given port is in a valid range, returning 0 if it is invalid.
try {
    if ("*".equals(port)) {
        return randomPort();
    final int number = Integer.parseInt(port);
    if (number < 0 || number > 65535) {
        return 0;
    } else {
...
intsearchPort(int number, int even, boolean stream)
Searches for a number of successive free ports in the range between minPort and maxPort
boolean found = false;
int offset = 0;
int port = minPort;
while (!found) {
    switch (even) {
    case 2:
        port = minPort + 2 * offset;
        break;
...
intselectAvailablePort(int preferedPort)
select Available Port
int port = -1;
ServerSocket ss = null;
try {
    ss = new ServerSocket();
    ss.bind(new InetSocketAddress(preferedPort));
    port = ss.getLocalPort();
} catch (IOException e) {
    try {
...
StringsendCommand(final String command, final int monitorPort)
send Command
try (Socket socket = new Socket("127.0.0.1", monitorPort);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
    writer.write(command);
    writer.newLine();
    writer.flush();
    return reader.readLine();
voidsendCommand(String host, Integer port, String cmd)
send Command
Socket socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
out.write(cmd.getBytes());
out.close();
socket.close();
booleanserverIsUp(int port)
server Is Up
try {
    URL url = new URL("http", "localhost", port, "/");
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.connect();
    OutputStream outputStream = httpURLConnection.getOutputStream();
...
booleanServerListening(String host, int port)
The method check if a host is listening in the specified port.
try (Socket s = new Socket(host, port);) {
    return true;
} catch (Exception e) {
    return false;
booleanserverListening(String host, int port)
server Listening
Socket s = null;
try {
    s = new Socket(host, port);
    return true;
} catch (Exception e) {
    return false;
} finally {
    if (s != null) {
...
booleanserverListening(String hostName, int port)
Checks if there is a server socket listening in the given host and port.
try {
    return serverListening(InetAddress.getByName(hostName), port);
} catch (UnknownHostException e) {
    return false;
voidwaitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
wait For Live Server
InetSocketAddress address = new InetSocketAddress(webContainerHostname, webContainerPort);
System.out.println("Waiting " + timeoutMin + " min for web server...");
long beginTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis() + (timeoutMin * 60 * 1000);
boolean portOpened = false;
while ((!portOpened) && (System.currentTimeMillis() < endTime)) {
    portOpened = trySocket(address);
    if (portOpened) {
...