forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContext.java
More file actions
175 lines (160 loc) · 5.95 KB
/
Copy pathHttpContext.java
File metadata and controls
175 lines (160 loc) · 5.95 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
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.servlet;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import php.java.bridge.NotImplementedException;
import php.java.bridge.Util;
import php.java.bridge.http.IContext;
/*
* 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.
*/
/**
* A custom context which keeps the HttpServletResponse. Used when JSR223 is not available.
*
* @author jostb
*
*/
public class HttpContext extends php.java.bridge.http.Context {
protected HttpServletResponse response;
protected ServletContext context;
protected HttpServletRequest request;
/**{@inheritDoc}*/
public Object getAttribute(String name) throws IllegalArgumentException{
Object result;
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
if ((getEngineScope()!=null) && (result=getEngineScope().get(name)) != null) {
return result;
} else if ((getGlobalScope()!=null) && (result=getGlobalScope().get(name)) != null) {
return result;
} else 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;
}
/**
* Create a new context.
* @param kontext The servlet context
* @param req The servlet request
* @param res The HttpServletResponse
*/
public HttpContext(ServletContext kontext, HttpServletRequest req, HttpServletResponse res) {
this.context = kontext;
this.response = res;
this.request = req;
}
/**{@inheritDoc}*/
public Writer getWriter() throws IOException {
return response.getWriter();
}
/**
* Return the http servlet response
* @return The http servlet reponse
*/
public Object getHttpServletResponse() {
return getAttribute(IContext.SERVLET_RESPONSE);
}
/**
* Return the http servlet request
* @return The http servlet request
*/
public Object getHttpServletRequest() {
return getAttribute(IContext.SERVLET_REQUEST);
}
/**
* Return the http servlet
* @return The http servlet
*/
public Object getServlet() {
return getAttribute(IContext.SERVLET);
}
/**
* Return the servlet config
* @return The servlet config
*/
public Object getServletConfig() {
return getAttribute(IContext.SERVLET_CONFIG);
}
/**
* Return the servlet context
* @return The servlet context
*/
public Object getServletContext() {
return getAttribute(IContext.SERVLET_CONTEXT);
}
/**
* Only for internal use. <br><br>
* Used when scripts are running within of a servlet environment:
* Either php.java.servlet.Context or the JSR223 Context (see PhpSimpleHttpScriptContext).<br>
* Outside of a servlet environment use the ContextLoaderListener instead:
* Either the Standalone or the JSR223 Standalone (see PhpScriptContext).
* @param closeable The manageable beforeShutdown(), will be called by the {@link ContextLoaderListener#contextDestroyed(javax.servlet.ServletContextEvent)}
* @param ctx The ServletContext
*/
public static void handleManaged(Object closeable, ServletContext ctx) {
List list = (List) ContextLoaderListener.getContextLoaderListener(ctx).getCloseables();
list.add(closeable);
}
/**{@inheritDoc}*/
public Object init(Object callable) throws Exception {
if(Util.logLevel>3) Util.logDebug("calling servlet context init");
return php.java.bridge.http.Context.getManageable(callable);
}
/**{@inheritDoc}*/
public void onShutdown(Object closeable) {
if(Util.logLevel>3) Util.logDebug("calling servlet context register shutdown ");
handleManaged(closeable, context);
}
/** Only for internal use
* @param path the path
* @param ctx the servlet context
* @return the real path
* @deprecated Use {@link php.java.servlet.ServletUtil#getRealPath(ServletContext, String)}
*/
public static String getRealPathInternal(String path, ServletContext ctx) {
return ServletUtil.getRealPath(ctx, path);
}
/**{@inheritDoc}*/
public String getRealPath(String path) {
return ServletUtil.getRealPath(context, path);
}
/**@deprecated*/
public String getRedirectString() {
throw new NotImplementedException();
}
/**@deprecated*/
public String getRedirectString(String webPath) {
throw new NotImplementedException();
}
/**{@inheritDoc}*/
public String getSocketName() {
return String.valueOf(ServletUtil.getLocalPort(request));
}
}