KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > rewrite > TomcatResolver


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23
24 package org.jboss.web.rewrite;
25
26 import java.util.Calendar JavaDoc;
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     /**
43      * The following are not implemented:
44      * - SERVER_ADMIN
45      * - API_VERSION
46      * - IS_SUBREQ
47      */

48     public String JavaDoc resolve(String JavaDoc 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 JavaDoc 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()); //FIXME ?
81
} 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 JavaDoc resolveEnv(String JavaDoc key) {
135         return System.getProperty(key);
136     }
137     
138     public String JavaDoc resolveSsl(String JavaDoc key) {
139         // FIXME: Implement SSL environment variables
140
return null;
141     }
142
143     public String JavaDoc resolveHttp(String JavaDoc key) {
144         return request.getHeader(key);
145     }
146     
147     public boolean resolveResource(int type, String JavaDoc 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 JavaDoc emptyStringIfNull(String JavaDoc value) {
168         if (value == null) {
169             return "";
170         } else {
171             return value;
172         }
173     }
174
175 }
176
Popular Tags