1 16 package org.mortbay.http.handler; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import org.apache.commons.logging.Log; 22 import org.mortbay.log.LogFactory; 23 import org.mortbay.http.HttpException; 24 import org.mortbay.http.HttpMessage; 25 import org.mortbay.http.HttpRequest; 26 import org.mortbay.http.HttpResponse; 27 import org.mortbay.http.PathMap; 28 import org.mortbay.util.URI; 29 import org.mortbay.util.UrlEncoded; 30 31 32 33 38 public class ForwardHandler extends AbstractHttpHandler 39 { 40 private static Log log = LogFactory.getLog(ForwardHandler.class); 41 42 PathMap _forward = new PathMap(); 43 String _root; 44 boolean _handleQueries = false; 45 46 47 49 public ForwardHandler() 50 {} 51 52 53 56 public ForwardHandler(String rootForward) 57 { 58 _root=rootForward; 59 } 60 61 62 66 public void addForward(String pathSpecInContext, 67 String newPath) 68 { 69 _forward.put(pathSpecInContext,newPath); 70 } 71 72 73 78 public void setRootForward(String newPath) 79 { 80 _root=newPath; 81 } 82 83 84 88 public void setHandleQueries(boolean b) 89 { 90 _handleQueries = b; 91 } 92 93 94 public void handle(String pathInContext, 95 String pathParams, 96 HttpRequest request, 97 HttpResponse response) 98 throws HttpException, IOException 99 { 100 if(log.isTraceEnabled())log.trace("Look for "+pathInContext+" in "+_forward); 101 102 String newPath=null; 103 String query=null; 104 if (_root!=null && ("/".equals(pathInContext) || pathInContext.startsWith("/;"))) 105 newPath=_root; 106 else 107 { 108 Map.Entry entry = _forward.getMatch(pathInContext); 109 if (entry!=null) 110 { 111 String match = (String )entry.getValue(); 112 if (_handleQueries) 113 { 114 int hook = match.indexOf('?'); 115 if (hook != -1){ 116 query = match.substring(hook+1); 117 match = match.substring(0, hook); 118 } 119 } 120 String info=PathMap.pathInfo((String )entry.getKey(),pathInContext); 121 if(log.isDebugEnabled())log.debug("Forward: match:\""+ match+ "\" info:"+info+"\" query:"+query); 122 newPath=info==null?match:(URI.addPaths(match,info)); 123 } 124 } 125 126 if (newPath!=null) 127 { 128 if(log.isDebugEnabled())log.debug("Forward from "+pathInContext+" to "+newPath); 129 130 int last=request.setState(HttpMessage.__MSG_EDITABLE); 131 String context=getHttpContext().getContextPath(); 132 if (context.length()==1) 133 request.setPath(newPath); 134 else 135 request.setPath(URI.addPaths(context,newPath)); 136 if (_handleQueries && query != null){ 137 UrlEncoded.decodeTo(query, request.getParameters()); 139 } 140 request.setState(last); 141 getHttpContext().getHttpServer().service(request,response); 142 return; 143 } 144 } 145 } 146 | Popular Tags |