KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > ServerArrayValue


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.quercus.env;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import java.io.IOException JavaDoc;
35 import java.io.NotSerializableException JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 /**
43  * Represents the server
44  */

45 public class ServerArrayValue extends ArrayValueImpl {
46   private static final StringValue SERVER_NAME_V
47     = new StringValueImpl("SERVER_NAME");
48   private static final StringValue SERVER_PORT_V
49     = new StringValueImpl("SERVER_PORT");
50   private static final StringValue REMOTE_HOST_V
51     = new StringValueImpl("REMOTE_HOST");
52   private static final StringValue REMOTE_ADDR_V
53     = new StringValueImpl("REMOTE_ADDR");
54   private static final StringValue REMOTE_PORT_V
55     = new StringValueImpl("REMOTE_PORT");
56   
57   private static final StringValue DOCUMENT_ROOT_V
58     = new StringValueImpl("DOCUMENT_ROOT");
59   
60   private static final StringValue SERVER_PROTOCOL_V
61     = new StringValueImpl("SERVER_PROTOCOL");
62   private static final StringValue REQUEST_METHOD_V
63     = new StringValueImpl("REQUEST_METHOD");
64   private static final StringValue QUERY_STRING_V
65     = new StringValueImpl("QUERY_STRING");
66   
67   private static final StringValue REQUEST_URI_V
68     = new StringValueImpl("REQUEST_URI");
69   private static final StringValue SCRIPT_NAME_V
70     = new StringValueImpl("SCRIPT_NAME");
71   private static final StringValue SCRIPT_FILENAME_V
72     = new StringValueImpl("SCRIPT_FILENAME");
73   private static final StringValue PATH_INFO_V
74     = new StringValueImpl("PATH_INFO");
75   private static final StringValue PATH_TRANSLATED_V
76     = new StringValueImpl("PATH_TRANSLATED");
77   
78   private static final StringValue PHP_SELF_V
79     = new StringValueImpl("PHP_SELF");
80   
81   private static final StringValue HTTPS_V
82     = new StringValueImpl("HTTPS");
83   
84   private static final StringValue HTTP_HOST_V
85     = new StringValueImpl("HTTP_HOST");
86   
87   private final Env _env;
88   
89   private boolean _isFilled;
90
91   public ServerArrayValue(Env env)
92   {
93     _env = env;
94   }
95   
96   /**
97    * Converts to an object.
98    */

99   public Object JavaDoc toObject()
100   {
101     return null;
102   }
103
104   /**
105    * Adds a new value.
106    */

107   public Value put(Value key, Value value)
108   {
109     if (! _isFilled)
110       fillMap();
111
112     return super.put(key, value);
113   }
114
115   /**
116    * Adds a new value.
117    */

118   public Value put(Value value)
119   {
120     if (! _isFilled)
121       fillMap();
122
123     return super.put(value);
124   }
125
126   /**
127    * Gets a new value.
128    */

129   public Value get(Value key)
130   {
131     if (! _isFilled)
132       fillMap();
133
134     return super.get(key);
135   }
136   
137   /**
138    * Returns the array ref.
139    */

140   public Var getRef(Value key)
141   {
142     if (! _isFilled)
143       fillMap();
144     
145     return super.getRef(key);
146   }
147   
148   /**
149    * Copy for assignment.
150    */

151   public Value copy()
152   {
153     if (! _isFilled)
154       fillMap();
155     
156     return new ArrayValueImpl(this);
157   }
158
159   /**
160    * Returns an iterator of the entries.
161    */

162   public Set JavaDoc<Map.Entry JavaDoc<Value,Value>> entrySet()
163   {
164     if (! _isFilled)
165       fillMap();
166     
167     return super.entrySet();
168   }
169
170   /**
171    * Convenience for lib.
172    */

173   public void put(String JavaDoc key, String JavaDoc value)
174   {
175     if (! _isFilled)
176       fillMap();
177
178     super.put(key, value);
179   }
180
181   /**
182    * Prints the value.
183    * @param env
184    */

185   public void print(Env env)
186   {
187     env.print("Array");
188   }
189
190   /**
191    * Fills the map.
192    */

193   private void fillMap()
194   {
195     if (_isFilled)
196       return;
197
198     _isFilled = true;
199
200     for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry: System.getenv().entrySet()) {
201       super.put(new StringValueImpl(entry.getKey()),
202           new StringValueImpl(entry.getValue()));
203     }
204
205     for (Map.Entry JavaDoc<Value,Value> entry:
206         _env.getQuercus().getServerEnvMap().entrySet()) {
207       super.put(entry.getKey(), entry.getValue());
208     }
209     
210     HttpServletRequest JavaDoc request = _env.getRequest();
211
212     if (request != null) {
213       super.put(SERVER_NAME_V,
214                 new StringValueImpl(request.getServerName()));
215
216       super.put(SERVER_PORT_V,
217                 new LongValue(request.getServerPort()));
218       super.put(REMOTE_HOST_V,
219                 new StringValueImpl(request.getRemoteHost()));
220       super.put(REMOTE_ADDR_V,
221                 new StringValueImpl(request.getRemoteAddr()));
222       super.put(REMOTE_PORT_V,
223                 new LongValue(request.getRemotePort()));
224
225       super.put(SERVER_PROTOCOL_V,
226                 new StringValueImpl(request.getProtocol()));
227       super.put(REQUEST_METHOD_V,
228                 new StringValueImpl(request.getMethod()));
229
230       if (request.getQueryString() != null) {
231         super.put(QUERY_STRING_V,
232                   new StringValueImpl(request.getQueryString()));
233       }
234
235       super.put(DOCUMENT_ROOT_V,
236                 new StringValueImpl(request.getRealPath("/")));
237
238       super.put(SCRIPT_NAME_V,
239                 new StringValueImpl(request.getContextPath() +
240                                     request.getServletPath()));
241
242       String JavaDoc requestURI = request.getRequestURI();
243       String JavaDoc queryString = request.getQueryString();
244
245       if (queryString != null)
246         requestURI = requestURI + '?' + queryString;
247
248       super.put(REQUEST_URI_V,
249                 new StringValueImpl(requestURI));
250       super.put(SCRIPT_FILENAME_V,
251                 new StringValueImpl(request.getRealPath(request.getServletPath())));
252
253       if (request.getPathInfo() != null) {
254         super.put(PATH_INFO_V,
255                   new StringValueImpl(request.getPathInfo()));
256         super.put(PATH_TRANSLATED_V,
257                   new StringValueImpl(request.getRealPath(request.getPathInfo())));
258       }
259
260       if (request.isSecure())
261         super.put(HTTPS_V, new StringValueImpl("on"));
262
263       String JavaDoc contextPath = request.getContextPath();
264       String JavaDoc servletPath = request.getServletPath();
265       String JavaDoc pathInfo = request.getPathInfo();
266
267       if (pathInfo == null)
268         super.put(PHP_SELF_V, new StringValueImpl(contextPath + servletPath));
269       else
270         super.put(PHP_SELF_V, new StringValueImpl(contextPath + servletPath + pathInfo));
271
272       Enumeration JavaDoc e = request.getHeaderNames();
273       while (e.hasMoreElements()) {
274         String JavaDoc key = (String JavaDoc) e.nextElement();
275
276         String JavaDoc value = request.getHeader(key);
277
278         if (key.equalsIgnoreCase("Host")) {
279           super.put(HTTP_HOST_V, new StringValueImpl(value));
280         }
281         else {
282           super.put(convertHttpKey(key), new StringValueImpl(value));
283         }
284       }
285     }
286   }
287
288   /**
289    * Converts a header key to HTTP_
290    */

291   private StringValue convertHttpKey(String JavaDoc key)
292   {
293     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
294
295     sb.append("HTTP_");
296
297     int len = key.length();
298     for (int i = 0; i < len; i++) {
299       char ch = key.charAt(i);
300
301       if (Character.isLowerCase(ch))
302     sb.append(Character.toUpperCase(ch));
303       else if (ch == '-')
304     sb.append('_');
305       else
306     sb.append(ch);
307     }
308
309     return new StringValueImpl(sb.toString());
310   }
311   
312   //
313
// Java serialization code
314
//
315

316   private Object JavaDoc writeReplace()
317   {
318     if (! _isFilled)
319       fillMap();
320     
321     return new ArrayValueImpl(this);
322   }
323 }
324
325
Popular Tags