KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > ApacheModule


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.lib;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.annotation.Optional;
34 import com.caucho.quercus.env.ArrayValue;
35 import com.caucho.quercus.env.ArrayValueImpl;
36 import com.caucho.quercus.env.Env;
37 import com.caucho.quercus.env.StringValueImpl;
38 import com.caucho.quercus.env.Value;
39 import com.caucho.quercus.module.AbstractQuercusModule;
40 import com.caucho.util.L10N;
41
42 import javax.servlet.http.*;
43 import java.util.Enumeration JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * PHP apache routines.
48  */

49 public class ApacheModule extends AbstractQuercusModule {
50   private static final L10N L = new L10N(ApacheModule.class);
51
52   private static final Logger JavaDoc log =
53     Logger.getLogger(ApacheModule.class.getName());
54
55   /**
56    * Stub for insisting the apache process should terminate.
57    */

58   public boolean apache_child_terminate()
59   {
60     return false;
61   }
62
63   // XXX: apache_get_modules
64
// XXX: apache_get_version
65
// XXX: apache_getenv
66
// XXX: apache_lookup_uri
67

68   /**
69    * Gets and sets apache notes
70    */

71   public String JavaDoc apache_note(Env env,
72                             String JavaDoc name,
73                             @Optional Value value)
74   {
75     HttpServletRequest req = env.getRequest();
76
77     Object JavaDoc oldValue = req.getAttribute(name);
78
79     if (value.isset())
80       req.setAttribute(name, value.toString());
81
82     if (oldValue != null)
83       return oldValue.toString();
84     else
85       return null;
86   }
87
88   /**
89    * Returns all the request headers
90    */

91   public Value apache_request_headers(Env env)
92   {
93     HttpServletRequest req = env.getRequest();
94
95     ArrayValue result = new ArrayValueImpl();
96
97     Enumeration JavaDoc e = req.getHeaderNames();
98
99     while (e.hasMoreElements()) {
100       String JavaDoc key = (String JavaDoc) e.nextElement();
101
102       result.put(new StringValueImpl(key), new StringValueImpl(req.getHeader(key)));
103     }
104
105     return result;
106   }
107
108   // XXX: apache_response_headers
109

110   /**
111    * Stub for resetting the output timeout.
112    */

113   public boolean apache_reset_timeout()
114   {
115     return false;
116   }
117
118   // XXX: apache_setenv
119
// XXX: ascii2ebcdic
120
// XXX: ebcdic2ascii
121

122   /**
123    * Returns all the request headers
124    */

125   public Value getallheaders(Env env)
126   {
127     return apache_request_headers(env);
128   }
129
130   /**
131    * Include request.
132    */

133   public boolean virtual(Env env, String JavaDoc url)
134   {
135     try {
136       HttpServletRequest req = env.getRequest();
137       HttpServletResponse res = env.getResponse();
138
139       // XXX: need to put the output, so the included stream gets the
140
// buffer, too
141
env.getOut().flushBuffer();
142
143       req.getRequestDispatcher(url).include(req, res);
144
145       return true;
146     } catch (RuntimeException JavaDoc e) {
147       throw e;
148     } catch (Exception JavaDoc e) {
149       throw new QuercusModuleException(e);
150     }
151   }
152 }
153
Popular Tags