1 64 65 package com.jcorporate.expresso.core.servlet; 66 67 import com.jcorporate.expresso.core.misc.ConfigManager; 68 import com.jcorporate.expresso.core.misc.ConfigPathFixedParam; 69 import com.jcorporate.expresso.core.misc.ConfigPathMapping; 70 import com.jcorporate.expresso.core.misc.ConfigPathParam; 71 import com.jcorporate.expresso.core.misc.ConfigurationException; 72 import org.apache.log4j.Logger; 73 import org.apache.oro.text.regex.Pattern; 74 import org.apache.oro.text.regex.PatternMatcher; 75 import org.apache.oro.text.regex.Perl5Matcher; 76 77 import javax.servlet.RequestDispatcher ; 78 import javax.servlet.ServletException ; 79 import javax.servlet.http.HttpServletRequest ; 80 import javax.servlet.http.HttpServletResponse ; 81 import java.io.IOException ; 82 import java.util.Enumeration ; 83 import java.util.StringTokenizer ; 84 import java.util.Vector ; 85 86 87 92 public class PathHandler 93 extends DBServlet { 94 private static Logger log = Logger.getLogger(PathHandler.class); 95 private static PatternMatcher matcher = new Perl5Matcher(); 96 97 public PathHandler() { 98 } 99 100 103 private static void setupLog() { 104 } 105 106 112 private void process(HttpServletRequest req, HttpServletResponse res) 113 throws ServletException , IOException { 114 String originalPath = req.getPathInfo(); 115 116 try { 117 for (Enumeration e = ConfigManager.getContext(getDBName(req)).getPathMappings().elements(); 118 e.hasMoreElements();) { 119 ConfigPathMapping oneMapping = (ConfigPathMapping) e.nextElement(); 120 if (log.isDebugEnabled()) { 121 log.debug("Mapping:" + oneMapping.getUrlPattern() + ", " + 122 oneMapping.getPath()); 123 } 124 125 if (log.isDebugEnabled()) { 126 for (Enumeration ep = oneMapping.getParams().elements(); 127 ep.hasMoreElements();) { 128 ConfigPathParam oneParam = (ConfigPathParam) ep. 129 nextElement(); 130 log.debug("-- Param:" + oneParam.getNumber() + ", " + 131 oneParam.getName()); 132 } 133 } 134 135 if (log.isDebugEnabled()) { 136 for (Enumeration epf = oneMapping.getFixedParams().elements(); 137 epf.hasMoreElements();) { 138 ConfigPathFixedParam oneFixedParam = (ConfigPathFixedParam) epf.nextElement(); 139 log.debug("-- Fixed:" + oneFixedParam.getName() + ", " + 140 oneFixedParam.getValue()); 141 } 142 } 143 144 Pattern p = oneMapping.getCompiledUrlPattern(); 145 146 boolean result; 147 synchronized (matcher) { 148 result = matcher.matches(originalPath, p); 149 } 150 151 if (result) { 152 log.debug("Path Matches!"); 153 redirect(req, res, oneMapping); 154 } 155 } 156 } catch (ConfigurationException ce) { 157 throw new ServletException (ce.getMessage()); 158 } 159 } 160 161 private void redirect(HttpServletRequest req, HttpServletResponse res, 162 ConfigPathMapping mapping) 163 throws ServletException , IOException { 164 String queryString = req.getQueryString(); 165 String originalPath = req.getPathInfo(); 166 167 if (log.isDebugEnabled()) { 168 log.debug("Path:" + originalPath); 169 log.debug("Query:" + queryString); 170 } 171 172 Vector params = new Vector (); 173 StringTokenizer stk = new StringTokenizer (originalPath, "/"); 174 175 while (stk.hasMoreElements()) { 176 params.addElement(stk.nextElement()); 177 } 178 179 ConfigPathParam oneParam = null; 180 ConfigPathFixedParam oneFixedParam = null; 181 int numberNeeded = 0; 182 boolean needAmpersand = false; 183 StringBuffer newURL = new StringBuffer (mapping.getPath() + "?"); 184 185 for (Enumeration ep = mapping.getParams().elements(); 186 ep.hasMoreElements();) { 187 oneParam = (ConfigPathParam) ep.nextElement(); 188 189 if (needAmpersand) { 190 newURL.append("&"); 191 } 192 193 numberNeeded = oneParam.getNumber(); 194 195 if (numberNeeded <= params.size()) { 196 newURL.append(oneParam.getName() + "=" + 197 params.elementAt(numberNeeded)); 198 } 199 200 needAmpersand = true; 201 } 202 for (Enumeration efp = mapping.getFixedParams().elements(); 203 efp.hasMoreElements();) { 204 if (needAmpersand) { 205 newURL.append("&"); 206 } 207 208 oneFixedParam = (ConfigPathFixedParam) efp.nextElement(); 209 newURL.append(oneFixedParam.getName() + "=" + 210 oneFixedParam.getValue()); 211 } 212 213 214 log.debug("New URL:" + newURL); 215 216 RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher(newURL.toString()); 217 218 if (rd == null) { 219 throw new ServletException ("Can't forward"); 220 } 221 222 rd.forward(req, res); 223 } 224 225 229 public void doGet(HttpServletRequest request, HttpServletResponse response) 230 throws ServletException , IOException { 231 super.doGet(request, response); 232 setupLog(); 233 process(request, response); 234 } 235 236 237 241 public void doPost(HttpServletRequest request, 242 HttpServletResponse response) 243 throws ServletException , IOException { 244 super.doGet(request, response); 245 setupLog(); 246 process(request, response); 247 } 248 249 250 } | Popular Tags |