forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpHttpScriptContext.java
More file actions
314 lines (292 loc) · 9.68 KB
/
Copy pathPhpHttpScriptContext.java
File metadata and controls
314 lines (292 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package php.java.script.servlet;
/*-*- mode: Java; tab-width:8 -*-*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import javax.script.ScriptContext;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import php.java.bridge.ILogger;
import php.java.bridge.NotImplementedException;
import php.java.bridge.Util;
import php.java.bridge.http.ContextServer;
import php.java.bridge.http.HeaderParser;
import php.java.bridge.http.WriterOutputStream;
import php.java.script.Continuation;
import php.java.script.IPhpScriptContext;
import php.java.script.PhpScriptContextDecorator;
import php.java.script.PhpScriptWriter;
import php.java.script.ResultProxy;
import php.java.script.servlet.HttpFastCGIProxy;
import php.java.script.servlet.PhpScriptLogWriter;
import php.java.servlet.ContextLoaderListener;
import php.java.servlet.ServletUtil;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* An example decorator for compiled script engines running in a servlet environment.
* Use
* <blockquote>
* <code>
* static final CompiledScript script = ((Compilable)(new ScriptEngineManager().getEngineByName("php-invocable"))).compile("<?php ...?>");<br>
* <br>
* script.eval(new php.java.script.servlet.PhpCompiledHttpScriptContext(script.getEngine().getContext(),this,application,request,response));
* </code>
* </blockquote>
*
* @author jostb
*
*/
public class PhpHttpScriptContext extends PhpScriptContextDecorator {
/**
* Create a new PhpCompiledScriptContext using an existing
* PhpScriptContext
* @param ctx the script context to be decorated
*/
public PhpHttpScriptContext(ScriptContext ctx, Servlet servlet, ServletContext context, HttpServletRequest request, HttpServletResponse response) {
super((IPhpScriptContext)ctx);
this.request = request;
this.response = response;
this.context = context;
this.servlet = servlet;
}
/**{@inheritDoc}*/
public Continuation createContinuation(Reader reader, Map env,
OutputStream out, OutputStream err, HeaderParser headerParser, ResultProxy result,
ILogger logger, boolean isCompiled) {
Continuation cont;
if (isCompiled) {
ContextLoaderListener listener = ContextLoaderListener.getContextLoaderListener((ServletContext) getServletContext());
cont = new HttpFastCGIProxy(env, out, err, headerParser, result, listener.getConnectionPool());
} else
cont = super.createContinuation(reader, env, out, err, headerParser, result, logger, isCompiled);
return cont;
}
public void startContinuation() {
ContextLoaderListener listener = ContextLoaderListener.getContextLoaderListener((ServletContext) getServletContext());
listener.getThreadPool().start(getContinuation());
}
/** Integer value for the level of SCRIPT_SCOPE */
public static final int REQUEST_SCOPE = 0;
/** Integer value for the level of SESSION_SCOPE */
public static final int SESSION_SCOPE = 150;
/** Integer value for the level of APPLICATION_SCOPE */
public static final int APPLICATION_SCOPE = 175;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected ServletContext context;
protected Servlet servlet;
/**{@inheritDoc}*/
public Object getAttribute(String key, int scope){
if(scope == REQUEST_SCOPE){
return request.getAttribute(key);
}else if(scope == SESSION_SCOPE){
return request.getSession().getAttribute(key);
}else if(scope == APPLICATION_SCOPE){
return context.getAttribute(key);
}else{
return super.getAttribute(key, scope);
}
}
/**{@inheritDoc}*/
public Object getAttribute(String name) throws IllegalArgumentException{
Object result;
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
if ((result = super.getAttribute(name))!=null) return result;
if ((result=request.getAttribute(name)) != null) {
return result;
} else if ((result=request.getSession().getAttribute(name)) != null) {
return result;
} else if ((result=context.getAttribute(name)) != null) {
return result;
}
return null;
}
/**{@inheritDoc}*/
public void setAttribute(String key, Object value, int scope)
throws IllegalArgumentException {
if(scope == REQUEST_SCOPE){
request.setAttribute(key, value);
}else if(scope == SESSION_SCOPE){
request.getSession().setAttribute(key, value);
}else if(scope == APPLICATION_SCOPE){
context.setAttribute(key, value);
}else{
super.setAttribute(key, value, scope);
}
}
/**
* Get the servlet response
* @return The HttpServletResponse
*/
public HttpServletResponse getResponse() {
return response;
}
/**
* Get the HttpServletRequest
* @return The HttpServletRequest
*/
public HttpServletRequest getRequest() {
return request;
}
/**
* Get the ServletContext
* @return The current ServletContext
*/
public ServletContext getContext() {
return context;
}
protected Writer writer;
/** {@inheritDoc} */
public Writer getWriter() {
if(writer == null) {
try {
setWriter(response.getWriter());
} catch (IOException e) {
Util.printStackTrace(e);
}
}
return writer;
}
/** {@inheritDoc} */
public void setWriter(Writer writer) {
if(! (writer instanceof PhpScriptWriter)) {
writer = new PhpScriptWriter(new WriterOutputStream(writer));
}
super.setWriter(this.writer = writer);
}
protected Writer errorWriter;
/** {@inheritDoc} */
public Writer getErrorWriter() {
if(errorWriter == null) {
setErrorWriter(PhpScriptLogWriter.getWriter(new php.java.servlet.Logger()));
}
return errorWriter;
}
/**{@inheritDoc}*/
public void setErrorWriter(Writer errorWriter) {
if(! (errorWriter instanceof PhpScriptWriter)) {
errorWriter = new PhpScriptWriter(new WriterOutputStream(errorWriter));
}
super.setErrorWriter(this.errorWriter = errorWriter);
}
protected Reader reader;
/**{@inheritDoc}*/
public Reader getReader() {
if(reader == null) {
try {
reader = request.getReader();
} catch (IOException e) {
Util.printStackTrace(e);
}
}
return reader;
}
public void setReader(Reader reader) {
super.setReader(this.reader = reader);
}
/**{@inheritDoc}*/
public Object init(Object callable) throws Exception {
return php.java.bridge.http.Context.getManageable(callable);
}
/**{@inheritDoc}*/
public void onShutdown(Object closeable) {
php.java.servlet.HttpContext.handleManaged(closeable, context);
}
/**
* Return the http servlet response
* @return The http servlet reponse
*/
public Object getHttpServletResponse() {
return response;
}
/**
* Return the http servlet request
* @return The http servlet request
*/
public Object getHttpServletRequest() {
return request;
}
/**
* Return the http servlet
* @return The http servlet
*/
public Object getServlet() {
return servlet;
}
/**
* Return the servlet config
* @return The servlet config
*/
public Object getServletConfig() {
return servlet.getServletConfig();
}
/**
* Return the servlet context
* @return The servlet context
*/
public Object getServletContext() {
return context;
}
/**{@inheritDoc}*/
public String getRealPath(String path) {
return ServletUtil.getRealPath(context, path);
}
/**@deprecated*/
public String getRedirectString(String webPath) {
throw new NotImplementedException();
}
/**@deprecated*/
public String getRedirectString() {
throw new NotImplementedException();
}
/**{@inheritDoc}*/
public String getRedirecturl(String webPath) {
StringBuffer buf = new StringBuffer();
buf.append(getSocketName());
buf.append("/");
buf.append(webPath);
try {
URI uri = new URI(request.isSecure()?"https:127.0.0.1":"http:127.0.0.1", buf.toString(), null);
return uri.toASCIIString();
} catch (URISyntaxException e) {
Util.printStackTrace(e);
throw new RuntimeException(e);
}
}
/**{@inheritDoc}*/
public String getSocketName() {
return String.valueOf(ServletUtil.getLocalPort(request));
}
/**{@inheritDoc}*/
public ContextServer getContextServer() {
return ContextLoaderListener.getContextLoaderListener(context).getContextServer();
}
}