Example usage for org.apache.hadoop.net NetUtils wrapException

List of usage examples for org.apache.hadoop.net NetUtils wrapException

Introduction

In this page you can find the example usage for org.apache.hadoop.net NetUtils wrapException.

Prototype

public static IOException wrapException(final String destHost, final int destPort, final String localHost,
        final int localPort, final IOException exception) 

Source Link

Document

Take an IOException , the local host port and remote host port details and return an IOException with the input exception as the cause and also include the host details.

Usage

From source file:org.apache.slider.core.restclient.UrlConnectionOperations.java

License:Apache License

public HttpOperationResponse execHttpOperation(HttpVerb verb, URL url, byte[] payload, String contentType)
        throws IOException, AuthenticationException {
    HttpURLConnection conn = null;
    HttpOperationResponse outcome = new HttpOperationResponse();
    int resultCode;
    byte[] body = null;
    log.debug("{} {} spnego={}", verb, url, useSpnego);

    boolean doOutput = verb.hasUploadBody();
    if (doOutput) {
        Preconditions.checkArgument(payload != null, "Null payload on a verb which expects one");
    }//  w  w w  .j a  v  a  2 s. c  o  m
    try {
        conn = openConnection(url);
        conn.setRequestMethod(verb.getVerb());
        conn.setDoOutput(doOutput);
        if (doOutput) {
            conn.setRequestProperty("Content-Type", contentType);
        }

        // now do the connection
        conn.connect();

        if (doOutput) {
            OutputStream output = conn.getOutputStream();
            IOUtils.write(payload, output);
            output.close();
        }

        resultCode = conn.getResponseCode();
        outcome.lastModified = conn.getLastModified();
        outcome.contentType = conn.getContentType();
        outcome.headers = conn.getHeaderFields();
        InputStream stream = conn.getErrorStream();
        if (stream == null) {
            stream = conn.getInputStream();
        }
        if (stream != null) {
            // read into a buffer.
            body = IOUtils.toByteArray(stream);
        } else {
            // no body: 
            log.debug("No body in response");

        }
    } catch (SSLException e) {
        throw e;
    } catch (IOException e) {
        throw NetUtils.wrapException(url.toString(), url.getPort(), "localhost", 0, e);

    } catch (AuthenticationException e) {
        throw new AuthenticationException("From " + url + ": " + e, e);

    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    uprateFaults(HttpVerb.GET, url.toString(), resultCode, "", body);
    outcome.responseCode = resultCode;
    outcome.data = body;
    return outcome;
}

From source file:org.apache.tajo.rpc.NettyServerBase.java

License:Apache License

public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Configuration conf,
        String rangeConf) throws IOException {
    try {//w  w w  . ja v a2  s.c  o  m
        IntegerRanges range = null;
        if (rangeConf != null) {
            range = conf.getRange(rangeConf, "");
        }
        if (range == null || range.isEmpty() || (address.getPort() != 0)) {
            socket.bind(address, backlog);
        } else {
            for (Integer port : range) {
                if (socket.isBound())
                    break;
                try {
                    InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port);
                    socket.bind(temp, backlog);
                } catch (BindException e) {
                    //Ignored
                }
            }
            if (!socket.isBound()) {
                throw new BindException("Could not find a free port in " + range);
            }
        }
    } catch (SocketException e) {
        throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e);
    }
}