Source Code Cross Referenced for ServletUtils.java in  » Library » Apache-beehive-1.0.2-src » org » apache » beehive » netui » util » internal » 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 » Library » Apache beehive 1.0.2 src » org.apache.beehive.netui.util.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         *
017:         * $Header:$
018:         */
019:        package org.apache.beehive.netui.util.internal;
020:
021:        import javax.servlet.http.HttpServletRequest;
022:        import javax.servlet.http.HttpServletResponse;
023:        import javax.servlet.http.HttpSession;
024:        import javax.servlet.ServletContext;
025:        import javax.servlet.ServletResponse;
026:        import javax.servlet.ServletRequest;
027:        import javax.servlet.ServletException;
028:        import java.io.PrintStream;
029:        import java.util.Enumeration;
030:
031:        import org.apache.beehive.netui.util.logging.Logger;
032:
033:        public class ServletUtils {
034:
035:            private static final Logger LOG = Logger
036:                    .getInstance(ServletUtils.class);
037:
038:            public static final String SESSION_MUTEX_ATTRIBUTE = ServletUtils.class
039:                    .getName()
040:                    + ".MUTEX";
041:
042:            /**
043:             * Print parameters and attributes in the given request.
044:             * 
045:             * @param request the current HttpServletRequest.
046:             * @param output a PrintStream to which to output request parameters and request/session
047:             *            attributes; if <code>null</null>, <code>System.err</code> is used.
048:             * 
049:             */
050:            public static void dumpRequest(ServletRequest request,
051:                    PrintStream output) {
052:                if (output == null) {
053:                    output = System.err;
054:                }
055:
056:                output.println("*** ServletRequest " + request);
057:
058:                if (request instanceof  HttpServletRequest) {
059:                    output.println("        uri = "
060:                            + ((HttpServletRequest) request).getRequestURI());
061:                }
062:
063:                for (Enumeration e = request.getParameterNames(); e
064:                        .hasMoreElements();) {
065:                    String name = (String) e.nextElement();
066:                    output.println("            parameter " + name + " = "
067:                            + request.getParameter(name));
068:                }
069:
070:                for (Enumeration e = request.getAttributeNames(); e
071:                        .hasMoreElements();) {
072:                    String name = (String) e.nextElement();
073:                    output.println("            attribute " + name + " = "
074:                            + request.getAttribute(name));
075:                }
076:
077:                if (request instanceof  HttpServletRequest) {
078:                    HttpSession session = ((HttpServletRequest) request)
079:                            .getSession(false);
080:
081:                    if (session != null) {
082:                        for (Enumeration e = session.getAttributeNames(); e
083:                                .hasMoreElements();) {
084:                            String name = (String) e.nextElement();
085:                            output
086:                                    .println("            session attribute "
087:                                            + name + " = "
088:                                            + session.getAttribute(name));
089:                        }
090:                    }
091:                }
092:            }
093:
094:            /**
095:             * Print attributes in the given ServletContext.
096:             * 
097:             * @param context the current ServletContext.
098:             * @param output a PrintStream to which to output ServletContext attributes; if <code>null</null>,
099:                             <code>System.err</code> is used.
100:             * 
101:             */
102:            public static void dumpServletContext(ServletContext context,
103:                    PrintStream output) {
104:                if (output == null) {
105:                    output = System.err;
106:                }
107:
108:                output.println("*** ServletContext " + context);
109:
110:                for (Enumeration e = context.getAttributeNames(); e
111:                        .hasMoreElements();) {
112:                    String name = (String) e.nextElement();
113:                    output.println("            attribute " + name + " = "
114:                            + context.getAttribute(name));
115:                }
116:            }
117:
118:            /**
119:             * Set response headers to prevent caching of the response by the browser.
120:             * 
121:             * @param response the current ServletResponse
122:             */
123:            public static void preventCache(ServletResponse response) {
124:                if (response instanceof  HttpServletResponse) {
125:                    HttpServletResponse httpResponse = (HttpServletResponse) response;
126:                    httpResponse.setHeader("Pragma", "No-cache");
127:                    httpResponse.setHeader("Cache-Control",
128:                            "no-cache,no-store,max-age=0");
129:                    httpResponse.setDateHeader("Expires", 1);
130:                }
131:            }
132:
133:            /**
134:             * Get the base filename of the given URI.
135:             *
136:             * @param uri the URI from which to get the base filename.
137:             * @return a String containing everything after the last slash of the given URI.
138:             */
139:            public static String getBaseName(String uri) {
140:                int lastSlash = uri.lastIndexOf('/');
141:                assert lastSlash != -1 : uri;
142:                assert lastSlash < uri.length() - 1 : "URI must not end with a slash: "
143:                        + uri;
144:                return uri.substring(lastSlash + 1);
145:            }
146:
147:            /**
148:             * Get the directory path of the given URI.
149:             *
150:             * @param uri the URI from which to get the directory path.
151:             * @return a String containing everything before the last slash of the given URI.
152:             */
153:            public static String getDirName(String uri) {
154:                int lastSlash = uri.lastIndexOf('/');
155:                assert lastSlash != -1 : uri;
156:                assert uri.length() > 1 : uri;
157:                assert lastSlash < uri.length() - 1 : "URI must not end with a slash: "
158:                        + uri;
159:                return uri.substring(0, lastSlash);
160:            }
161:
162:            /**
163:             * This initializes the 'cause' on the exception before throwing it; otherwise, the chain of exceptions is hidden
164:             * because of legacy behavior in ServletException ('rootCause' vs. 'cause').
165:             */
166:            public static void throwServletException(Throwable cause)
167:                    throws ServletException {
168:                ServletException servletException = new ServletException(cause);
169:
170:                // todo: future cleanup
171:                // the servlet 2.5 api sets does the equivalent of initCause in its constructor
172:                // where the servlet 2.4 api does not, so check for the 2.5 behavior before setting initCause
173:                if (servletException.getCause() == null) {
174:                    servletException.initCause(cause);
175:                }
176:                throw servletException;
177:            }
178:
179:            /**
180:             * Returns a mutex object for the given {@link HttpSession} that can be used
181:             * as a lock for a given session. For example, to synchronize lazy
182:             * initialization of session scoped objects.
183:             *
184:             * <p>The semantics for locking on an HttpSession object are unspecified, and
185:             * servlet containers are free to implement the HttpSession in such a way
186:             * that acquiring a lock on the HttpSession itself is not safe.  When used
187:             * in conjunction with a HttpSessionListener (such as NetUI's
188:             * HttpSessionMutexListener) that puts a mutex object on the session when
189:             * the session is created, this method provides a lock that is 100% safe
190:             * to use across servlet containers. If a HttpSessionListener is not
191:             * registered in web.xml and there is no object for the given attribute name,
192:             * the HttpSession itself is returned as the next best lock.</p>
193:             *
194:             * @param httpSession the current session
195:             * @param attributeName the attribute name of the mutex object on the session
196:             * @return a mutex that can be used to serialize operations on the HttpSession
197:             */
198:            public static Object getSessionMutex(HttpSession httpSession,
199:                    String attributeName) {
200:                assert httpSession != null : "HttpSession must not be null";
201:                assert attributeName != null : "The attribute name must not be null";
202:
203:                Object mutex = httpSession.getAttribute(attributeName);
204:                if (mutex == null)
205:                    mutex = httpSession;
206:
207:                assert mutex != null;
208:
209:                if (LOG.isDebugEnabled())
210:                    LOG
211:                            .debug("Using session lock of type: "
212:                                    + mutex.getClass());
213:
214:                return mutex;
215:            }
216:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.