Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 23abc65d25cba29663f23239bfbca8e21bb298b2
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 import java.util.logging.Logger;
31 import java.util.logging.Level;
32
33 /**
34  * Der DelegateActor delegiert die Bearbeitung einer HTTP-Anfrage an einen 
35  * HttpHandler. Zu einem bestimmten 
36  * Muster passende URLs werden an den zum Muster hinterlegten HttpHandler 
37  * weitergegeben.
38  * 
39  * @author Ulrich Hilger
40  * @version 1, 03.07.2021
41  */
42 public class DelegateActor {
43
44   /** Der Logger dieser Klasse */
45   private static final Logger logger = Logger.getLogger(DelegateActor.class.getName());
46   
47   public boolean handle(HttpExchange exchange, Map<String, HandlerDescriptor>  handlers) throws IOException {
48     String path = exchange.getRequestURI().getPath();
49     logger.fine("path: " + path);
50     Set keys = handlers.keySet();
51     Iterator<String> i = keys.iterator();
52     boolean noMatch = true;
53     while(i.hasNext() && noMatch) {
54       String regex = i.next();
55       logger.fine("regex: " + regex);
56       if(path.matches(regex)) {
57         try {
58           noMatch = false;
59           logger.fine("match");
60           //HttpHandler handler = handlers.get(regex);
61           HandlerDescriptor hd = handlers.get(regex);
62           String handlerClass = hd.getHandlerClassName();
63           Object o = Class.forName(handlerClass).getConstructors()[0].newInstance();
64           if(o instanceof AttributeHandler) {
65             AttributeHandler handler = (AttributeHandler) o;
66             Map<String, String> attrs = hd.getAttributes();
67             Iterator<String> it = attrs.keySet().iterator();
68             while(it.hasNext()) {
69               String key = it.next();
70               String value = attrs.get(key);
71               handler.setAttribute(key, value);
72             }
73             handler.handle(exchange);      
74           } else if(o instanceof HttpHandler) {
75             HttpHandler handler = (HttpHandler) o;
76             handler.handle(exchange);      
77           }
78         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
79           Logger.getLogger(PatternDelegator.class.getName()).log(Level.SEVERE, null, ex);
80         }
81       }
82     }
83     return noMatch;
84   }
85   
86 }