Java Code Examples for io.vertx.core.net.SocketAddress#domainSocketAddress()

The following examples show how to use io.vertx.core.net.SocketAddress#domainSocketAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebClientExamples.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
public void testSocketAddress(WebClient client) {

    // Creates the unix domain socket address to access the Docker API
    SocketAddress serverAddress = SocketAddress
      .domainSocketAddress("/var/run/docker.sock");

    // We still need to specify host and port so the request
    // HTTP header will be localhost:8080
    // otherwise it will be a malformed HTTP request
    // the actual value does not matter much for this example
    client
      .request(
        HttpMethod.GET,
        serverAddress,
        8080,
        "localhost",
        "/images/json")
      .expect(ResponsePredicate.SC_ACCEPTED)
      .as(BodyCodec.jsonObject())
      .send()
      .onSuccess(res ->
        System.out.println("Current Docker images" + res.body()))
      .onFailure(err ->
        System.out.println("Something went wrong " + err.getMessage()));
  }
 
Example 2
Source File: DockerModuleHandle.java    From okapi with Apache License 2.0 5 votes vote down vote up
DockerModuleHandle(Vertx vertx, LaunchDescriptor desc,
                   String id, Ports ports, String containerHost, int port, JsonObject config) {
  this.hostPort = port;
  this.ports = ports;
  this.id = id;
  this.containerHost = containerHost;
  this.image = desc.getDockerImage();
  this.cmd = desc.getDockerCmd();
  this.env = desc.getEnv();
  this.dockerArgs = desc.getDockerArgs();
  this.client = vertx.createHttpClient();
  this.logBuffer = new StringBuilder();
  this.logSkip = 0;
  logger.info("Docker handler with native: {}", vertx.isNativeTransportEnabled());
  Boolean b = desc.getDockerPull();
  this.dockerPull = b == null || b.booleanValue();
  StringBuilder socketFile = new StringBuilder();
  this.dockerUrl = setupDockerAddress(socketFile,
      Config.getSysConf("dockerUrl", DEFAULT_DOCKER_URL, config));
  if (socketFile.length() > 0) {
    socketAddress = SocketAddress.domainSocketAddress(socketFile.toString());
  } else {
    socketAddress = null;
  }
  tcpPortWaiting = new TcpPortWaiting(vertx, containerHost, port);
  if (desc.getWaitIterations() != null) {
    tcpPortWaiting.setMaxIterations(desc.getWaitIterations());
  }
}
 
Example 3
Source File: DockerTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
private void checkDocker(Handler<AsyncResult<JsonArray>> future) {
  final SocketAddress socketAddress
    = SocketAddress.domainSocketAddress("/var/run/docker.sock");
  final String url = "http://localhost/images/json?all=1";
  HttpClientRequest req = HttpClientLegacy.requestAbs(client, HttpMethod.GET,
    socketAddress, url, res -> {
      Buffer body = Buffer.buffer();
      res.handler(d -> {
        body.appendBuffer(d);
      });
      res.endHandler(d -> {
        if (res.statusCode() == 200) {
          boolean gotIt = false;
          try {
            JsonArray ar = body.toJsonArray();
            future.handle(Future.succeededFuture(ar));
          } catch (Exception ex) {
            logger.warn(ex);
            future.handle(Future.failedFuture(ex));
          }
        } else {
          String m = "checkDocker HTTP error " + res.statusCode() + "\n"
            + body.toString();
          logger.error(m);
          future.handle(Future.failedFuture(m));
        }
      });
      res.exceptionHandler(d -> {
        logger.warn("exceptionHandler 2 " + d, d);
        future.handle(Future.failedFuture(d));
      });
    });
  req.exceptionHandler(d -> {
    logger.warn("exceptionHandler 1 " + d, d);
    future.handle(Future.failedFuture(d));
  });
  req.end();
}
 
Example 4
Source File: RedisURI.java    From vertx-redis-client with Apache License 2.0 4 votes vote down vote up
public RedisURI(String connectionString) {
  this.connectionString = connectionString;
  try {
    final URI uri = new URI(connectionString);

    final String host = uri.getHost() == null ? DEFAULT_HOST : uri.getHost();
    final int port = uri.getPort() == -1 ? DEFAULT_PORT : uri.getPort();
    final String path = (uri.getPath() == null || uri.getPath().isEmpty()) ? "/" : uri.getPath();

    // According to https://www.iana.org/assignments/uri-schemes/prov/redis there is no specified order of decision
    // in case if db number or password are given in 2 different ways (e.g. in path and query).
    // Implementation uses the query values as a fallback if another one is missing.
    Map<String, String> query = parseQuery(uri);
    switch (uri.getScheme()) {
      case "rediss":
        ssl = true;
        socketAddress = SocketAddress.inetSocketAddress(port, host);
        if (path.length() > 1) {
          // skip initial slash
          select = Integer.parseInt(uri.getPath().substring(1));
        } else if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      case "redis":
        ssl = false;
        socketAddress = SocketAddress.inetSocketAddress(port, host);
        if (path.length() > 1) {
          // skip initial slash
          select = Integer.parseInt(uri.getPath().substring(1));
        } else if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      case "unix":
        ssl = false;
        socketAddress = SocketAddress.domainSocketAddress(path);
        if (query.containsKey("db")) {
          select = Integer.parseInt(query.get("db"));
        } else {
          select = null;
        }
        break;
      default:
        throw new IllegalArgumentException("Unsupported Redis connection string scheme [" + uri.getScheme() + "]");
    }

    String userInfo = uri.getUserInfo();
    if (userInfo != null) {
      String[] userInfoArray = userInfo.split(":");
      if (userInfoArray.length > 0) {
        password = userInfoArray[userInfoArray.length - 1];
      } else {
        password = query.getOrDefault("password", null);
      }
    } else {
      password = query.getOrDefault("password", null);
    }

  } catch (URISyntaxException e) {
    throw new IllegalArgumentException("Failed to parse the connection string", e);
  }
}