use of java.net.UnknownHostException in project vert.x by eclipse.
the class HostnameResolutionTest method testMultipleSearchDomain.
@Test
public void testMultipleSearchDomain() throws Exception {
Map<String, String> records = new HashMap<>();
records.put("host1.foo.com", "127.0.0.1");
records.put("host2.bar.com", "127.0.0.2");
records.put("host3.bar.com", "127.0.0.3");
records.put("host3.foo.com", "127.0.0.4");
dnsServer.stop();
dnsServer = FakeDNSServer.testResolveA(records);
dnsServer.start();
VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").addSearchDomain("bar.com")));
// "host1" resolves via the "foo.com" search path
CountDownLatch latch1 = new CountDownLatch(1);
vertx.resolveAddress("host1", onSuccess(resolved -> {
assertEquals("127.0.0.1", resolved.getHostAddress());
latch1.countDown();
}));
awaitLatch(latch1);
// "host2" resolves via the "bar.com" search path
CountDownLatch latch2 = new CountDownLatch(1);
vertx.resolveAddress("host2", onSuccess(resolved -> {
assertEquals("127.0.0.2", resolved.getHostAddress());
latch2.countDown();
}));
awaitLatch(latch2);
// "host3" resolves via the the "foo.com" search path as it is the first one
CountDownLatch latch3 = new CountDownLatch(1);
vertx.resolveAddress("host3", onSuccess(resolved -> {
assertEquals("127.0.0.4", resolved.getHostAddress());
latch3.countDown();
}));
awaitLatch(latch3);
// "host4" does not resolve
vertx.resolveAddress("host4", onFailure(cause -> {
assertTrue(cause instanceof UnknownHostException);
testComplete();
}));
await();
}
use of java.net.UnknownHostException in project vert.x by eclipse.
the class HttpProxy method start.
/**
* Start the server.
*
* @param vertx
* Vertx instance to use for creating the server and client
* @param finishedHandler
* will be called when the server has started
*/
@Override
public void start(Vertx vertx, Handler<Void> finishedHandler) {
HttpServerOptions options = new HttpServerOptions();
options.setHost("localhost").setPort(PORT);
server = vertx.createHttpServer(options);
server.requestHandler(request -> {
HttpMethod method = request.method();
String uri = request.uri();
if (username != null) {
String auth = request.getHeader("Proxy-Authorization");
String expected = "Basic " + Base64.getEncoder().encodeToString((username + ":" + username).getBytes());
if (auth == null || !auth.equals(expected)) {
request.response().setStatusCode(407).end("proxy authentication failed");
return;
}
}
lastRequestHeaders = MultiMap.caseInsensitiveMultiMap().addAll(request.headers());
if (error != 0) {
request.response().setStatusCode(error).end("proxy request failed");
} else if (method == HttpMethod.CONNECT) {
if (!uri.contains(":")) {
request.response().setStatusCode(403).end("invalid request");
} else {
lastUri = uri;
if (forceUri != null) {
uri = forceUri;
}
String[] split = uri.split(":");
String host = split[0];
int port;
try {
port = Integer.parseInt(split[1]);
} catch (NumberFormatException ex) {
port = 443;
}
if (port == 8080 || port < 1024 && port != 443) {
request.response().setStatusCode(403).end("access to port denied");
return;
}
NetSocket serverSocket = request.netSocket();
NetClientOptions netOptions = new NetClientOptions();
NetClient netClient = vertx.createNetClient(netOptions);
netClient.connect(port, host, result -> {
if (result.succeeded()) {
NetSocket clientSocket = result.result();
serverSocket.write("HTTP/1.0 200 Connection established\n\n");
serverSocket.closeHandler(v -> clientSocket.close());
clientSocket.closeHandler(v -> serverSocket.close());
Pump.pump(serverSocket, clientSocket).start();
Pump.pump(clientSocket, serverSocket).start();
} else {
request.response().setStatusCode(403).end("request failed");
}
});
}
} else if (method == HttpMethod.GET) {
lastUri = request.uri();
HttpClient client = vertx.createHttpClient();
HttpClientRequest clientRequest = client.getAbs(request.uri(), resp -> {
for (String name : resp.headers().names()) {
request.response().putHeader(name, resp.headers().getAll(name));
}
resp.bodyHandler(body -> {
request.response().end(body);
});
});
for (String name : request.headers().names()) {
if (!name.equals("Proxy-Authorization")) {
clientRequest.putHeader(name, request.headers().getAll(name));
}
}
clientRequest.exceptionHandler(e -> {
log.debug("exception", e);
int status;
if (e instanceof UnknownHostException) {
status = 504;
} else {
status = 400;
}
request.response().setStatusCode(status).end(e.toString() + " on client request");
});
clientRequest.end();
} else {
request.response().setStatusCode(405).end("method not supported");
}
});
server.listen(server -> {
finishedHandler.handle(null);
});
}
use of java.net.UnknownHostException in project elasticsearch by elastic.
the class ZenDiscoveryIT method testHandleNodeJoin_incompatibleClusterState.
public void testHandleNodeJoin_incompatibleClusterState() throws UnknownHostException {
Settings nodeSettings = Settings.builder().put("discovery.type", // <-- To override the local setting if set externally
"zen").build();
String masterOnlyNode = internalCluster().startMasterOnlyNode(nodeSettings);
String node1 = internalCluster().startNode(nodeSettings);
ZenDiscovery zenDiscovery = (ZenDiscovery) internalCluster().getInstance(Discovery.class, masterOnlyNode);
ClusterService clusterService = internalCluster().getInstance(ClusterService.class, node1);
final ClusterState state = clusterService.state();
MetaData.Builder mdBuilder = MetaData.builder(state.metaData());
mdBuilder.putCustom(CustomMetaData.TYPE, new CustomMetaData("data"));
ClusterState stateWithCustomMetaData = ClusterState.builder(state).metaData(mdBuilder).build();
final AtomicReference<IllegalStateException> holder = new AtomicReference<>();
DiscoveryNode node = state.nodes().getLocalNode();
zenDiscovery.handleJoinRequest(node, stateWithCustomMetaData, new MembershipAction.JoinCallback() {
@Override
public void onSuccess() {
}
@Override
public void onFailure(Exception e) {
holder.set((IllegalStateException) e);
}
});
assertThat(holder.get(), notNullValue());
assertThat(holder.get().getMessage(), equalTo("failure when sending a validation request to node"));
}
use of java.net.UnknownHostException in project elasticsearch by elastic.
the class UnicastZenPingTests method testUnknownHost.
public void testUnknownHost() throws InterruptedException {
final Logger logger = mock(Logger.class);
final NetworkService networkService = new NetworkService(Settings.EMPTY, Collections.emptyList());
final String hostname = randomAsciiOfLength(8);
final UnknownHostException unknownHostException = new UnknownHostException(hostname);
final Transport transport = new MockTcpTransport(Settings.EMPTY, threadPool, BigArrays.NON_RECYCLING_INSTANCE, new NoneCircuitBreakerService(), new NamedWriteableRegistry(Collections.emptyList()), networkService, Version.CURRENT) {
@Override
public BoundTransportAddress boundAddress() {
return new BoundTransportAddress(new TransportAddress[] { new TransportAddress(InetAddress.getLoopbackAddress(), 9300) }, new TransportAddress(InetAddress.getLoopbackAddress(), 9300));
}
@Override
public TransportAddress[] addressesFromString(String address, int perAddressLimit) throws UnknownHostException {
throw unknownHostException;
}
};
closeables.push(transport);
final TransportService transportService = new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null);
closeables.push(transportService);
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists(executorService, logger, Arrays.asList(hostname), 1, transportService, "test_", TimeValue.timeValueSeconds(1));
assertThat(discoveryNodes, empty());
verify(logger).warn("failed to resolve host [" + hostname + "]", unknownHostException);
}
use of java.net.UnknownHostException in project che by eclipse.
the class JavaDebugger method connect.
/**
* Attach to a JVM that is already running at specified host.
*
* @throws DebuggerException
* when connection to Java VM is not established
*/
private void connect() throws DebuggerException {
final String connectorName = "com.sun.jdi.SocketAttach";
AttachingConnector connector = connector(connectorName);
if (connector == null) {
throw new DebuggerException(String.format("Unable connect to target Java VM. Requested connector '%s' not found. ", connectorName));
}
Map<String, Connector.Argument> arguments = connector.defaultArguments();
arguments.get("hostname").setValue(host);
((Connector.IntegerArgument) arguments.get("port")).setValue(port);
int attempt = 0;
for (; ; ) {
try {
Thread.sleep(2000);
vm = connector.attach(arguments);
vm.suspend();
break;
} catch (UnknownHostException | IllegalConnectorArgumentsException e) {
throw new DebuggerException(e.getMessage(), e);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
if (++attempt > 10) {
throw new DebuggerException(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
} catch (InterruptedException ignored) {
}
}
eventsCollector = new EventsCollector(vm.eventQueue(), this);
LOG.debug("Connect {}:{}", host, port);
}
Aggregations