| | |
| | | package de.uhilger.httpserver.base.handler; |
| | | |
| | | import com.sun.net.httpserver.HttpExchange; |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import de.uhilger.httpserver.base.actor.DelegateActor; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void handle(HttpExchange exchange) 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); |
| | | } |
| | | } |
| | | } |
| | | boolean noMatch = new DelegateActor().handle(exchange, handlers); |
| | | if(noMatch) { |
| | | super.handle(exchange); |
| | | } |