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);
}
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) {
}
}
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);
}
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);
}
}
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;
}
}
Aggregations