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.Filter; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import com.sun.net.httpserver.HttpHandler; |
1b0a18
|
23 |
import de.uhilger.httpserver.base.HttpHelper; |
0de53a
|
24 |
import de.uhilger.httpserver.base.handler.FileHandler; |
9c5db9
|
25 |
import java.io.File; |
U |
26 |
import java.io.IOException; |
|
27 |
import java.net.URI; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* AdocFilter prueft, ob fuer eine |
|
32 |
* Asciidoctor-Quelldatei bereits eine HTML-Version vorliegt. Wenn nicht, |
|
33 |
* verwendet der AdocFilter den AdocActor, um eine HTML-Version zu erzeugen. |
|
34 |
* |
|
35 |
* @author Ulrich Hilger |
|
36 |
* @version 1, 16.06.2021 |
|
37 |
*/ |
|
38 |
public class AdocFilter extends Filter { |
|
39 |
|
|
40 |
private static final Logger logger = Logger.getLogger(AdocFilter.class.getName()); |
|
41 |
|
|
42 |
public static final String DESCRIPTION = "Asciidoctor Filter"; |
|
43 |
|
|
44 |
public static final String ADOC = ".adoc"; |
|
45 |
|
|
46 |
@Override |
|
47 |
public void doFilter(HttpExchange exchange, Chain chain) throws IOException { |
|
48 |
URI uri = exchange.getRequestURI(); |
|
49 |
String query = uri.getQuery(); |
|
50 |
//String[] params = query.split("?&"); // hier noch Regex ermitteln |
|
51 |
String requestPathStr = uri.getPath(); |
|
52 |
//logger.fine("filter: " + requestUriStr); |
|
53 |
|
|
54 |
if(requestPathStr.toLowerCase().endsWith(ADOC)) { |
|
55 |
logger.fine("filter: " + requestPathStr); |
|
56 |
HttpHandler handler = exchange.getHttpContext().getHandler(); |
0de53a
|
57 |
if(handler instanceof FileHandler) { |
U |
58 |
FileHandler fileHandler = (FileHandler) handler; |
90c249
|
59 |
String fileBase = exchange.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(); |
9c5db9
|
60 |
AdocActor actor = new AdocActor(); |
1b0a18
|
61 |
HttpHelper helper = new HttpHelper(); |
U |
62 |
actor.processAdocFile(new File(fileBase, helper.getFileName(exchange)), Boolean.FALSE.toString()); |
9c5db9
|
63 |
} |
5ca813
|
64 |
} |
9c5db9
|
65 |
chain.doFilter(exchange); |
U |
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public String description() { |
|
70 |
return DESCRIPTION; |
|
71 |
} |
|
72 |
|
|
73 |
} |