1 22 23 24 package org.jboss.web.rewrite; 25 26 import java.util.Calendar ; 27 28 import org.apache.catalina.connector.Request; 29 30 import org.apache.naming.resources.CacheEntry; 31 import org.apache.naming.resources.ProxyDirContext; 32 import org.apache.tomcat.util.http.FastHttpDateFormat; 33 34 public class TomcatResolver extends Resolver { 35 36 protected Request request = null; 37 38 public TomcatResolver(Request request) { 39 this.request = request; 40 } 41 42 48 public String resolve(String key) { 49 if (key.equals("HTTP_USER_AGENT")) { 50 return request.getHeader("user-agent"); 51 } else if (key.equals("HTTP_REFERER")) { 52 return request.getHeader("referer"); 53 } else if (key.equals("HTTP_COOKIE")) { 54 return request.getHeader("cookie"); 55 } else if (key.equals("HTTP_FORWARDED")) { 56 return request.getHeader("forwarded"); 57 } else if (key.equals("HTTP_HOST")) { 58 String host = request.getHeader("host"); 59 int index = host.indexOf(':'); 60 if (index != -1) 61 host = host.substring(0, index); 62 return host; 63 } else if (key.equals("HTTP_PROXY_CONNECTION")) { 64 return request.getHeader("proxy-connection"); 65 } else if (key.equals("HTTP_ACCEPT")) { 66 return request.getHeader("accept"); 67 } else if (key.equals("REMOTE_ADDR")) { 68 return request.getRemoteAddr(); 69 } else if (key.equals("REMOTE_HOST")) { 70 return request.getRemoteHost(); 71 } else if (key.equals("REMOTE_PORT")) { 72 return String.valueOf(request.getRemotePort()); 73 } else if (key.equals("REMOTE_USER")) { 74 return request.getRemoteUser(); 75 } else if (key.equals("REMOTE_IDENT")) { 76 return request.getRemoteUser(); 77 } else if (key.equals("REQUEST_METHOD")) { 78 return request.getMethod(); 79 } else if (key.equals("SCRIPT_FILENAME")) { 80 return request.getRealPath(request.getServletPath()); } else if (key.equals("REQUEST_PATH")) { 82 return request.getRequestPathMB().toString(); 83 } else if (key.equals("CONTEXT_PATH")) { 84 return request.getContextPath(); 85 } else if (key.equals("SERVLET_PATH")) { 86 return emptyStringIfNull(request.getServletPath()); 87 } else if (key.equals("PATH_INFO")) { 88 return emptyStringIfNull(request.getPathInfo()); 89 } else if (key.equals("QUERY_STRING")) { 90 return emptyStringIfNull(request.getQueryString()); 91 } else if (key.equals("AUTH_TYPE")) { 92 return request.getAuthType(); 93 } else if (key.equals("DOCUMENT_ROOT")) { 94 return request.getRealPath("/"); 95 } else if (key.equals("SERVER_NAME")) { 96 return request.getLocalName(); 97 } else if (key.equals("SERVER_ADDR")) { 98 return request.getLocalAddr(); 99 } else if (key.equals("SERVER_PORT")) { 100 return String.valueOf(request.getLocalPort()); 101 } else if (key.equals("SERVER_PROTOCOL")) { 102 return request.getProtocol(); 103 } else if (key.equals("SERVER_SOFTWARE")) { 104 return "tomcat"; 105 } else if (key.equals("THE_REQUEST")) { 106 return request.getMethod() + " " + request.getRequestURI() 107 + " " + request.getProtocol(); 108 } else if (key.equals("REQUEST_URI")) { 109 return request.getRequestURI(); 110 } else if (key.equals("REQUEST_FILENAME")) { 111 return request.getPathTranslated(); 112 } else if (key.equals("HTTPS")) { 113 return request.isSecure() ? "on" : "off"; 114 } else if (key.equals("TIME_YEAR")) { 115 return String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); 116 } else if (key.equals("TIME_MON")) { 117 return String.valueOf(Calendar.getInstance().get(Calendar.MONTH)); 118 } else if (key.equals("TIME_DAY")) { 119 return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); 120 } else if (key.equals("TIME_HOUR")) { 121 return String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); 122 } else if (key.equals("TIME_MIN")) { 123 return String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)); 124 } else if (key.equals("TIME_SEC")) { 125 return String.valueOf(Calendar.getInstance().get(Calendar.SECOND)); 126 } else if (key.equals("TIME_WDAY")) { 127 return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)); 128 } else if (key.equals("TIME")) { 129 return FastHttpDateFormat.getCurrentDate(); 130 } 131 return null; 132 } 133 134 public String resolveEnv(String key) { 135 return System.getProperty(key); 136 } 137 138 public String resolveSsl(String key) { 139 return null; 141 } 142 143 public String resolveHttp(String key) { 144 return request.getHeader(key); 145 } 146 147 public boolean resolveResource(int type, String name) { 148 ProxyDirContext resources = (ProxyDirContext) request.getContext().getResources(); 149 CacheEntry cacheEntry = resources.lookupCache(name); 150 if (!cacheEntry.exists) { 151 return false; 152 } else { 153 switch (type) { 154 case 0: 155 return (cacheEntry.resource == null); 156 case 1: 157 return (cacheEntry.resource != null); 158 case 2: 159 return (cacheEntry.resource != null 160 && cacheEntry.attributes.getContentLength() > 0); 161 default: 162 return false; 163 } 164 } 165 } 166 167 private static final String emptyStringIfNull(String value) { 168 if (value == null) { 169 return ""; 170 } else { 171 return value; 172 } 173 } 174 175 } 176 | Popular Tags |