KickJava   Java API By Example, From Geeks To Geeks.

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


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.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30
31 /**
32  * PHP SAPI interface.
33  *
34  * @author Mladen Turk
35  * @version $Revision: 4354 $, $Date: 2006-05-22 19:53:20 +0200 (lun., 22 mai 2006) $
36  * @since 1.0
37  */

38 public final class SAPI
39 {
40
41     public static int write(HttpServletResponse JavaDoc res,
42                             byte[] buf, int len)
43     {
44         try {
45             res.getOutputStream().write(buf, 0, len);
46             return len;
47         } catch (IOException JavaDoc e) {
48             return -1;
49         }
50     }
51
52     public static int read(HttpServletRequest JavaDoc req,
53                            byte[] buf, int len)
54     {
55         try {
56             return req.getInputStream().read(buf, 0, len);
57         } catch (IOException JavaDoc e) {
58             return -1;
59         }
60     }
61
62     public static void log(Handler h, String JavaDoc msg)
63     {
64         h.log("php: " + msg);
65     }
66
67     public static int flush(HttpServletResponse JavaDoc res)
68     {
69         try {
70             res.getOutputStream().flush();
71             return 0;
72         } catch (IOException JavaDoc e) {
73             return -1;
74         }
75     }
76
77     public static void header(boolean set,
78                               HttpServletResponse JavaDoc res,
79                               String JavaDoc name, String JavaDoc value)
80     {
81         if (name.equalsIgnoreCase("Content-type")) {
82             res.setContentType(value);
83         }
84         else if (name.equalsIgnoreCase("Location")) {
85             try {
86                 res.sendRedirect(value);
87             } catch (IOException JavaDoc e) {
88                 // Nothing.
89
}
90         }
91         else {
92             if (set)
93                 res.setHeader(name, value);
94             else
95                 res.addHeader(name, value);
96         }
97     }
98
99     public static void status(HttpServletResponse JavaDoc res,
100                               int sc)
101     {
102         res.setStatus(sc);
103     }
104
105     public static String JavaDoc[] env(ScriptEnvironment e)
106     {
107         return e.getEnvironmentArray();
108     }
109
110     public static String JavaDoc cookies(ScriptEnvironment e)
111     {
112         return (String JavaDoc)e.getEnvironment().get("HTTP_COOKIE");
113     }
114
115
116 }
117
Popular Tags