KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > ssi > VarExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.servlets.ssi;
31
32 import com.caucho.util.Alarm;
33 import com.caucho.util.IntMap;
34 import com.caucho.util.QDate;
35 import com.caucho.vfs.Path;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 /**
41  * Represents a SSI variable
42  */

43 public class VarExpr extends SSIExpr {
44   private static final int ATTRIBUTE = 0;
45   private static final int HTTP_ = ATTRIBUTE + 1;
46   
47   private static final int SERVER_SOFTWARE = HTTP_ + 1;
48   private static final int SERVER_NAME = SERVER_SOFTWARE + 1;
49   private static final int SERVER_ADDR = SERVER_NAME + 1;
50   private static final int SERVER_PORT = SERVER_ADDR + 1;
51   private static final int REMOTE_ADDR = SERVER_PORT + 1;
52   private static final int REMOTE_PORT = REMOTE_ADDR + 1;
53   private static final int REMOTE_USER = REMOTE_PORT + 1;
54   private static final int AUTH_TYPE = REMOTE_USER + 1;
55   private static final int GATEWAY_INTERFACE = AUTH_TYPE + 1;
56   private static final int SERVER_PROTOCOL = GATEWAY_INTERFACE + 1;
57   private static final int REQUEST_METHOD = SERVER_PROTOCOL + 1;
58   private static final int QUERY_STRING = REQUEST_METHOD + 1;
59   private static final int REQUEST_URI = QUERY_STRING + 1;
60   private static final int SCRIPT_FILENAME = REQUEST_URI + 1;
61   private static final int SCRIPT_NAME = SCRIPT_FILENAME + 1;
62   private static final int PATH_INFO = SCRIPT_NAME + 1;
63   private static final int PATH_TRANSLATED = PATH_INFO + 1;
64   private static final int CONTENT_LENGTH = PATH_TRANSLATED + 1;
65   private static final int CONTENT_TYPE = CONTENT_LENGTH + 1;
66   
67   private static final int DATE_GMT = CONTENT_TYPE + 1;
68   private static final int DATE_LOCAL = DATE_GMT + 1;
69   private static final int DOCUMENT_NAME = DATE_LOCAL + 1;
70   private static final int DOCUMENT_URI = DOCUMENT_NAME + 1;
71   private static final int LAST_MODIFIED = DOCUMENT_URI + 1;
72   private static final int USER_NAME = LAST_MODIFIED + 1;
73
74   private static final IntMap _varMap = new IntMap();
75   
76   private final int _code;
77   
78   private final String JavaDoc _var;
79
80   private final Path _path;
81
82   VarExpr(String JavaDoc var, Path path)
83   {
84     int code = _varMap.get(var.toLowerCase());
85
86     if (code > 0) {
87     }
88     else if (var.startsWith("HTTP_")) {
89       var = var.substring(5).replace('_', '-').toLowerCase();
90       code = HTTP_;
91     }
92     else
93       code = ATTRIBUTE;
94     
95     _code = code;
96     _var = var;
97
98     _path = path;
99   }
100
101   /**
102    * Evaluate as a string.
103    */

104   public String JavaDoc evalString(HttpServletRequest JavaDoc request,
105                HttpServletResponse JavaDoc response)
106   {
107     String JavaDoc fmt;
108     String JavaDoc value = null;
109     
110     switch (_code) {
111     case ATTRIBUTE:
112       value = String.valueOf(request.getAttribute(_var));
113       break;
114
115     case HTTP_:
116       value = request.getHeader(_var);
117       break;
118
119     case SERVER_SOFTWARE:
120       value = "Resin/" + com.caucho.Version.VERSION;
121       break;
122
123     case SERVER_NAME:
124     case SERVER_ADDR:
125       value = request.getServerName();
126       break;
127
128     case SERVER_PORT:
129       value = String.valueOf(request.getServerPort());
130       break;
131
132     case REMOTE_ADDR:
133       value = request.getServerName();
134       break;
135
136     case REMOTE_PORT:
137       value = String.valueOf(request.getServerPort());
138       break;
139
140     case REMOTE_USER:
141       value = request.getRemoteUser();
142       break;
143
144     case AUTH_TYPE:
145       value = request.getAuthType();
146       break;
147
148     case GATEWAY_INTERFACE:
149       value = "CGI/1.1";
150       break;
151
152     case SERVER_PROTOCOL:
153       value = request.getProtocol();
154       break;
155
156     case REQUEST_METHOD:
157       value = request.getMethod();
158       break;
159
160     case QUERY_STRING:
161       value = request.getQueryString();
162       break;
163
164     case REQUEST_URI:
165       value = request.getRequestURI();
166       break;
167
168     case PATH_INFO:
169       value = request.getPathInfo();
170       break;
171
172     case PATH_TRANSLATED:
173       value = request.getRealPath(request.getPathInfo());
174       break;
175
176     case CONTENT_LENGTH:
177       value = String.valueOf(request.getContentLength());
178       break;
179
180     case CONTENT_TYPE:
181       value = request.getHeader("Content-Type");
182       break;
183
184     case DATE_GMT:
185       fmt = (String JavaDoc) request.getAttribute("caucho.ssi.timefmt");
186       if (fmt == null)
187     fmt = "%Y-%m-%d %H:%M:%S";
188       value = QDate.formatGMT(Alarm.getCurrentTime(), fmt);
189       break;
190
191     case DATE_LOCAL:
192       fmt = (String JavaDoc) request.getAttribute("caucho.ssi.timefmt");
193       if (fmt == null)
194     fmt = "%Y-%m-%d %H:%M:%S";
195       
196       value = QDate.formatLocal(Alarm.getCurrentTime(), fmt);
197       break;
198
199     case DOCUMENT_NAME:
200       value = _path.getTail();
201       break;
202
203     case DOCUMENT_URI:
204       value = request.getRequestURI();
205       break;
206
207     case LAST_MODIFIED:
208       fmt = (String JavaDoc) request.getAttribute("caucho.ssi.timefmt");
209       if (fmt == null)
210     fmt = "%Y-%m-%d %H:%M:%S";
211       
212       value = QDate.formatLocal(_path.getLastModified(), fmt);
213       break;
214
215     case USER_NAME:
216     default:
217       break;
218     }
219
220     if (value != null)
221       return value;
222     else
223       return "(null)";
224   }
225
226   static {
227     _varMap.put("server_software", SERVER_SOFTWARE);
228     _varMap.put("server_name", SERVER_NAME);
229     _varMap.put("server_addr", SERVER_ADDR);
230     _varMap.put("server_port", SERVER_PORT);
231     _varMap.put("remote_addr", REMOTE_ADDR);
232     _varMap.put("remote_port", REMOTE_PORT);
233     _varMap.put("remote_user", REMOTE_USER);
234     _varMap.put("auth_type", AUTH_TYPE);
235     _varMap.put("gateway_interface", GATEWAY_INTERFACE);
236     _varMap.put("server_protocol", SERVER_PROTOCOL);
237     _varMap.put("request_method", REQUEST_METHOD);
238     _varMap.put("query_string", QUERY_STRING);
239     _varMap.put("request_uri", REQUEST_URI);
240     _varMap.put("script_filename", SCRIPT_FILENAME);
241     _varMap.put("script_name", SCRIPT_NAME);
242     _varMap.put("path_info", PATH_INFO);
243     _varMap.put("path_translated", PATH_TRANSLATED);
244     _varMap.put("content_length", CONTENT_LENGTH);
245     _varMap.put("content_type", CONTENT_TYPE);
246     
247     _varMap.put("date_gmt", DATE_GMT);
248     _varMap.put("date_local", DATE_LOCAL);
249     _varMap.put("document_name", DOCUMENT_NAME);
250     _varMap.put("document_uri", DOCUMENT_URI);
251     _varMap.put("last_modified", LAST_MODIFIED);
252     _varMap.put("user_name", USER_NAME);
253   }
254 }
255
Popular Tags