Search in sources :

Example 36 with UnknownHostException

use of java.net.UnknownHostException in project springside4 by springside.

the class NetUtil method initLocalAddress.

/**
	 * 初始化本地地址
	 */
private static void initLocalAddress() {
    NetworkInterface nic = null;
    // 根据命令行执行hostname获得本机hostname, 与/etc/hosts 中该hostname的第一条ip配置,获得ip地址
    try {
        localAddress = InetAddress.getLocalHost();
        nic = NetworkInterface.getByInetAddress(localAddress);
    } catch (Exception ignored) {
    // NOSONAR
    }
    // 如果结果为空,或是一个loopback地址(127.0.0.1), 或是ipv6地址,再遍历网卡尝试获取
    if (localAddress == null || nic == null || localAddress.isLoopbackAddress() || localAddress instanceof Inet6Address) {
        InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface();
        // 仍然不符合要求,只好使用127.0.0.1
        try {
            localAddress = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException ignored) {
        // NOSONAR
        }
    }
    localHost = IPUtil.toString(localAddress);
    logger.info("localhost is {}", localHost);
}
Also used : UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 37 with UnknownHostException

use of java.net.UnknownHostException in project okhttp by square.

the class URLConnectionTest method urlWithSpaceInHost.

@Test
public void urlWithSpaceInHost() throws Exception {
    URLConnection urlConnection = urlFactory.open(new URL("http://and roid.com/"));
    try {
        urlConnection.getInputStream();
        fail();
    } catch (UnknownHostException expected) {
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 38 with UnknownHostException

use of java.net.UnknownHostException in project rest.li by linkedin.

the class AbstractNettyStreamClient method writeRequest.

private void writeRequest(final Request request, final RequestContext requestContext, final TimeoutTransportCallback<StreamResponse> callback) {
    State state = _state.get();
    if (state != State.RUNNING) {
        errorResponse(callback, new IllegalStateException("Client is " + state));
        return;
    }
    URI uri = request.getURI();
    String scheme = uri.getScheme();
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        errorResponse(callback, new IllegalArgumentException("Unknown scheme: " + scheme + " (only http/https is supported)"));
        return;
    }
    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        port = "http".equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
    }
    final SocketAddress address;
    try {
        // TODO investigate DNS resolution and timing
        InetAddress inetAddress = InetAddress.getByName(host);
        address = new InetSocketAddress(inetAddress, port);
        requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
    } catch (UnknownHostException e) {
        errorResponse(callback, e);
        return;
    }
    doWriteRequest(request, requestContext, address, callback);
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) InetAddress(java.net.InetAddress)

Example 39 with UnknownHostException

use of java.net.UnknownHostException in project rest.li by linkedin.

the class IPAddressSimpleCoercer method coerceOutput.

@Override
public InetAddress coerceOutput(Object object) throws TemplateOutputCastException {
    try {
        byte[] addressBytes;
        Class<?> objectType = object.getClass();
        if (objectType == String.class) {
            addressBytes = Data.stringToBytes((String) object, true);
        } else if (objectType == ByteString.class) {
            addressBytes = ((ByteString) object).copyBytes();
        } else {
            throw new TemplateOutputCastException("Invalid type");
        }
        return InetAddress.getByAddress(addressBytes);
    } catch (UnknownHostException e) {
        throw new TemplateOutputCastException("Invalid host", e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) TemplateOutputCastException(com.linkedin.data.template.TemplateOutputCastException)

Example 40 with UnknownHostException

use of java.net.UnknownHostException in project databus by linkedin.

the class BootstrapRequestProcessorBase method initBootstrapServerInfo.

private void initBootstrapServerInfo() {
    if (!_isServerInfoInitialized) {
        String host = null;
        try {
            host = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            LOG.error("Unable to fetch the local hostname !! Trying to fetch IP Address !!", e);
            try {
                host = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e1) {
                LOG.error("Unable to fetch the local IP Address too !! Giving up", e1);
                host = null;
            }
        }
        if (null != host) {
            int port = _bootstrapServer.getHttpPort();
            _serverHostPort = host + DbusConstants.HOSTPORT_DELIMITER + port;
            ServerInfo serverInfo = null;
            try {
                serverInfo = ServerInfo.buildServerInfoFromHostPort(_serverHostPort, DbusConstants.HOSTPORT_DELIMITER);
            } catch (Exception e) {
                LOG.error("Unable to build serverInfo from string (" + _serverHostPort + ")", e);
            }
            _serverInfo = serverInfo;
        } else {
            _serverHostPort = null;
            _serverInfo = null;
            LOG.error("Unable to fetch local address !! Clients connecting to this bootstrap server will restart bootstrap on failures !!");
        }
        _isServerInfoInitialized = true;
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) ServerInfo(com.linkedin.databus.client.pub.ServerInfo) Checkpoint(com.linkedin.databus.core.Checkpoint) SQLException(java.sql.SQLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException)

Aggregations

UnknownHostException (java.net.UnknownHostException)884 InetAddress (java.net.InetAddress)365 IOException (java.io.IOException)246 InetSocketAddress (java.net.InetSocketAddress)91 Test (org.junit.Test)79 SocketException (java.net.SocketException)70 ArrayList (java.util.ArrayList)66 Socket (java.net.Socket)49 SocketTimeoutException (java.net.SocketTimeoutException)44 URL (java.net.URL)40 File (java.io.File)39 MalformedURLException (java.net.MalformedURLException)35 HashMap (java.util.HashMap)35 ConnectException (java.net.ConnectException)28 HttpURLConnection (java.net.HttpURLConnection)28 NetworkInterface (java.net.NetworkInterface)26 Properties (java.util.Properties)26 Inet4Address (java.net.Inet4Address)24 URI (java.net.URI)24 InputStream (java.io.InputStream)23