1 16 package org.directwebremoting.servlet; 17 18 import java.io.IOException ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.directwebremoting.Container; 28 import org.directwebremoting.extend.Handler; 29 import org.directwebremoting.extend.InitializingBean; 30 import org.directwebremoting.util.Logger; 31 32 49 public class UrlProcessor implements Handler, InitializingBean 50 { 51 54 public void afterContainerSetup(Container container) 55 { 56 Collection beanNames = container.getBeanNames(); 57 for (Iterator it = beanNames.iterator(); it.hasNext();) 58 { 59 String name = (String ) it.next(); 60 if (name.startsWith(PathConstants.URL_PREFIX)) 61 { 62 Object bean = container.getBean(name); 63 64 if (bean instanceof Handler) 65 { 66 urlMapping.put(name.substring(PathConstants.URL_PREFIX.length()), bean); 67 } 68 else 69 { 70 log.error("Discarding non Handler for " + name); 71 } 72 } 73 } 74 } 75 76 79 public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException 80 { 81 try 82 { 83 String pathInfo = request.getPathInfo(); 84 85 if (pathInfo == null || pathInfo.length() == 0 || pathInfo.equals("/")) 86 { 87 response.sendRedirect(request.getContextPath() + request.getServletPath() + indexHandlerUrl); 88 } 89 else 90 { 91 for (Iterator it = urlMapping.entrySet().iterator(); it.hasNext();) 93 { 94 Map.Entry entry = (Map.Entry ) it.next(); 95 String url = (String ) entry.getKey(); 96 97 if (pathInfo.startsWith(url)) 99 { 100 Handler handler = (Handler) entry.getValue(); 101 handler.handle(request, response); 102 return; 103 } 104 } 105 106 notFoundHandler.handle(request, response); 107 } 108 } 109 catch (Exception ex) 110 { 111 exceptionHandler.setException(ex); 112 exceptionHandler.handle(request, response); 113 } 114 } 115 116 120 public void setIndexHandlerUrl(String indexHandlerUrl) 121 { 122 this.indexHandlerUrl = indexHandlerUrl; 123 } 124 125 128 private String indexHandlerUrl; 129 130 133 private Map urlMapping = new HashMap (); 134 135 138 private Handler notFoundHandler = new NotFoundHandler(); 139 140 143 private ExceptionHandler exceptionHandler = new ExceptionHandler(); 144 145 148 private static final Logger log = Logger.getLogger(UrlProcessor.class); 149 } 150 | Popular Tags |