/*
|
http-adoc - Asciidoctor extensions to jdk.httpserver
|
Copyright (C) 2021 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.httpserver.adoc;
|
|
import com.sun.net.httpserver.Filter;
|
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpHandler;
|
import de.uhilger.httpserver.base.HttpHelper;
|
import de.uhilger.httpserver.base.handler.FileHandler;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URI;
|
import java.util.logging.Logger;
|
|
/**
|
* AdocFilter prueft, ob fuer eine
|
* Asciidoctor-Quelldatei bereits eine HTML-Version vorliegt. Wenn nicht,
|
* verwendet der AdocFilter den AdocActor, um eine HTML-Version zu erzeugen.
|
*
|
* @author Ulrich Hilger
|
* @version 1, 16.06.2021
|
*/
|
public class AdocFilter extends Filter {
|
|
private static final Logger logger = Logger.getLogger(AdocFilter.class.getName());
|
|
public static final String DESCRIPTION = "Asciidoctor Filter";
|
|
public static final String ADOC = ".adoc";
|
|
@Override
|
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
|
URI uri = exchange.getRequestURI();
|
String query = uri.getQuery();
|
//String[] params = query.split("?&"); // hier noch Regex ermitteln
|
String requestPathStr = uri.getPath();
|
//logger.fine("filter: " + requestUriStr);
|
|
if(requestPathStr.toLowerCase().endsWith(ADOC)) {
|
logger.fine("filter: " + requestPathStr);
|
HttpHandler handler = exchange.getHttpContext().getHandler();
|
if(handler instanceof FileHandler) {
|
FileHandler fileHandler = (FileHandler) handler;
|
String fileBase = exchange.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString();
|
AdocActor actor = new AdocActor();
|
HttpHelper helper = new HttpHelper();
|
actor.processAdocFile(new File(fileBase, helper.getFileName(exchange)), Boolean.FALSE.toString());
|
}
|
}
|
chain.doFilter(exchange);
|
}
|
|
@Override
|
public String description() {
|
return DESCRIPTION;
|
}
|
|
}
|