Source Code Cross Referenced for SimpleHttpScriptContext.java in  » Scripting » beanshell » javax » script » http » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » beanshell » javax.script.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * SimpleHttpScriptContext.java
003:         *
004:         * @version 1.0
005:         * @author Mike Grogan
006:         * @Created on March 11, 2004, 10:47 AM
007:         */
008:
009:        package javax.script.http;
010:
011:        import javax.script.*;
012:        import javax.script.http.*;
013:        import javax.servlet.*;
014:        import javax.servlet.http.*;
015:        import java.io.*;
016:        import java.util.*;
017:
018:        /**
019:         * Generic implementation of <code>HttpScriptContext</code>
020:         */
021:        public class SimpleHttpScriptContext extends SimpleScriptContext
022:                implements  HttpScriptContext {
023:
024:            protected boolean disableScript;
025:            protected boolean displayResults;
026:            protected boolean useSession;
027:            protected String[] methods;
028:            protected String[] languages;
029:            protected String docRoot;
030:
031:            protected HttpServletRequest request;
032:            protected HttpServletResponse response;
033:            protected Servlet servlet;
034:
035:            public static final String[] defaultMethods = { "GET", "POST" };
036:
037:            public SimpleHttpScriptContext() {
038:                //sets default field values
039:                release();
040:            }
041:
042:            public boolean disableScript() {
043:                return disableScript;
044:            }
045:
046:            public boolean displayResults() {
047:                return displayResults;
048:            }
049:
050:            public void forward(String relativePath) throws ServletException,
051:                    IOException {
052:
053:                ServletContext context = servlet.getServletConfig()
054:                        .getServletContext();
055:                String uri = ((HttpServletRequest) request).getServletPath();
056:                String baseURI = uri.substring(0, uri.lastIndexOf('/'));
057:                String path = baseURI + '/' + relativePath;
058:                context.getRequestDispatcher(path).forward(request, response);
059:            }
060:
061:            public String[] getMethods() {
062:                return methods;
063:            }
064:
065:            public String[] getAllowedLanguages() {
066:                return languages;
067:            }
068:
069:            public HttpServletRequest getRequest() {
070:                return request;
071:            }
072:
073:            public HttpServletResponse getResponse() {
074:                return response;
075:            }
076:
077:            public java.io.Reader getScriptSource() {
078:                //we may be included from another page.  The call to request.getRequestURI()
079:                //returns the URI for the original request
080:                String requestURI = (String) request
081:                        .getAttribute("javax.servlet.include.request_uri");
082:                if (requestURI == null || requestURI.equals("")) {
083:
084:                    requestURI = request.getRequestURI();
085:                }
086:
087:                String resourcePath = requestURI.substring(request
088:                        .getContextPath().length());
089:                try {
090:                    if (docRoot != null) {
091:                        String separator = docRoot.endsWith(File.separator)
092:                                || resourcePath.startsWith(File.separator) ? ""
093:                                : File.separator;
094:
095:                        String fullPath = docRoot + separator + resourcePath;
096:                        return new InputStreamReader(new FileInputStream(
097:                                fullPath));
098:                    } else {
099:                        InputStream stream = servlet.getServletConfig()
100:                                .getServletContext().getResourceAsStream(
101:                                        resourcePath);
102:                        return new BufferedReader(new InputStreamReader(stream));
103:                    }
104:                } catch (Exception e) {
105:                    return null;
106:                }
107:
108:            }
109:
110:            public javax.servlet.Servlet getServlet() {
111:                return servlet;
112:            }
113:
114:            private void includeOnNewThread(final String path,
115:                    final ServletContext context) {
116:                try {
117:                    Runnable r = new Runnable() {
118:                        public void run() {
119:                            try {
120:                                context.getRequestDispatcher(path).include(
121:                                        request, response);
122:                            } catch (Exception e) {
123:                                e.printStackTrace();
124:                            }
125:                        }
126:                    };
127:
128:                    Thread t = new Thread(r);
129:                    t.start();
130:                    t.join();
131:
132:                } catch (Exception e) {
133:                    e.printStackTrace();
134:                }
135:            }
136:
137:            public void include(String relativePath) throws ServletException,
138:                    IOException {
139:                ServletContext context = servlet.getServletConfig()
140:                        .getServletContext();
141:                String uri = ((HttpServletRequest) request).getServletPath();
142:                String baseURI = uri.substring(0, uri.lastIndexOf('/'));
143:                String path = baseURI + '/' + relativePath;
144:
145:                context.getRequestDispatcher(path).include(request, response);
146:
147:                //includeOnNewThread(path, context);
148:            }
149:
150:            public void initialize(javax.servlet.Servlet servlet,
151:                    javax.servlet.http.HttpServletRequest req,
152:                    javax.servlet.http.HttpServletResponse res)
153:                    throws javax.servlet.ServletException {
154:                request = new RequestWrapper(req);
155:                response = new ResponseWrapper(res);
156:                this .servlet = servlet;
157:
158:                ServletContext context = servlet.getServletConfig()
159:                        .getServletContext();
160:                docRoot = context.getInitParameter("script-directory");
161:                String disable = context.getInitParameter("script-disable");
162:                String session = context.getInitParameter("script-use-session");
163:                String display = context
164:                        .getInitParameter("script-display-results");
165:                String methodstring = context
166:                        .getInitParameter("script-methods");
167:                String languagesstring = context
168:                        .getInitParameter("allow-languages");
169:
170:                if (docRoot != null && !(new File(docRoot)).isDirectory()) {
171:                    throw new ServletException("Specified document root ,"
172:                            + docRoot
173:                            + " either does not exist or is not a directory.");
174:                }
175:
176:                if (disable != null && disable.equals("true")) {
177:                    disableScript = true;
178:                    return;
179:                }
180:
181:                if (session != null && session.equals("false")) {
182:                    useSession = false;
183:                }
184:
185:                if (display != null && display.equals("false")) {
186:                    displayResults = false;
187:                }
188:
189:                if (methodstring != null) {
190:                    StringTokenizer tokenizer = new StringTokenizer(
191:                            methodstring, ",");
192:                    ArrayList methodList = new ArrayList();
193:                    while (tokenizer.hasMoreTokens()) {
194:                        methodList.add(tokenizer.nextToken());
195:                    }
196:                    methods = (String[]) methodList
197:                            .toArray(new String[methodList.size()]);
198:                }
199:
200:                if (languagesstring != null && !(languagesstring.equals(""))) {
201:                    StringTokenizer tokenizer = new StringTokenizer(
202:                            languagesstring, ",");
203:                    ArrayList languagesList = new ArrayList();
204:                    while (tokenizer.hasMoreTokens()) {
205:                        languagesList.add(tokenizer.nextToken());
206:                    }
207:                    languages = (String[]) languagesList
208:                            .toArray(new String[languagesList.size()]);
209:                } else {
210:                    languages = null;
211:                }
212:            }
213:
214:            public void release() {
215:                //restore defaults
216:                disableScript = false;
217:                displayResults = true;
218:                useSession = true;
219:                methods = defaultMethods;
220:                request = null;
221:                response = null;
222:                servlet = null;
223:            }
224:
225:            private void testScopeValue(int scope) {
226:                if (scope != REQUEST_SCOPE && scope != SESSION_SCOPE
227:                        && scope != APPLICATION_SCOPE) {
228:                    throw new IllegalArgumentException("Invalid scope value.");
229:                } else if (scope == SESSION_SCOPE) {
230:                    HttpSession sess = request.getSession();
231:                    if (!useSession || sess == null) {
232:                        throw new IllegalStateException(
233:                                "Session state disabled.");
234:                    }
235:                }
236:            }
237:
238:            public void setAttribute(String key, Object value, int scope) {
239:                testScopeValue(scope);
240:                if (scope == REQUEST_SCOPE) {
241:                    request.setAttribute(key, value);
242:                } else if (scope == SESSION_SCOPE) {
243:                    request.getSession().setAttribute(key, value);
244:                } else if (scope == APPLICATION_SCOPE) {
245:                    System.out.println("setting key = " + key + " value = "
246:                            + value);
247:                    servlet.getServletConfig().getServletContext()
248:                            .setAttribute(key, value);
249:                }
250:            }
251:
252:            //overwrite that in GenericScriptContext
253:            public Object getAttribute(String key) {
254:
255:                Object ret;
256:                if (null != (ret = getAttribute(key, REQUEST_SCOPE))) {
257:                    return ret;
258:                } else if (null != (ret = getAttribute(key, SESSION_SCOPE))) {
259:                    return ret;
260:                } else if (null != (ret = getAttribute(key, APPLICATION_SCOPE))) {
261:                    return ret;
262:                }
263:
264:                return null;
265:            }
266:
267:            public Object getAttribute(String key, int scope) {
268:                testScopeValue(scope);
269:                if (scope == REQUEST_SCOPE) {
270:                    return request.getAttribute(key);
271:                } else if (scope == SESSION_SCOPE) {
272:                    return request.getSession().getAttribute(key);
273:                } else if (scope == APPLICATION_SCOPE) {
274:                    Object obj = servlet.getServletConfig().getServletContext()
275:                            .getAttribute(key);
276:                    System.out.println("value = " + obj);
277:                    return obj;
278:                }
279:                return null;
280:            }
281:
282:            public boolean useSession() {
283:                return useSession;
284:            }
285:
286:            public Writer getWriter() {
287:                try {
288:                    return response.getWriter();
289:                } catch (IOException e) {
290:                    e.printStackTrace();
291:                    return null;
292:                }
293:            }
294:
295:            public class RequestWrapper extends
296:                    javax.servlet.http.HttpServletRequestWrapper {
297:                public RequestWrapper(HttpServletRequest req) {
298:                    super (req);
299:                }
300:
301:                public HttpSession getSession() {
302:                    if (useSession()) {
303:                        return super .getSession();
304:                    } else {
305:                        return null;
306:                    }
307:                }
308:            }
309:
310:            public class ResponseWrapper extends
311:                    javax.servlet.http.HttpServletResponseWrapper {
312:                public ResponseWrapper(HttpServletResponse res) {
313:                    super (res);
314:                }
315:
316:                //need to use trivial wrapper for cases where we are included from another page using same
317:                //script engine.  In this case, since RequestDispatcher.include calls response.getOutputStream,
318:                //outer page fails when it trys to call getWriter.  ServletAPI allow wrapper to be used in
319:                //which case the IllegalStateException is not thrown.  Need to research what this breaks. 
320:                public javax.servlet.ServletOutputStream getOutputStream()
321:                        throws java.io.IOException {
322:                    return super .getOutputStream();
323:                }
324:            }
325:
326:            public List<Integer> getScopes() {
327:                return scopes;
328:            }
329:
330:            private static List<Integer> scopes;
331:            static {
332:                scopes = new ArrayList<Integer>();
333:                scopes.add(REQUEST_SCOPE);
334:                scopes.add(SESSION_SCOPE);
335:                scopes.add(APPLICATION_SCOPE);
336:                scopes.add(ENGINE_SCOPE);
337:                scopes.add(GLOBAL_SCOPE);
338:                scopes = Collections.unmodifiableList(scopes);
339:            }
340:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.