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: }
|