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