commit | author | age
|
23abc6
|
1 |
/* |
U |
2 |
http-base - 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.base.actor; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
|
21 |
import com.sun.net.httpserver.HttpHandler; |
|
22 |
import de.uhilger.httpserver.base.handler.AttributeHandler; |
|
23 |
import de.uhilger.httpserver.base.handler.HandlerDescriptor; |
|
24 |
import de.uhilger.httpserver.base.handler.PatternDelegator; |
|
25 |
import java.io.IOException; |
|
26 |
import java.lang.reflect.InvocationTargetException; |
|
27 |
import java.util.Iterator; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.Set; |
|
30 |
|
|
31 |
/** |
|
32 |
* Der DelegateActor delegiert die Bearbeitung einer HTTP-Anfrage an einen |
|
33 |
* HttpHandler. Zu einem bestimmten |
|
34 |
* Muster passende URLs werden an den zum Muster hinterlegten HttpHandler |
|
35 |
* weitergegeben. |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
|
38 |
* @version 1, 03.07.2021 |
|
39 |
*/ |
|
40 |
public class DelegateActor { |
|
41 |
|
|
42 |
public boolean handle(HttpExchange exchange, Map<String, HandlerDescriptor> handlers) throws IOException { |
|
43 |
String path = exchange.getRequestURI().getPath(); |
dd1db1
|
44 |
//logger.fine("path: " + path); |
23abc6
|
45 |
Set keys = handlers.keySet(); |
U |
46 |
Iterator<String> i = keys.iterator(); |
|
47 |
boolean noMatch = true; |
|
48 |
while(i.hasNext() && noMatch) { |
|
49 |
String regex = i.next(); |
dd1db1
|
50 |
//logger.fine("regex: " + regex); |
23abc6
|
51 |
if(path.matches(regex)) { |
U |
52 |
try { |
|
53 |
noMatch = false; |
dd1db1
|
54 |
//logger.fine("match"); |
23abc6
|
55 |
//HttpHandler handler = handlers.get(regex); |
U |
56 |
HandlerDescriptor hd = handlers.get(regex); |
|
57 |
String handlerClass = hd.getHandlerClassName(); |
|
58 |
Object o = Class.forName(handlerClass).getConstructors()[0].newInstance(); |
|
59 |
if(o instanceof AttributeHandler) { |
|
60 |
AttributeHandler handler = (AttributeHandler) o; |
|
61 |
Map<String, String> attrs = hd.getAttributes(); |
|
62 |
Iterator<String> it = attrs.keySet().iterator(); |
|
63 |
while(it.hasNext()) { |
|
64 |
String key = it.next(); |
|
65 |
String value = attrs.get(key); |
|
66 |
handler.setAttribute(key, value); |
|
67 |
} |
|
68 |
handler.handle(exchange); |
|
69 |
} else if(o instanceof HttpHandler) { |
|
70 |
HttpHandler handler = (HttpHandler) o; |
|
71 |
handler.handle(exchange); |
|
72 |
} |
|
73 |
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { |
dd1db1
|
74 |
//Logger.getLogger(PatternDelegator.class.getName()).log(Level.SEVERE, null, ex); |
23abc6
|
75 |
} |
U |
76 |
} |
|
77 |
} |
|
78 |
return noMatch; |
|
79 |
} |
|
80 |
|
|
81 |
} |