Java HTTP Port Find waitForRespose(String name, String host, int port)

Here you can find the source of waitForRespose(String name, String host, int port)

Description

wait For Respose

License

Open Source License

Declaration

public static String waitForRespose(String name, String host, int port)
            throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2007 Red Hat, Inc. /*from   w w  w .j a v a2  s.  co m*/
 * Distributed under license by Red Hat, Inc. All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 ******************************************************************************/

import java.io.BufferedInputStream;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    private static final int RESPONSE_TIMEOUT = 10 * 1024;
    private static final long WAIT_TIMEOUT = 10 * 1024;

    public static String waitForRespose(String name, String host, int port)
            throws IOException {
        waitForResponseCode(200, name, host, port);
        return getResponse(name, host, port);
    }

    public static HttpURLConnection waitForResponseCode(int code,
            String name, String host, int port) throws IOException {
        URL url = new URL("http://" + host + ":" + port + "/" + name);
        long until = System.currentTimeMillis() + WAIT_TIMEOUT;
        while (System.currentTimeMillis() < until) {
            HttpURLConnection connection = connect(url);
            try {
                if (connection.getResponseCode() == code) {
                    return connection;
                }
            } catch (FileNotFoundException e) {
                if (code == 404) {
                    return connection;
                }
                throw e;
            }
        }
        throw new RuntimeException("wait on url " + url
                + " for response code " + code + " timed out.");
    }

    public static String getResponse(String name, String host, int port)
            throws IOException {
        URL url = new URL("http://" + host + ":" + port + "/" + name);
        HttpURLConnection connection = connect(url);
        return toString(new BufferedInputStream(connection.getInputStream()));
    }

    private static HttpURLConnection connect(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setAllowUserInteraction(false);
        connection.setConnectTimeout(RESPONSE_TIMEOUT);
        connection.setInstanceFollowRedirects(true);
        connection.setDoOutput(false);
        return connection;
    }

    public static String toString(InputStream in) throws IOException {
        StringWriter writer = new StringWriter();
        for (int data = -1; ((data = in.read()) != -1);) {
            writer.write(data);
        }
        return writer.toString();
    }
}

Related

  1. serverListening(String host, int port)
  2. serverListening(String hostName, int port)
  3. waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
  4. waitForPort(String host, int port)
  5. waitForResponseCode(int code, String name, String host, int port)
  6. waitForServerDown(String host, int port, long timeout)
  7. waitForService(int port)
  8. writeContactFile(int port, String appName)