Example usage for java.net BindException BindException

List of usage examples for java.net BindException BindException

Introduction

In this page you can find the example usage for java.net BindException BindException.

Prototype

public BindException() 

Source Link

Document

Construct a new BindException with no detailed message.

Usage

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java

/**
 * Samples the URL passed in and stores the result in
 * <code>HTTPSampleResult</code>, following redirects and downloading
 * page resources as appropriate./* w w  w .ja va  2  s  .com*/
 * <p>
 * When getting a redirect target, redirects are not followed and resources
 * are not downloaded. The caller will take care of this.
 *
 * @param url
 *            URL to sample
 * @param method
 *            HTTP method: GET, POST,...
 * @param areFollowingRedirect
 *            whether we're getting a redirect target
 * @param frameDepth
 *            Depth of this target in the frame structure. Used only to
 *            prevent infinite recursion.
 * @return results of the sampling
 */
@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    HttpURLConnection conn = null;

    String urlStr = url.toString();
    if (log.isDebugEnabled()) {
        log.debug("Start : sample " + urlStr);
        log.debug("method " + method + " followingRedirect " + areFollowingRedirect + " depth " + frameDepth);
    }

    HTTPSampleResult res = new HTTPSampleResult();
    res.setMonitor(isMonitor());

    res.setSampleLabel(urlStr);
    res.setURL(url);
    res.setHTTPMethod(method);

    res.sampleStart(); // Count the retries as well in the time

    // Check cache for an entry with an Expires header in the future
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) {
        if (cacheManager.inCache(url)) {
            return updateSampleResultForResourceInCache(res);
        }
    }

    try {
        // Sampling proper - establish the connection and read the response:
        // Repeatedly try to connect:
        int retry = -1;
        // Start with -1 so tries at least once, and retries at most MAX_CONN_RETRIES times
        for (; retry < MAX_CONN_RETRIES; retry++) {
            try {
                conn = setupConnection(url, method, res);
                // Attempt the connection:
                savedConn = conn;
                conn.connect();
                break;
            } catch (BindException e) {
                if (retry >= MAX_CONN_RETRIES) {
                    log.error("Can't connect after " + retry + " retries, " + e);
                    throw e;
                }
                log.debug("Bind exception, try again");
                if (conn != null) {
                    savedConn = null; // we don't want interrupt to try disconnection again
                    conn.disconnect();
                }
                setUseKeepAlive(false);
            } catch (IOException e) {
                log.debug("Connection failed, giving up");
                throw e;
            }
        }
        if (retry > MAX_CONN_RETRIES) {
            // This should never happen, but...
            throw new BindException();
        }
        // Nice, we've got a connection. Finish sending the request:
        if (method.equals(HTTPConstants.POST)) {
            String postBody = sendPostData(conn);
            res.setQueryString(postBody);
        } else if (method.equals(HTTPConstants.PUT)) {
            String putBody = sendPutData(conn);
            res.setQueryString(putBody);
        }
        // Request sent. Now get the response:
        byte[] responseData = readResponse(conn, res);

        res.sampleEnd();
        // Done with the sampling proper.

        // Now collect the results into the HTTPSampleResult:

        res.setResponseData(responseData);

        int errorLevel = conn.getResponseCode();
        String respMsg = conn.getResponseMessage();
        String hdr = conn.getHeaderField(0);
        if (hdr == null) {
            hdr = "(null)"; // $NON-NLS-1$
        }
        if (errorLevel == -1) {// Bug 38902 - sometimes -1 seems to be returned unnecessarily
            if (respMsg != null) {// Bug 41902 - NPE
                try {
                    errorLevel = Integer.parseInt(respMsg.substring(0, 3));
                    log.warn("ResponseCode==-1; parsed " + respMsg + " as " + errorLevel);
                } catch (NumberFormatException e) {
                    log.warn("ResponseCode==-1; could not parse " + respMsg + " hdr: " + hdr);
                }
            } else {
                respMsg = hdr; // for result
                log.warn("ResponseCode==-1 & null ResponseMessage. Header(0)= " + hdr);
            }
        }
        if (errorLevel == -1) {
            res.setResponseCode("(null)"); // $NON-NLS-1$
        } else {
            res.setResponseCode(Integer.toString(errorLevel));
        }
        res.setSuccessful(isSuccessCode(errorLevel));

        if (respMsg == null) {// has been seen in a redirect
            respMsg = hdr; // use header (if possible) if no message found
        }
        res.setResponseMessage(respMsg);

        String ct = conn.getContentType();
        if (ct != null) {
            res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
            res.setEncodingAndType(ct);
        }

        String responseHeaders = getResponseHeaders(conn);
        res.setResponseHeaders(responseHeaders);
        if (res.isRedirect()) {
            res.setRedirectLocation(conn.getHeaderField(HTTPConstants.HEADER_LOCATION));
        }

        // record headers size to allow HTTPSampleResult.getBytes() with different options
        res.setHeadersSize(responseHeaders.replaceAll("\n", "\r\n") // $NON-NLS-1$ $NON-NLS-2$
                .length() + 2); // add 2 for a '\r\n' at end of headers (before data) 
        if (log.isDebugEnabled()) {
            log.debug("Response headersSize=" + res.getHeadersSize() + " bodySize=" + res.getBodySize()
                    + " Total=" + (res.getHeadersSize() + res.getBodySize()));
        }

        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            res.setURL(conn.getURL());
        }

        // Store any cookies received in the cookie manager:
        saveConnectionCookies(conn, url, getCookieManager());

        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(conn, res);
        }

        res = resultProcessing(areFollowingRedirect, frameDepth, res);

        log.debug("End : sample");
        return res;
    } catch (IOException e) {
        res.sampleEnd();
        savedConn = null; // we don't want interrupt to try disconnection again
        // We don't want to continue using this connection, even if KeepAlive is set
        if (conn != null) { // May not exist
            conn.disconnect();
        }
        conn = null; // Don't process again
        return errorResult(e, res);
    } finally {
        // calling disconnect doesn't close the connection immediately,
        // but indicates we're through with it. The JVM should close
        // it when necessary.
        savedConn = null; // we don't want interrupt to try disconnection again
        disconnect(conn); // Disconnect unless using KeepAlive
    }
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImplClassifier.java

/**
 * Samples the URL passed in and stores the result in
 * <code>HTTPSampleResult</code>, following redirects and downloading page
 * resources as appropriate.//from   w ww . j  a va  2s.  c o  m
 * <p>
 * When getting a redirect target, redirects are not followed and resources
 * are not downloaded. The caller will take care of this.
 * 
 * @param url
 *            URL to sample
 * @param method
 *            HTTP method: GET, POST,...
 * @param areFollowingRedirect
 *            whether we're getting a redirect target
 * @param frameDepth
 *            Depth of this target in the frame structure. Used only to
 *            prevent infinite recursion.
 * @return results of the sampling
 */

protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    HttpURLConnection conn = null;

    String urlStr = url.toString();
    log.debug("Start : sample " + urlStr);

    HTTPSampleResult res = new HTTPSampleResult();
    res.setMonitor(isMonitor());

    res.setSampleLabel(urlStr);
    res.setURL(url);
    res.setHTTPMethod(method);

    res.sampleStart(); // Count the retries as well in the time

    // Check cache for an entry with an Expires header in the future
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) {
        if (cacheManager.inCache(url)) {
            res.sampleEnd();
            res.setResponseNoContent();
            res.setSuccessful(true);
            return res;
        }
    }

    try {
        // Sampling proper - establish the connection and read the response:
        // Repeatedly try to connect:
        int retry;
        // Start with 0 so tries at least once, and retries at most
        // MAX_CONN_RETRIES times
        for (retry = 0; retry <= MAX_CONN_RETRIES; retry++) {
            try {
                conn = setupConnection(url, method, res);
                // Attempt the connection:
                savedConn = conn;
                conn.connect();
                break;
            } catch (BindException e) {
                if (retry >= MAX_CONN_RETRIES) {
                    log.error("Can't connect after " + retry + " retries, " + e);
                    throw e;
                }
                log.debug("Bind exception, try again");
                if (conn != null) {
                    savedConn = null; // we don't want interrupt to try
                                      // disconnection again
                    conn.disconnect();
                }
                setUseKeepAlive(false);
                continue; // try again
            } catch (IOException e) {
                log.debug("Connection failed, giving up");
                throw e;
            }
        }
        if (retry > MAX_CONN_RETRIES) {
            // This should never happen, but...
            throw new BindException();
        }
        // Nice, we've got a connection. Finish sending the request:
        if (method.equals(HTTPConstants.POST)) {
            String postBody = sendPostData(conn);
            res.setQueryString(postBody);
        } else if (method.equals(HTTPConstants.PUT)) {
            String putBody = sendPutData(conn);
            res.setQueryString(putBody);
        }
        // Request sent. Now get the response:
        byte[] responseData = readResponse(conn, res);

        res.sampleEnd();
        // Done with the sampling proper.

        // Now collect the results into the HTTPSampleResult:

        res.setResponseData(responseData);

        @SuppressWarnings("null")
        // Cannot be null here
        int errorLevel = conn.getResponseCode();
        String respMsg = conn.getResponseMessage();
        String hdr = conn.getHeaderField(0);
        if (hdr == null) {
            hdr = "(null)"; // $NON-NLS-1$
        }
        if (errorLevel == -1) {// Bug 38902 - sometimes -1 seems to be
            // returned unnecessarily
            if (respMsg != null) {// Bug 41902 - NPE
                try {
                    errorLevel = Integer.parseInt(respMsg.substring(0, 3));
                    log.warn("ResponseCode==-1; parsed " + respMsg + " as " + errorLevel);
                } catch (NumberFormatException e) {
                    log.warn("ResponseCode==-1; could not parse " + respMsg + " hdr: " + hdr);
                }
            } else {
                respMsg = hdr; // for result
                log.warn("ResponseCode==-1 & null ResponseMessage. Header(0)= " + hdr);
            }
        }
        if (errorLevel == -1) {
            res.setResponseCode("(null)"); // $NON-NLS-1$
        } else {
            res.setResponseCode(Integer.toString(errorLevel));
        }
        res.setSuccessful(isSuccessCode(errorLevel));

        if (respMsg == null) {// has been seen in a redirect
            respMsg = hdr; // use header (if possible) if no message found
        }
        res.setResponseMessage(respMsg);

        String ct = conn.getContentType();
        if (ct != null) {
            res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
            res.setEncodingAndType(ct);
        }

        String responseHeaders = getResponseHeaders(conn);
        res.setResponseHeaders(responseHeaders);
        if (res.isRedirect()) {
            res.setRedirectLocation(conn.getHeaderField(HTTPConstants.HEADER_LOCATION));
        }

        // record headers size to allow HTTPSampleResult.getBytes() with
        // different options
        res.setHeadersSize(responseHeaders.replaceAll("\n", "\r\n") // $NON-NLS-1$
                // $NON-NLS-2$
                .length() + 2); // add 2 for a '\r\n' at end of headers
        // (before data)
        if (log.isDebugEnabled()) {
            log.debug("Response headersSize=" + res.getHeadersSize() + " bodySize=" + res.getBodySize()
                    + " Total=" + (res.getHeadersSize() + res.getBodySize()));
        }

        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            res.setURL(conn.getURL());
        }

        // Store any cookies received in the cookie manager:
        saveConnectionCookies(conn, url, getCookieManager());

        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(conn, res);
        }

        res = resultProcessing(areFollowingRedirect, frameDepth, res);

        log.debug("End : sample");
        return res;
    } catch (IOException e) {
        res.sampleEnd();
        savedConn = null; // we don't want interrupt to try disconnection
                          // again
                          // We don't want to continue using this connection, even if
                          // KeepAlive is set
        if (conn != null) { // May not exist
            conn.disconnect();
        }
        conn = null; // Don't process again
        return errorResult(e, res);
    } finally {
        // calling disconnect doesn't close the connection immediately,
        // but indicates we're through with it. The JVM should close
        // it when necessary.
        savedConn = null; // we don't want interrupt to try disconnection
                          // again
        disconnect(conn); // Disconnect unless using KeepAlive
    }
}