2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.assertj.core.api.Assertions.catchThrowable;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
33 import com.google.gson.Gson;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.net.HttpURLConnection;
37 import java.net.MalformedURLException;
39 import java.net.URLConnection;
40 import java.util.UUID;
41 import org.apache.commons.io.IOUtils;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
46 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
47 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
48 import org.onap.policy.common.utils.coder.StandardYamlCoder;
49 import org.onap.policy.common.utils.gson.GsonTestUtils;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * HttpServletServer JUNIT tests.
57 public class HttpServerTest {
58 private static final String LOCALHOST = "localhost";
59 private static final String JSON_MEDIA = "application/json";
60 private static final String YAML_MEDIA = YamlMessageBodyHandler.APPLICATION_YAML;
61 private static final String SWAGGER_JSON = "/swagger.json";
62 private static final String JUNIT_ECHO_HELLO = "/junit/echo/hello";
63 private static final String JUNIT_ECHO_FULL_REQUEST = "/junit/echo/full/request";
64 private static final String SOME_TEXT = "some text";
65 private static final String HELLO = "hello";
70 private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
72 private static final String LOCALHOST_PREFIX = "http://localhost:";
74 private static final Gson gson = new Gson();
77 * Server port. Incremented by 10 with each test.
79 private static int port = 5608;
81 private String portUrl;
84 * Increments the port number, clears the servers, and resets the providers.
89 portUrl = LOCALHOST_PREFIX + port;
91 HttpServletServerFactoryInstance.getServerFactory().destroy();
93 MyJacksonProvider.resetSome();
94 MyGsonProvider.resetSome();
95 MyYamlProvider.resetSome();
98 private static void incrementPort() {
103 public static void tearDownAfterClass() {
104 HttpServletServerFactoryInstance.getServerFactory().destroy();
108 public void testDefaultPackageServer() throws Exception {
109 logger.info("-- testDefaultPackageServer() --");
111 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
112 .build("echo", LOCALHOST, port, "/", false, true);
113 server.addServletPackage("/*", this.getClass().getPackage().getName());
114 server.addFilterClass("/*", TestFilter.class.getName());
115 server.waitedStart(5000);
117 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
119 RestEchoReqResp request = new RestEchoReqResp();
120 request.setRequestId(100);
121 request.setText(SOME_TEXT);
122 String reqText = gson.toJson(request);
124 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
125 assertEquals(reqText, response);
129 public void testJacksonPackageServer() throws Exception {
130 logger.info("-- testJacksonPackageServer() --");
132 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
133 .build("echo", LOCALHOST, port, "/", false, true);
135 server.setSerializationProvider(MyJacksonProvider.class.getName());
136 server.addServletPackage("/*", this.getClass().getPackage().getName());
137 server.addFilterClass("/*", TestFilter.class.getName());
138 server.waitedStart(5000);
140 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
142 RestEchoReqResp request = new RestEchoReqResp();
143 request.setRequestId(100);
144 request.setText(SOME_TEXT);
145 String reqText = gson.toJson(request);
147 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
148 assertEquals(reqText, response);
150 assertTrue(MyJacksonProvider.hasReadSome());
151 assertTrue(MyJacksonProvider.hasWrittenSome());
153 assertFalse(MyGsonProvider.hasReadSome());
154 assertFalse(MyGsonProvider.hasWrittenSome());
156 assertFalse(MyYamlProvider.hasReadSome());
157 assertFalse(MyYamlProvider.hasWrittenSome());
161 public void testGsonPackageServer() throws Exception {
162 logger.info("-- testGsonPackageServer() --");
164 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
165 .build("echo", LOCALHOST, port, "/", false, true);
167 server.setSerializationProvider(MyGsonProvider.class.getName());
168 server.addServletPackage("/*", this.getClass().getPackage().getName());
169 server.addFilterClass("/*", TestFilter.class.getName());
170 server.waitedStart(5000);
172 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
174 RestEchoReqResp request = new RestEchoReqResp();
175 request.setRequestId(100);
176 request.setText(SOME_TEXT);
177 String reqText = gson.toJson(request);
179 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
180 assertEquals(reqText, response);
182 assertTrue(MyGsonProvider.hasReadSome());
183 assertTrue(MyGsonProvider.hasWrittenSome());
185 assertFalse(MyJacksonProvider.hasReadSome());
186 assertFalse(MyJacksonProvider.hasWrittenSome());
188 assertFalse(MyYamlProvider.hasReadSome());
189 assertFalse(MyYamlProvider.hasWrittenSome());
193 public void testYamlPackageServer() throws Exception {
194 logger.info("-- testYamlPackageServer() --");
196 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
197 .build("echo", LOCALHOST, port, "/", false, true);
199 server.setSerializationProvider(MyYamlProvider.class.getName());
200 server.addServletPackage("/*", this.getClass().getPackage().getName());
201 server.addFilterClass("/*", TestFilter.class.getName());
202 server.waitedStart(5000);
204 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
206 RestEchoReqResp request = new RestEchoReqResp();
207 request.setRequestId(100);
208 request.setText(SOME_TEXT);
209 String reqText = new StandardYamlCoder().encode(request);
211 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, YAML_MEDIA, reqText);
213 // response reader strips newlines, so we should, too, before comparing
214 assertEquals(reqText.replace("\n", ""), response);
216 assertTrue(MyYamlProvider.hasReadSome());
217 assertTrue(MyYamlProvider.hasWrittenSome());
219 assertFalse(MyGsonProvider.hasReadSome());
220 assertFalse(MyGsonProvider.hasWrittenSome());
222 assertFalse(MyJacksonProvider.hasReadSome());
223 assertFalse(MyJacksonProvider.hasWrittenSome());
227 public void testDefaultClassServer() throws Exception {
228 logger.info("-- testDefaultClassServer() --");
230 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
231 .build("echo", LOCALHOST, port, "/", false, true);
232 server.addServletClass("/*", RestEchoService.class.getName());
233 server.addFilterClass("/*", TestFilter.class.getName());
234 server.waitedStart(5000);
236 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
238 RestEchoReqResp request = new RestEchoReqResp();
239 request.setRequestId(100);
240 request.setText(SOME_TEXT);
241 String reqText = gson.toJson(request);
243 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
244 assertEquals(reqText, response);
248 public void testJacksonClassServer() throws Exception {
249 logger.info("-- testJacksonClassServer() --");
251 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
252 .build("echo", LOCALHOST, port, "/", false, true);
253 server.setSerializationProvider(MyJacksonProvider.class.getName());
254 server.addServletClass("/*", RestEchoService.class.getName());
255 server.addFilterClass("/*", TestFilter.class.getName());
256 server.waitedStart(5000);
258 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
260 RestEchoReqResp request = new RestEchoReqResp();
261 request.setRequestId(100);
262 request.setText(SOME_TEXT);
263 String reqText = gson.toJson(request);
265 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
266 assertEquals(reqText, response);
268 assertTrue(MyJacksonProvider.hasReadSome());
269 assertTrue(MyJacksonProvider.hasWrittenSome());
271 assertFalse(MyGsonProvider.hasReadSome());
272 assertFalse(MyGsonProvider.hasWrittenSome());
274 assertFalse(MyYamlProvider.hasReadSome());
275 assertFalse(MyYamlProvider.hasWrittenSome());
279 public void testGsonClassServer() throws Exception {
280 logger.info("-- testGsonClassServer() --");
282 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
283 .build("echo", LOCALHOST, port, "/", false, true);
284 server.setSerializationProvider(MyGsonProvider.class.getName());
285 server.addServletClass("/*", RestEchoService.class.getName());
286 server.addFilterClass("/*", TestFilter.class.getName());
287 server.waitedStart(5000);
289 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
291 RestEchoReqResp request = new RestEchoReqResp();
292 request.setRequestId(100);
293 request.setText(SOME_TEXT);
294 String reqText = gson.toJson(request);
296 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
297 assertEquals(reqText, response);
299 assertTrue(MyGsonProvider.hasReadSome());
300 assertTrue(MyGsonProvider.hasWrittenSome());
302 assertFalse(MyJacksonProvider.hasReadSome());
303 assertFalse(MyJacksonProvider.hasWrittenSome());
305 assertFalse(MyYamlProvider.hasReadSome());
306 assertFalse(MyYamlProvider.hasWrittenSome());
310 public void testYamlClassServer() throws Exception {
311 logger.info("-- testYamlClassServer() --");
313 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
314 .build("echo", LOCALHOST, port, "/", false, true);
315 server.setSerializationProvider(MyYamlProvider.class.getName());
316 server.addServletClass("/*", RestEchoService.class.getName());
317 server.addFilterClass("/*", TestFilter.class.getName());
318 server.waitedStart(5000);
320 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
322 RestEchoReqResp request = new RestEchoReqResp();
323 request.setRequestId(100);
324 request.setText(SOME_TEXT);
325 String reqText = new StandardYamlCoder().encode(request);
327 String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, YAML_MEDIA, reqText);
329 // response reader strips newlines, so we should, too, before comparing
330 assertEquals(reqText.replace("\n", ""), response);
332 assertTrue(MyYamlProvider.hasReadSome());
333 assertTrue(MyYamlProvider.hasWrittenSome());
335 assertFalse(MyGsonProvider.hasReadSome());
336 assertFalse(MyGsonProvider.hasWrittenSome());
338 assertFalse(MyJacksonProvider.hasReadSome());
339 assertFalse(MyJacksonProvider.hasWrittenSome());
343 public void testSerialize() {
344 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
345 .build("echo", LOCALHOST, port, "/", false, true);
346 server.addServletPackage("/*", this.getClass().getPackage().getName());
347 server.addFilterClass("/*", TestFilter.class.getName());
349 // ensure we can serialize the server
350 assertThatCode(() -> new GsonTestUtils().compareGson(server, HttpServerTest.class)).doesNotThrowAnyException();
354 public void testSingleServer() throws Exception {
355 logger.info("-- testSingleServer() --");
357 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
358 .build("echo", LOCALHOST, port, "/", false, true);
359 server.addServletPackage("/*", this.getClass().getPackage().getName());
360 server.addFilterClass("/*", TestFilter.class.getName());
361 server.waitedStart(5000);
363 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
364 assertFalse(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
366 String response = http(portUrl + JUNIT_ECHO_HELLO);
367 assertEquals(HELLO, response);
369 assertThatThrownBy(() -> http(portUrl + SWAGGER_JSON)).isInstanceOf(IOException.class);
371 response = http(portUrl + "/junit/echo/hello?block=true");
372 assertEquals("FILTERED", response);
374 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
375 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
377 server.setAafAuthentication("/*");
378 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAaf());
380 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
381 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
385 public void testMultipleServers() throws Exception {
386 logger.info("-- testMultipleServers() --");
388 HttpServletServer server1 = HttpServletServerFactoryInstance.getServerFactory()
389 .build("echo-1", false, LOCALHOST, port, "/", true, true);
390 server1.addServletPackage("/*", this.getClass().getPackage().getName());
391 server1.waitedStart(5000);
393 int port2 = port + 1;
395 HttpServletServer server2 = HttpServletServerFactoryInstance.getServerFactory()
396 .build("echo-2", LOCALHOST, port2, "/", false, true);
397 server2.addServletPackage("/*", this.getClass().getPackage().getName());
398 server2.waitedStart(5000);
400 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
401 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port2).isAlive());
403 String response = http(portUrl + JUNIT_ECHO_HELLO);
404 assertEquals(HELLO, response);
406 response = http(portUrl + SWAGGER_JSON);
407 assertNotNull(response);
409 response = http(LOCALHOST_PREFIX + port2 + JUNIT_ECHO_HELLO);
410 assertEquals(HELLO, response);
412 assertThatThrownBy(() -> http(LOCALHOST_PREFIX + port2 + SWAGGER_JSON)).isInstanceOf(IOException.class);
414 HttpServletServerFactoryInstance.getServerFactory().destroy();
415 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
419 public void testMultiServicePackage() throws Exception {
420 logger.info("-- testMultiServicePackage() --");
422 String randomName = UUID.randomUUID().toString();
424 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
425 .build(randomName, LOCALHOST, port, "/", false, true);
426 server.addServletPackage("/*", this.getClass().getPackage().getName());
427 server.waitedStart(5000);
429 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
431 String response = http(portUrl + JUNIT_ECHO_HELLO);
432 assertEquals(HELLO, response);
434 response = http(portUrl + "/junit/endpoints/http/servers");
435 assertTrue(response.contains(randomName));
437 HttpServletServerFactoryInstance.getServerFactory().destroy();
438 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
442 public void testServiceClass() throws Exception {
443 logger.info("-- testServiceClass() --");
444 String randomName = UUID.randomUUID().toString();
446 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
447 .build(randomName, LOCALHOST, port, "/", false, true);
448 server.addServletClass("/*", RestEchoService.class.getName());
449 server.waitedStart(5000);
451 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
453 String response = http(portUrl + JUNIT_ECHO_HELLO);
454 assertEquals(HELLO, response);
456 HttpServletServerFactoryInstance.getServerFactory().destroy();
457 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
461 public void testMultiServiceClass() throws Exception {
462 logger.info("-- testMultiServiceClass() --");
464 String randomName = UUID.randomUUID().toString();
466 HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
467 .build(randomName, LOCALHOST, port, "/", false, true);
468 server.addServletClass("/*", RestEchoService.class.getName());
469 server.addServletClass("/*", RestEndpoints.class.getName());
470 server.waitedStart(5000);
472 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
474 String response = http(portUrl + JUNIT_ECHO_HELLO);
475 assertEquals(HELLO, response);
477 response = http(portUrl + "/junit/endpoints/http/servers");
478 assertTrue(response.contains(randomName));
480 HttpServletServerFactoryInstance.getServerFactory().destroy();
481 assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty());
485 public void testSingleStaticResourceServer() throws Exception {
486 logger.info("-- testSingleStaticResourceServer() --");
488 HttpServletServer staticServer = HttpServletServerFactoryInstance.getServerFactory()
489 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
490 Throwable thrown = catchThrowable(() -> staticServer.addServletResource("/*", null));
491 assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
492 .hasMessageContaining("No resourceBase provided");
494 staticServer.addServletResource(null,
495 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
497 thrown = catchThrowable(() -> staticServer.addServletClass("/*", RestEchoService.class.getName()));
498 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
499 .hasMessageContaining("is not supported on this type of jetty server");
501 thrown = catchThrowable(() -> staticServer.addServletPackage("/api/*", this.getClass().getPackage().getName()));
502 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
503 .hasMessageContaining("is not supported on this type of jetty server");
505 thrown = catchThrowable(() -> staticServer.setSerializationProvider(MyGsonProvider.class.getName()));
506 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
507 .hasMessageContaining("is not supported on this type of jetty server");
509 staticServer.waitedStart(5000);
511 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
512 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
514 String response = http(portUrl);
515 assertThat(response).contains("Test Jetty Static Resources Root");
517 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
518 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
522 public void testMultiStaticResourceServer() throws Exception {
523 logger.info("-- testMultiStaticResourceServer() --");
525 HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
526 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
527 staticResourceServer.addServletResource("/root/*",
528 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
529 staticResourceServer.addServletResource("/alt-root/*",
530 HttpServerTest.class.getClassLoader().getResource("webapps/alt-root").toExternalForm());
531 staticResourceServer.waitedStart(5000);
533 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
534 assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
536 String response = http(portUrl + "/root/");
537 assertThat(response).contains("Test Jetty Static Resources Root");
539 response = http(portUrl + "/alt-root/");
540 assertThat(response).contains("Test Jetty Static Resources Alt-Root");
542 HttpServletServerFactoryInstance.getServerFactory().destroy(port);
543 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
547 public void testMultiTypesServer() throws Exception {
548 logger.info("-- testMultiTypesServer() --");
550 HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory()
551 .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true);
552 staticResourceServer.addServletResource("/root/*",
553 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm());
554 staticResourceServer.waitedStart(5000);
556 int port2 = port + 1;
557 HttpServletServer jerseyServer =
558 HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, port2, "/", false, true);
559 jerseyServer.addServletPackage("/api/*", this.getClass().getPackage().getName());
561 Throwable thrown = catchThrowable(() -> jerseyServer.addServletResource("/root/*",
562 HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()));
563 assertThat(thrown).isInstanceOf(UnsupportedOperationException.class)
564 .hasMessageContaining("is not supported on this type of jetty server");
566 jerseyServer.waitedStart(5000);
568 assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
569 assertEquals(2, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
571 String response = http(portUrl + "/root/");
572 assertThat(response).contains("Test Jetty Static Resources Root");
574 response = http(LOCALHOST_PREFIX + port2 + "/api" + JUNIT_ECHO_HELLO);
575 assertEquals(HELLO, response);
577 HttpServletServerFactoryInstance.getServerFactory().destroy();
578 assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size());
582 * performs an http request.
584 * @throws MalformedURLException make sure URL is good
585 * @throws IOException thrown is IO exception occurs
586 * @throws InterruptedException thrown if thread interrupted occurs
588 private String http(String urlString)
589 throws IOException, InterruptedException {
590 URL url = new URL(urlString);
591 if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) {
592 throw new IllegalStateException("port never opened: " + url);
594 return response(url.openConnection());
598 * Performs an http request.
600 * @throws MalformedURLException make sure URL is good
601 * @throws IOException thrown is IO exception occurs
602 * @throws InterruptedException thrown if thread interrupted occurs
604 private String http(String urlString, String mediaType, String post)
605 throws IOException, InterruptedException {
606 URL url = new URL(urlString);
607 if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 100)) {
608 throw new IllegalStateException("port never opened: " + url);
610 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
611 conn.setRequestMethod("POST");
612 conn.setDoOutput(true);
613 conn.setRequestProperty("Content-Type", mediaType);
614 conn.setRequestProperty("Accept", mediaType);
615 IOUtils.write(post, conn.getOutputStream());
616 return response(conn);
620 * gets http response.
622 * @param conn connection from which to read
624 * @throws IOException if an I/O error occurs
626 private String response(URLConnection conn) throws IOException {
627 try (InputStream inpstr = conn.getInputStream()) {
628 return String.join("", IOUtils.readLines(inpstr));