KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > php > Handler


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 package org.jboss.web.php;
24
25 import java.io.IOException JavaDoc;
26
27 import javax.servlet.ServletConfig JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.UnavailableException JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.catalina.Globals;
35 import org.apache.catalina.util.StringManager;
36
37 /**
38  * Handler.
39  *
40  * @author Mladen Turk
41  * @version $Revision: 4721 $, $Date: 2006-06-11 00:28:06 +0200 (dim., 11 juin 2006) $
42  * @since 1.0
43  */

44 public class Handler extends HttpServlet JavaDoc
45 {
46
47     /** the debugging detail level for this servlet. */
48     private int debug = 0;
49
50     /** Buffer size. */
51     private int bufferSize = 4096;
52
53     /**
54      * The Servlet configuration object we are associated with. If this value
55      * is null, this filter instance is not currently configured.
56      */

57     private ServletConfig JavaDoc servletConfig = null;
58
59     /**
60      * The string manager for this package.
61      */

62     private StringManager sm =
63         StringManager.getManager(Constants.Package);
64
65
66     /** Are doing source sysntax highlight. */
67     protected boolean syntaxHighlight = false;
68
69     /** the encoding to use for parameters */
70     private String JavaDoc parameterEncoding = System.getProperty("file.encoding",
71                                                           "UTF-8");
72
73     /**
74      * The Script search path will start at
75      * webAppRootDir + File.separator + scriptPathPrefix
76      * (or webAppRootDir alone if scriptPathPrefix is
77      * null)
78      */

79     private String JavaDoc scriptPathPrefix = null;
80
81     /**
82      * Sets instance variables.
83      * <P>
84      * Modified from Craig R. McClanahan's InvokerServlet
85      * </P>
86      *
87      * @param config a <code>ServletConfig</code> object
88      * containing the servlet's
89      * configuration and initialization
90      * parameters
91      *
92      * @exception ServletException if an exception has occurred that
93      * interferes with the servlet's normal
94      * operation
95      */

96     public void init(ServletConfig JavaDoc servletConfig)
97         throws ServletException JavaDoc
98     {
99         super.init(servletConfig);
100
101         if (!Library.isInitialized()) {
102             // try to load the library.
103
try {
104                 Library.initialize(null);
105             } catch(Exception JavaDoc e) {
106                 e.printStackTrace();
107             }
108         }
109
110         if (!Library.isInitialized())
111             throw new UnavailableException JavaDoc
112                 (sm.getString("handler.missing"));
113
114         this.servletConfig = servletConfig;
115
116         // Verify that we were not accessed using the invoker servlet
117
String JavaDoc servletName = servletConfig.getServletName();
118         if (servletName == null)
119             servletName = "";
120         if (servletName.startsWith("org.apache.catalina.INVOKER."))
121             throw new UnavailableException JavaDoc
122                 ("Cannot invoke Handler through the invoker");
123
124
125         // Set our properties from the initialization parameters
126
String JavaDoc value = null;
127         try {
128             value = servletConfig.getInitParameter("debug");
129             debug = Integer.parseInt(value);
130             scriptPathPrefix =
131                 servletConfig.getInitParameter("scriptPathPrefix");
132             value = servletConfig.getInitParameter("bufferSize");
133             if (value != null) {
134                 bufferSize = Integer.parseInt(value);
135                 if (bufferSize < 1024)
136                     bufferSize = 1024;
137                 log("init: bufferSize set to " + bufferSize);
138             }
139         } catch (Throwable JavaDoc t) {
140             // Nothing.
141
}
142         log("init: loglevel set to " + debug);
143
144         value = servletConfig.getInitParameter("parameterEncoding");
145         if (value != null) {
146             parameterEncoding = value;
147         }
148     }
149
150     /**
151      * Finalize this servlet.
152      */

153     public void destroy()
154     {
155         this.servletConfig = null;
156     }
157
158     private static native int php(byte[] buf,
159                                   ScriptEnvironment env,
160                                   HttpServletRequest JavaDoc req,
161                                   HttpServletResponse JavaDoc res,
162                                   String JavaDoc requestMethod,
163                                   String JavaDoc queryString,
164                                   String JavaDoc contentType,
165                                   String JavaDoc authUser,
166                                   String JavaDoc requestURI,
167                                   String JavaDoc pathTranslated,
168                                   int contentLength,
169                                   boolean syntaxHighlight);
170
171     /**
172      * Provides PHP Gateway service
173      *
174      * @param req HttpServletRequest passed in by servlet container
175      * @param res HttpServletResponse passed in by servlet container
176      *
177      * @exception ServletException if a servlet-specific exception occurs
178      * @exception IOException if a read/write exception occurs
179      *
180      * @see javax.servlet.http.HttpServlet
181      *
182      */

183     protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
184         throws ServletException JavaDoc, IOException JavaDoc
185     {
186
187         // Verify that we were not accessed using the invoker servlet
188
if (req.getAttribute(Globals.INVOKED_ATTR) != null)
189             throw new UnavailableException JavaDoc
190                 ("Cannot invoke PHP Getaway Handler through the invoker");
191
192         ScriptEnvironment env = new ScriptEnvironment(req,
193                                                       getServletContext(),
194                                                       scriptPathPrefix);
195         if (env.isValid()) {
196             byte[] buf = new byte[bufferSize];
197             int rv = php(buf,
198                          env,
199                          req,
200                          res,
201                          req.getMethod(),
202                          req.getQueryString(),
203                          req.getContentType(),
204                          req.getRemoteUser(),
205                          req.getRequestURI(),
206                          env.getFullPath(),
207                          req.getContentLength(),
208                          syntaxHighlight);
209         }
210         else {
211             res.setStatus(HttpServletResponse.SC_NOT_FOUND);
212         }
213     }
214     
215     public static void log(Handler handler, String JavaDoc msg)
216     {
217         // TODO: Log the message
218
}
219 }
220
Popular Tags