2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.http.server.test;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.assertj.core.api.Assertions.catchThrowable;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
31 import com.google.gson.Gson;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.HttpURLConnection;
36 import java.net.MalformedURLException;
38 import java.net.URLConnection;
39 import java.util.UUID;
40 import org.apache.commons.io.IOUtils;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
45 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
46 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
47 import org.onap.policy.common.utils.coder.StandardYamlCoder;
48 import org.onap.policy.common.utils.gson.GsonTestUtils;
49 import org.onap.policy.common.utils.network.NetworkUtil;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * HttpServletServer JUNIT tests.
56 public class HttpServerTest {
57 private static final String LOCALHOST = "localhost";
58 private static final String JSON_MEDIA = "application/json";
59 private static final String YAML_MEDIA = YamlMessageBodyHandler.APPLICATION_YAML;
60 private static final String SWAGGER_JSON = "/swagger.json";
61 private static final String JUNIT_ECHO_HELLO = "/junit/echo/hello";
62 private static final String JUNIT_ECHO_FULL_REQUEST = "/junit/echo/full/request";
63 private static final String SOME_TEXT = "some text";
64 private static final String HELLO = "hello";
69 private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
71 private static final String LOCALHOST_PREFIX = "http://localhost:";
73 private static final Gson gson = new Gson();
76 * Server port. Incremented by 10 with each test.
78 private static int port = 5608;
80 private String portUrl;
83 * Increments the port number, clears the servers, and resets the providers.
88 portUrl = LOCALHOST_PREFIX + port;
90 HttpServletServerFactoryInstance.getServerFactory().destroy();
92 MyJacksonProvider.resetSome();
93 MyGsonProvider.resetSome();
94 MyYamlProvider.resetSome();
97 private static void incrementPort() {
102 public static void tearDownAfterClass() {
103 HttpServletServerFactoryInstance.getServerFactory().destroy();
107 public void testDefaultPackageServer() throws Exception {
108 logger.info("-- testDefaultPackageServer() --");
110 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
111 .build("echo", LOCALHOST, port, "/", false, true);
112 server.addServletPackage("/*", this.getClass().getPackage().getName());
113 server.addFilterClass("/*", TestFilter.class.getName());
114 server.waitedStart(5000);
116 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
118 RestEchoReqResp request = new RestEchoReqResp();
119 request.setRequestId(100);
120 request.setText(SOME_TEXT);
121 String reqText = gson.toJson(request);
123 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
124 assertEquals(reqText, response);
128 public void testJacksonPackageServer() throws Exception {
129 logger.info("-- testJacksonPackageServer() --");
131 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
132 .build("echo", LOCALHOST, port, "/", false, true);
134 server.setSerializationProvider(MyJacksonProvider.class.getName());
135 server.addServletPackage("/*", this.getClass().getPackage().getName());
136 server.addFilterClass("/*", TestFilter.class.getName());
137 server.waitedStart(5000);
139 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
141 RestEchoReqResp request = new RestEchoReqResp();
142 request.setRequestId(100);
143 request.setText(SOME_TEXT);
144 String reqText = gson.toJson(request);
146 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
147 assertEquals(reqText, response);
149 assertTrue(MyJacksonProvider.hasReadSome());
150 assertTrue(MyJacksonProvider.hasWrittenSome());
152 assertFalse(MyGsonProvider.hasReadSome());
153 assertFalse(MyGsonProvider.hasWrittenSome());
155 assertFalse(MyYamlProvider.hasReadSome());
156 assertFalse(MyYamlProvider.hasWrittenSome());
160 public void testGsonPackageServer() throws Exception {
161 logger.info("-- testGsonPackageServer() --");
163 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
164 .build("echo", LOCALHOST, port, "/", false, true);
166 server.setSerializationProvider(MyGsonProvider.class.getName());
167 server.addServletPackage("/*", this.getClass().getPackage().getName());
168 server.addFilterClass("/*", TestFilter.class.getName());
169 server.waitedStart(5000);
171 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
173 RestEchoReqResp request = new RestEchoReqResp();
174 request.setRequestId(100);
175 request.setText(SOME_TEXT);
176 String reqText = gson.toJson(request);
178 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
179 assertEquals(reqText, response);
181 assertTrue(MyGsonProvider.hasReadSome());
182 assertTrue(MyGsonProvider.hasWrittenSome());
184 assertFalse(MyJacksonProvider.hasReadSome());
185 assertFalse(MyJacksonProvider.hasWrittenSome());
187 assertFalse(MyYamlProvider.hasReadSome());
188 assertFalse(MyYamlProvider.hasWrittenSome());
192 public void testYamlPackageServer() throws Exception {
193 logger.info("-- testYamlPackageServer() --");
195 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
196 .build("echo", LOCALHOST, port, "/", false, true);
198 server.setSerializationProvider(MyYamlProvider.class.getName());
199 server.addServletPackage("/*", this.getClass().getPackage().getName());
200 server.addFilterClass("/*", TestFilter.class.getName());
201 server.waitedStart(5000);
203 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
205 RestEchoReqResp request = new RestEchoReqResp();
206 request.setRequestId(100);
207 request.setText(SOME_TEXT);
208 String reqText = new StandardYamlCoder().encode(request);
210 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, YAML_MEDIA, reqText);
212 // response reader strips newlines, so we should, too, before comparing
213 assertEquals(reqText.replace("\n", ""), response);
215 assertTrue(MyYamlProvider.hasReadSome());
216 assertTrue(MyYamlProvider.hasWrittenSome());
218 assertFalse(MyGsonProvider.hasReadSome());
219 assertFalse(MyGsonProvider.hasWrittenSome());
221 assertFalse(MyJacksonProvider.hasReadSome());
222 assertFalse(MyJacksonProvider.hasWrittenSome());
226 public void testDefaultClassServer() throws Exception {
227 logger.info("-- testDefaultClassServer() --");
229 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
230 .build("echo", LOCALHOST, port, "/", false, true);
231 server.addServletClass("/*", RestEchoService.class.getName());
232 server.addFilterClass("/*", TestFilter.class.getName());
233 server.waitedStart(5000);
235 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
237 RestEchoReqResp request = new RestEchoReqResp();
238 request.setRequestId(100);
239 request.setText(SOME_TEXT);
240 String reqText = gson.toJson(request);
242 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
243 assertEquals(reqText, response);
247 public void testJacksonClassServer() throws Exception {
248 logger.info("-- testJacksonClassServer() --");
250 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
251 .build("echo", LOCALHOST, port, "/", false, true);
252 server.setSerializationProvider(MyJacksonProvider.class.getName());
253 server.addServletClass("/*", RestEchoService.class.getName());
254 server.addFilterClass("/*", TestFilter.class.getName());
255 server.waitedStart(5000);
257 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
259 RestEchoReqResp request = new RestEchoReqResp();
260 request.setRequestId(100);
261 request.setText(SOME_TEXT);
262 String reqText = gson.toJson(request);
264 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
265 assertEquals(reqText, response);
267 assertTrue(MyJacksonProvider.hasReadSome());
268 assertTrue(MyJacksonProvider.hasWrittenSome());
270 assertFalse(MyGsonProvider.hasReadSome());
271 assertFalse(MyGsonProvider.hasWrittenSome());
273 assertFalse(MyYamlProvider.hasReadSome());
274 assertFalse(MyYamlProvider.hasWrittenSome());
278 public void testGsonClassServer() throws Exception {
279 logger.info("-- testGsonClassServer() --");
281 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
282 .build("echo", LOCALHOST, port, "/", false, true);
283 server.setSerializationProvider(MyGsonProvider.class.getName());
284 server.addServletClass("/*", RestEchoService.class.getName());
285 server.addFilterClass("/*", TestFilter.class.getName());
286 server.waitedStart(5000);
288 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
290 RestEchoReqResp request = new RestEchoReqResp();
291 request.setRequestId(100);
292 request.setText(SOME_TEXT);
293 String reqText = gson.toJson(request);
295 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
296 assertEquals(reqText, response);
298 assertTrue(MyGsonProvider.hasReadSome());
299 assertTrue(MyGsonProvider.hasWrittenSome());
301 assertFalse(MyJacksonProvider.hasReadSome());
302 assertFalse(MyJacksonProvider.hasWrittenSome());
304 assertFalse(MyYamlProvider.hasReadSome());
305 assertFalse(MyYamlProvider.hasWrittenSome());
309 public void testYamlClassServer() throws Exception {
310 logger.info("-- testYamlClassServer() --");
312 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
313 .build("echo", LOCALHOST, port, "/", false, true);
314 server.setSerializationProvider(MyYamlProvider.class.getName());
315 server.addServletClass("/*", RestEchoService.class.getName());
316 server.addFilterClass("/*", TestFilter.class.getName());
317 server.waitedStart(5000);
319 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
321 RestEchoReqResp request = new RestEchoReqResp();
322 request.setRequestId(100);
323 request.setText(SOME_TEXT);
324 String reqText = new StandardYamlCoder().encode(request);
326 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, YAML_MEDIA, reqText);
328 // response reader strips newlines, so we should, too, before comparing
329 assertEquals(reqText.replace("\n", ""), response);
331 assertTrue(MyYamlProvider.hasReadSome());
332 assertTrue(MyYamlProvider.hasWrittenSome());
334 assertFalse(MyGsonProvider.hasReadSome());
335 assertFalse(MyGsonProvider.hasWrittenSome());
337 assertFalse(MyJacksonProvider.hasReadSome());
338 assertFalse(MyJacksonProvider.hasWrittenSome());
342 public void testSerialize() {
343 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
344 .build("echo", LOCALHOST, port, "/", false, true);
345 server.addServletPackage("/*", this.getClass().getPackage().getName());
346 server.addFilterClass("/*", TestFilter.class.getName());
348 // ensure we can serialize the server
349 new GsonTestUtils().compareGson(server, HttpServerTest.class);
353 public void testSingleServer() throws Exception {
354 logger.info("-- testSingleServer() --");
356 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
357 .build("echo", LOCALHOST, port, "/", false, true);
358 server.addServletPackage("/*", this.getClass().getPackage().getName());
359 server.addFilterClass("/*", TestFilter.class.getName());
360 server.waitedStart(5000);
362 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
363 assertFalse(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
365 String response = http(portUrl + JUNIT_ECHO_HELLO);
366 assertEquals(HELLO, response);
368 assertThatThrownBy(() -> http(portUrl + SWAGGER_JSON)).isInstanceOf(IOException.class);
370 response = http(portUrl + "/junit/echo/hello?block=true");
371 assertEquals("FILTERED", response);
373 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
374 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
376 server.setAafAuthentication("/*");
377 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
379 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
380 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
384 public void testMultipleServers() throws Exception {
385 logger.info("-- testMultipleServers() --");
387 HttpServletServer server1 = HttpServletServerFactoryInstance.getServerFactory()
388 .build("echo-1", false,LOCALHOST, port, "/", true, true);
389 server1.addServletPackage("/*", this.getClass().getPackage().getName());
390 server1.waitedStart(5000);
392 int port2 = port + 1;
394 HttpServletServer server2 = HttpServletServerFactoryInstance.getServerFactory()
395 .build("echo-2", LOCALHOST, port2, "/", false, true);
396 server2.addServletPackage("/*", this.getClass().getPackage().getName());
397 server2.waitedStart(5000);
399 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
400 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port2).isAlive());
402 String response = http(portUrl + JUNIT_ECHO_HELLO);
403 assertTrue(HELLO.equals(response));
405 response = http(portUrl + SWAGGER_JSON);
406 assertTrue(response != null);
408 response = http(LOCALHOST_PREFIX + port2 + JUNIT_ECHO_HELLO);
409 assertTrue(HELLO.equals(response));
411 assertThatThrownBy(() -> http(LOCALHOST_PREFIX + port2 + SWAGGER_JSON)).isInstanceOf(IOException.class);
413 HttpServletServerFactoryInstance.getServerFactory().destroy();
414 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
418 public void testMultiServicePackage() throws Exception {
419 logger.info("-- testMultiServicePackage() --");
421 String randomName = UUID.randomUUID().toString();
423 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
424 .build(randomName, LOCALHOST, port, "/", false, true);
425 server.addServletPackage("/*", this.getClass().getPackage().getName());
426 server.waitedStart(5000);
428 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
430 String response = http(portUrl + JUNIT_ECHO_HELLO);
431 assertTrue(HELLO.equals(response));
433 response = http(portUrl + "/junit/endpoints/http/servers");
434 assertTrue(response.contains(randomName));
436 HttpServletServerFactoryInstance.getServerFactory().destroy();
437 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
441 public void testServiceClass() throws Exception {
442 logger.info("-- testServiceClass() --");
443 String randomName = UUID.randomUUID().toString();
445 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
446 .build(randomName, LOCALHOST, port, "/", false, true);
447 server.addServletClass("/*", RestEchoService.class.getName());
448 server.waitedStart(5000);
450 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
452 String response = http(portUrl + JUNIT_ECHO_HELLO);
453 assertTrue(HELLO.equals(response));
455 HttpServletServerFactoryInstance.getServerFactory().destroy();
456 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
460 public void testMultiServiceClass() throws Exception {
461 logger.info("-- testMultiServiceClass() --");
463 String randomName = UUID.randomUUID().toString();
465 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
466 .build(randomName, LOCALHOST, port, "/", false, true);
467 server.addServletClass("/*", RestEchoService.class.getName());
468 server.addServletClass("/*", RestEndpoints.class.getName());
469 server.waitedStart(5000);
471 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
473 String response = http(portUrl + JUNIT_ECHO_HELLO);
474 assertTrue(HELLO.equals(response));
476 response = http(portUrl + "/junit/endpoints/http/servers");
477 assertTrue(response.contains(randomName));
479 HttpServletServerFactoryInstance.getServerFactory().destroy();
480 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
484 public void testSingleStaticResourceServer() throws Exception {
485 logger.info("-- testSingleStaticResourceServer() --");
487 HttpServletServer staticServer = HttpServletServerFactoryInstance.getServerFactory()
488 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
489 Throwable thrown = catchThrowable(() -> staticServer.addServletResource("/*", null));
490 assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
491 .hasMessageContaining("No resourceBase provided");
493 staticServer.addServletResource(null,
494 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
496 thrown = catchThrowable(() -> staticServer.addServletClass("/*", RestEchoService.class.getName()));
497 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
498 .hasMessageContaining("is not supported on this type of jetty server");
500 thrown = catchThrowable(() -> staticServer.addServletPackage("/api/*", this.getClass().getPackage().getName()));
501 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
502 .hasMessageContaining("is not supported on this type of jetty server");
504 thrown = catchThrowable(() -> staticServer.setSerializationProvider(MyGsonProvider.class.getName()));
505 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
506 .hasMessageContaining("is not supported on this type of jetty server");
508 staticServer.waitedStart(5000);
510 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
511 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
513 String response = http(portUrl);
514 assertThat(response).contains("Test Jetty Static Resources Root");
516 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
517 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
521 public void testMultiStaticResourceServer() throws Exception {
522 logger.info("-- testMultiStaticResourceServer() --");
524 HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
525 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
526 staticResourceServer.addServletResource("/root/*",
527 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
528 staticResourceServer.addServletResource("/alt-root/*",
529 HttpServerTest.class.getClassLoader().getResource("webapps/alt-root").toExternalForm());
530 staticResourceServer.waitedStart(5000);
532 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
533 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
535 String response = http(portUrl + "/root/");
536 assertThat(response).contains("Test Jetty Static Resources Root");
538 response = http(portUrl + "/alt-root/");
539 assertThat(response).contains("Test Jetty Static Resources Alt-Root");
541 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
542 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
546 public void testMultiTypesServer() throws Exception {
547 logger.info("-- testMultiTypesServer() --");
549 HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
550 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
551 staticResourceServer.addServletResource("/root/*",
552 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
553 staticResourceServer.waitedStart(5000);
555 int port2 = port + 1;
556 HttpServletServer jerseyServer =
557 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, port2, "/", false, true);
558 jerseyServer.addServletPackage("/api/*", this.getClass().getPackage().getName());
560 Throwable thrown = catchThrowable(() -> jerseyServer.addServletResource("/root/*",
561 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()));
562 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
563 .hasMessageContaining("is not supported on this type of jetty server");
565 jerseyServer.waitedStart(5000);
567 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
568 assertEquals(2, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
570 String response = http(portUrl + "/root/");
571 assertThat(response).contains("Test Jetty Static Resources Root");
573 response = http(LOCALHOST_PREFIX + port2 + "/api" + JUNIT_ECHO_HELLO);
574 assertEquals(HELLO, response);
576 HttpServletServerFactoryInstance.getServerFactory().destroy();
577 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
581 * performs an http request.
583 * @throws MalformedURLException make sure URL is good
584 * @throws IOException thrown is IO exception occurs
585 * @throws InterruptedException thrown if thread interrupted occurs
587 private String http(String urlString)
588 throws IOException, InterruptedException {
589 URL url = new URL(urlString);
590 if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) {
591 throw new IllegalStateException("port never opened: " + url);
593 return response(url.openConnection());
597 * Performs an http request.
599 * @throws MalformedURLException make sure URL is good
600 * @throws IOException thrown is IO exception occurs
601 * @throws InterruptedException thrown if thread interrupted occurs
603 private String http(String urlString, String mediaType, String post)
604 throws IOException, InterruptedException {
605 URL url = new URL(urlString);
606 if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) {
607 throw new IllegalStateException("port never opened: " + url);
609 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
610 conn.setRequestMethod("POST");
611 conn.setDoOutput(true);
612 conn.setRequestProperty("Content-Type", mediaType);
613 conn.setRequestProperty("Accept", mediaType);
614 IOUtils.write(post, conn.getOutputStream());
615 return response(conn);
619 * gets http response.
621 * @param conn connection from which to read
623 * @throws IOException if an I/O error occurs
625 private String response(URLConnection conn) throws IOException {
626 try (InputStream inpstr = conn.getInputStream()) {
627 return String.join("", IOUtils.readLines(inpstr));