@Override public void serviceAdded(ServiceConfig cfg) { if (armeriaServer != null) { if (armeriaServer != cfg.server()) { throw new IllegalStateException("cannot be added to more than one server"); } else { return; } } armeriaServer = cfg.server(); armeriaServer.addListener(configurator); if (hostname == null) { hostname = armeriaServer.defaultHostname(); } }
@Override public void serverStarted(Server server) throws Exception { if (endpoint == null) { assert server.activePort().isPresent(); endpoint = Endpoint.of(server.defaultHostname(), server.activePort().get() .localAddress().getPort()); } client.start(); final String key = endpoint.host() + '_' + endpoint.port(); final byte[] value = nodeValueCodec.encode(endpoint); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(zNodePath + '/' + key, value); }
@Override public void serviceAdded(ServiceConfig cfg) throws Exception { if (hostName == null) { hostName = cfg.server().defaultHostname(); } if (armeriaServer != null) { if (armeriaServer != cfg.server()) { throw new IllegalStateException("cannot be added to more than one server"); } else { return; } } armeriaServer = cfg.server(); armeriaServer.addListener(configurator); }
@Test public void addressesAndPorts_localhost() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpGet request = new HttpGet(server().uri("/jsp/addrs_and_ports.jsp")); request.setHeader("Host", "localhost:1111"); try (CloseableHttpResponse res = hc.execute(request)) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue()) .startsWith("text/html"); final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())) .replaceAll(""); assertThat(actualContent).matches( "<html><body>" + "<p>RemoteAddr: 127\\.0\\.0\\.1</p>" + "<p>RemoteHost: 127\\.0\\.0\\.1</p>" + "<p>RemotePort: [1-9][0-9]+</p>" + "<p>LocalAddr: (?!null)[^<]+</p>" + "<p>LocalName: " + server().server().defaultHostname() + "</p>" + "<p>LocalPort: " + server().httpPort() + "</p>" + "<p>ServerName: localhost</p>" + "<p>ServerPort: 1111</p>" + "</body></html>"); } } }
@Test public void addressesAndPorts_127001() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc.execute( new HttpGet(server().uri("/jsp/addrs_and_ports.jsp")))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue()) .startsWith("text/html"); final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())) .replaceAll(""); assertThat(actualContent).matches( "<html><body>" + "<p>RemoteAddr: 127\\.0\\.0\\.1</p>" + "<p>RemoteHost: 127\\.0\\.0\\.1</p>" + "<p>RemotePort: [1-9][0-9]+</p>" + "<p>LocalAddr: (?!null)[^<]+</p>" + "<p>LocalName: " + server().server().defaultHostname() + "</p>" + "<p>LocalPort: " + server().httpPort() + "</p>" + "<p>ServerName: 127\\.0\\.0\\.1</p>" + "<p>ServerPort: " + server().httpPort() + "</p>" + "</body></html>"); } } }
@Override public Entry get(String path, @Nullable String contentEncoding) { requireNonNull(path, "path"); return new ByteArrayEntry(path, MediaType.PLAIN_TEXT_UTF_8, server.defaultHostname().getBytes(StandardCharsets.UTF_8)); }
@Override public HttpFile get(String path, Clock clock, @Nullable String contentEncoding) { requireNonNull(path, "path"); return HttpFileBuilder.of(HttpData.of(StandardCharsets.UTF_8, server.defaultHostname())) .setHeader(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8) .build(); }
@Override public HttpFile get(String path, Clock clock, @Nullable String contentEncoding) { requireNonNull(path, "path"); return HttpFileBuilder.of(HttpData.of(StandardCharsets.UTF_8, server.defaultHostname())) .setHeader(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8) .build(); }