commit | author | age
|
9c5db9
|
1 |
/* |
U |
2 |
http-adoc - Asciidoctor extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.adoc; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
d686d3
|
21 |
import com.sun.net.httpserver.HttpHandler; |
9c5db9
|
22 |
import de.uhilger.httpserver.base.handler.FileHandler; |
U |
23 |
import java.io.IOException; |
5ca813
|
24 |
import java.net.URI; |
9c5db9
|
25 |
import java.util.logging.Logger; |
U |
26 |
|
|
27 |
/** |
d686d3
|
28 |
* Der AdocHandler liefert die HTML-Version einer Asciidoc-Quelldatei aus. |
U |
29 |
* |
|
30 |
* Dazu muss ein AdocHandler mit dem PatternDelegator an einen Pfad |
|
31 |
* geknuepft werden, der Dateien mit der Endung adoc bezeichnet. |
|
32 |
* |
7c7c9b
|
33 |
* Ein dafuer geeigneter regulaerer Ausdruck ist z.B. |
d686d3
|
34 |
* <pre> |
U |
35 |
* .+\\.adoc |
|
36 |
* </pre> |
|
37 |
* |
|
38 |
* Zum Ausliefern der HTML-Version delegiert der AdocHandler an eine |
|
39 |
* Instanz des AdocActors. |
|
40 |
* |
|
41 |
* Zum Erzeugen der HTML-Version muss ein AdocFilter mit dem HttpContext |
|
42 |
* verbunden sein, an den der AdocHandler gebunden ist. |
9c5db9
|
43 |
* |
93ae55
|
44 |
* Benoetigt das Attribut |
U |
45 |
* FileHandler.ATTR_FILE_BASE |
|
46 |
* |
9c5db9
|
47 |
* @author Ulrich Hilger |
U |
48 |
* @version 1, 16.06.2021 |
|
49 |
*/ |
d686d3
|
50 |
public class AdocHandler implements HttpHandler { |
9c5db9
|
51 |
|
U |
52 |
private static final Logger logger = Logger.getLogger(AdocHandler.class.getName()); |
|
53 |
|
|
54 |
@Override |
|
55 |
public void handle(HttpExchange e) throws IOException { |
5ca813
|
56 |
URI uri = e.getRequestURI(); |
U |
57 |
logger.fine(uri.toString()); |
d686d3
|
58 |
|
U |
59 |
String ctxPath = e.getHttpContext().getPath(); |
|
60 |
String uriPath = e.getRequestURI().getPath(); |
|
61 |
String fName = uriPath.substring(ctxPath.length()); |
8b9c74
|
62 |
|
93ae55
|
63 |
AdocActor actor = new AdocActor(); |
8b9c74
|
64 |
String query = uri.getQuery(); |
U |
65 |
if(query != null && query.equalsIgnoreCase("pdf=true")) { |
|
66 |
actor.handle(e, e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), fName, true); |
|
67 |
} else { |
|
68 |
actor.handle(e, e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), fName, false); |
|
69 |
} |
9c5db9
|
70 |
} |
U |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
} |