test-api-clearing http://10.200.200.183:8701/ последние улучшение тестового модуля

This commit is contained in:
AKurakin 2023-10-11 11:22:39 +03:00
parent 3eaa2a9ac6
commit 894408354b
2 changed files with 18 additions and 5 deletions

View file

@ -35,9 +35,9 @@ public class Controller extends HttpServerSimpleFramework {
@Override
protected void createContextPages(HttpServer server, String baseUrl) throws IOException {
//server.createContext(baseUrl, new RedirectPage(baseUrl+"index.html"));
server.createContext(baseUrl, new RedirectPage(baseUrl, baseUrl+"index.html"));
//server.createContext(baseUrl + "/favicon.ico", staticPageFromResource("pages/favicon.ico", null).contentType="image/icon");
server.createContext(baseUrl + "", staticPageFromResource("pages/index.html", null));
server.createContext(baseUrl + "index.html", staticPageFromResource("pages/index.html", null));
// Database
server.createContext(baseUrl + "imdg/reload", new ImdgHandler());

View file

@ -37,7 +37,7 @@ public abstract class HttpServerSimpleFramework implements InitializingBean, Dis
}
protected void createContextPages(HttpServer server, String baseUrl) throws IOException {
server.createContext(baseUrl, new RedirectPage(baseUrl + "index.html"));
server.createContext(baseUrl, new RedirectPage(baseUrl, baseUrl + "index.html"));
server.createContext(baseUrl + "index.html", staticPageFromResource("pages/index.html", null));
}
@ -58,7 +58,7 @@ public abstract class HttpServerSimpleFramework implements InitializingBean, Dis
@Override
public void destroy() throws Exception {
if (server != null) {
server.stop(100);
server.stop(6);
log.info("HTTP test controller stop.");
}
}
@ -121,14 +121,27 @@ public abstract class HttpServerSimpleFramework implements InitializingBean, Dis
public static class RedirectPage implements HttpHandler {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected String myUrl; // для редиректа с корня /
protected String toUrl;
public RedirectPage(String toUrl) {
public RedirectPage(String myUrl, String toUrl) {
this.myUrl = Objects.requireNonNull(myUrl);
this.toUrl = Objects.requireNonNull(toUrl);
}
@Override
public void handle(HttpExchange he) throws IOException {
if (!myUrl.equalsIgnoreCase(he.getRequestURI().getPath()) &&
!myUrl.equalsIgnoreCase(he.getRequestURI().getPath() + "/") &&
!(myUrl + "/").equalsIgnoreCase(he.getRequestURI().getPath())) {
log.info("Access {} url \"{}\" IP {}; 404", he.getRequestMethod(),
he.getRequestURI(), he.getRemoteAddress().getAddress().getHostAddress());
he.sendResponseHeaders(404, 0);
try (OutputStream os = he.getResponseBody()) {
os.write("404 Page not found".getBytes("windows-1251"));
}
he.close();
}
log.info("Access {} url \"{}\" IP {}; redirect to \"{}\"", he.getRequestMethod(),
he.getRequestURI(), he.getRemoteAddress().getAddress().getHostAddress(), toUrl);
Headers responseHeaders = he.getResponseHeaders();