1 44 45 88 package org.jpublish.servlet; 89 90 import java.io.IOException ; 91 import java.util.*; 92 93 import javax.servlet.RequestDispatcher ; 94 import javax.servlet.ServletException ; 95 import javax.servlet.http.HttpServletRequest ; 96 import javax.servlet.http.HttpServletResponse ; 97 98 import com.anthonyeden.lib.config.Configuration; 99 import com.anthonyeden.lib.config.ConfigurationException; 100 import org.apache.commons.logging.Log; 101 import org.apache.commons.logging.LogFactory; 102 import org.jpublish.SiteContext; 103 import org.jpublish.util.PathUtilities; 104 105 108 public class PathDispatcher { 109 110 private static Log log = LogFactory.getLog(PathDispatcher.class); 111 private static List includeDispatchers; 112 private static List forwardDispatchers; 113 114 private SiteContext siteContext; 115 116 121 public PathDispatcher(SiteContext siteContext) { 122 includeDispatchers = new ArrayList(); 123 forwardDispatchers = new ArrayList(); 124 this.siteContext = siteContext; 126 } 127 128 134 public void loadConfiguration(Configuration configuration) 135 throws ConfigurationException { 136 137 Iterator pathDispatcherElements = configuration.getChildren("path-dispatcher").iterator(); 139 140 while (pathDispatcherElements.hasNext()) { 141 Configuration pathDispatcherElement = 142 (Configuration) pathDispatcherElements.next(); 143 String name = pathDispatcherElement.getAttribute("name"); 144 String path = pathDispatcherElement.getAttribute("path"); 145 146 String action = pathDispatcherElement.getAttribute("action"); 147 if ("forward".equalsIgnoreCase(action)) { 148 forwardDispatchers.add(pathDispatcherElement); 149 } else { 150 includeDispatchers.add(pathDispatcherElement); 151 } 152 153 if (log.isDebugEnabled()) { 154 log.debug("Defined `" + action + "` action for path: " + 155 path + ", to be dispatched by: [" + name + "]"); 156 } 157 } 158 } 159 160 166 public boolean canDispatch(String path) { 167 return canForwardDispatch(path) != null || canIncludeDispatch(path) != null; 168 } 169 170 176 public String canForwardDispatch(String path) { 177 Iterator configurations = forwardDispatchers.iterator(); 178 while (configurations.hasNext()) { 179 Configuration configuration = (Configuration) configurations.next(); 180 if ("true".equals(configuration.getAttribute("regex"))) { 181 } else if (PathUtilities.match(path, configuration.getAttribute("path"))) { 183 return configuration.getAttribute("name"); 184 } 185 } 186 return null; 187 } 188 189 195 public String canIncludeDispatch(String path) { 196 Iterator configurations = includeDispatchers.iterator(); 197 while (configurations.hasNext()) { 198 Configuration configuration = (Configuration) configurations.next(); 199 if ("true".equals(configuration.getAttribute("regex"))) { 200 } else if (PathUtilities.match(path, configuration.getAttribute("path"))) { 202 return configuration.getAttribute("name"); 203 } 204 } 205 return null; 206 } 207 208 217 public void forward(HttpServletRequest request, HttpServletResponse response, String dispatcherName) 218 throws IOException , ServletException { 219 if (dispatcherName != null) { 220 RequestDispatcher rd = siteContext.getServletContext().getNamedDispatcher(dispatcherName); 221 rd.forward(request, response); 222 } 223 } 224 225 240 public void include(HttpServletRequest request, HttpServletResponse response, String dispatcherName) 241 throws IOException , ServletException { 242 if (dispatcherName != null) { 243 RequestDispatcher rd = siteContext.getServletContext().getNamedDispatcher(dispatcherName); 244 rd.include(request, response); 245 } 247 } 248 249 public void dispatch(HttpServletRequest request, HttpServletResponse response, String path) 250 throws IOException , ServletException { 251 } 253 254 256 275 public static int countMatches(String str, String sub) { 276 if (str == null || str.length() == 0 || sub == null || sub.length() == 0) { 277 return 0; 278 } 279 int count = 0; 280 int idx = 0; 281 while ((idx = str.indexOf(sub, idx)) != -1) { 282 count++; 283 idx += sub.length(); 284 } 285 return count; 286 } 287 288 } | Popular Tags |