/*
|
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.HttpExchange;
|
import com.sun.net.httpserver.HttpHandler;
|
import de.uhilger.httpserver.base.handler.FileHandler;
|
import java.io.IOException;
|
import java.net.URI;
|
import java.util.logging.Logger;
|
|
/**
|
* Der AdocHandler liefert die HTML-Version einer Asciidoc-Quelldatei aus.
|
*
|
* Dazu muss ein AdocHandler mit dem PatternDelegator an einen Pfad
|
* geknuepft werden, der Dateien mit der Endung adoc bezeichnet.
|
*
|
* Ein dafuer geeigneter regulaerer Ausdruck ist z.B.
|
* <pre>
|
* .+\\.adoc
|
* </pre>
|
*
|
* Zum Ausliefern der HTML-Version delegiert der AdocHandler an eine
|
* Instanz des AdocActors.
|
*
|
* Zum Erzeugen der HTML-Version muss ein AdocFilter mit dem HttpContext
|
* verbunden sein, an den der AdocHandler gebunden ist.
|
*
|
* Benoetigt das Attribut
|
* FileHandler.ATTR_FILE_BASE
|
*
|
* @author Ulrich Hilger
|
* @version 1, 16.06.2021
|
*/
|
public class AdocHandler implements HttpHandler {
|
|
private static final Logger logger = Logger.getLogger(AdocHandler.class.getName());
|
|
@Override
|
public void handle(HttpExchange e) throws IOException {
|
URI uri = e.getRequestURI();
|
logger.fine(uri.toString());
|
|
String ctxPath = e.getHttpContext().getPath();
|
String uriPath = e.getRequestURI().getPath();
|
String fName = uriPath.substring(ctxPath.length());
|
|
AdocActor actor = new AdocActor();
|
String query = uri.getQuery();
|
if(query != null && query.equalsIgnoreCase("pdf=true")) {
|
actor.handle(e, e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), fName, true);
|
} else {
|
actor.handle(e, e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), fName, false);
|
}
|
}
|
|
|
|
}
|