Either way, good luck with it, and thanks for contributing.
-- James Mitchell Software Engineer / Open Source Evangelist EdgeTech, Inc. 678.910.8017 AIM: jmitchtx
----- Original Message ----- From: "Frank W. Zammetti" <[EMAIL PROTECTED]>
To: "Jakarta Commons Developers List" <commons-dev@jakarta.apache.org>
Sent: Tuesday, December 28, 2004 2:13 PM
Subject: Re: [SERVLET] Code submission
Your very welcome James! :)
I'm actually in agreement with Martin in that it probably does belong in the Servlet package. I like the idea of it not being tied to Struts as I originally intended, although I'd have no problem if either thing was.
Of course, I ultimately don't have a say (aside from I guess saying "no, you can't have it at all!"), but I'm not sure there's really a better place for either thing. :)
My only concern is whether the servlet package is active or not... it seems, looking at some of the dates on things, that it hasn't been touched in a while, and certainly it's been in the sandbox for some time. I'd hate to put these potentially useful things somewhere they'll never see the light of day. Can anyone comment on the real status of the Servlet package?
-- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com
James Mitchell wrote:I already hijacked the SessionSize piece for my own debug script (Thanks Frank!!)
I would be +1 (non binding) for such an addition. But, as Frank mentioned, I'm not sure where it would live either.
My $0.02
-- James Mitchell Software Engineer / Open Source Evangelist EdgeTech, Inc. 678.910.8017 AIM: jmitchtx
----- Original Message ----- From: "Frank W. Zammetti" <[EMAIL PROTECTED]>
To: "Commons Developer" <commons-dev@jakarta.apache.org>
Sent: Tuesday, December 28, 2004 1:00 PM
Subject: [SERVLET] Code submission
I recently proposed an addition to Struts that Martin Cooper suggested might be a better fit for the Commons Servlet package, and I agreed with him, so I'm posting it here. However, looking at the sandbox contents I'm not sure the Servlet package is still being developed. Is it?
If so, I offer the following two additions...
The first is an addition to the RequestUtils class in the form of a new dumpRequestSessionData() method. It is a simple static method that dumps to stdout all request headers/parameters/attributes and session attributes. Martin informed me that there is a Tomcat filter to do this, but I personally like the idea of this not requiring a filter, and also this ensures it is completely portable across containers (and could always be called from a filter anyway!) so I make the offer. It's simple, but gets the job done. It is just a method, but I also included the imports it requires.
The second submission would be a new SessionUtils class. The first function it contains, getSessionSize(), allows you to get the total size of a given session object. This can come in very handy during development, especially when working in a distributed environment (which is how it came about in my case).
Please let me know what questions, comments or concerns you have. The one I know already is that the dumpRequestSessionData() method doesn't use commons logging, but I'm frankly not too familiar with that, and besides, with the idea being that this is probably just a development-time function, stdout might not be such a problem there.
Note that none of this code has any dependencies outside J2SE and servlet.jar. Lastly, I'm not sure if attachments get through to the list, but I've attached source files as well as pasted all the code in below.
Thanks all!
Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com
--------------------------------------- RequestUtils.dumpRequestSessionData() : ---------------------------------------
import java.util.Enumeration; import java.util.HashMap; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletRequest;
/**
* This method is used to dump to stdout all request headers,
parameter and
* attributes, and all session attributes (if a session exists).
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a>
* @param request A valid HTTPServletRequest object
*/
public static void dumpRequestSessionData(HttpServletRequest request) {
HashMap request_attributes = new HashMap(); for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) { String next = (String)en.nextElement(); request_attributes.put(next, request.getAttribute(next)); } HashMap request_parameters = new HashMap(); for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) { String next = (String)en.nextElement(); request_parameters.put(next, request.getParameter(next)); } HashMap request_headers = new HashMap(); for (Enumeration en = request.getHeaderNames(); en.hasMoreElements();) { String next = (String)en.nextElement(); request_headers.put(next, request.getHeader(next)); } HttpSession session = request.getSession(); HashMap session_attributes = new HashMap(); if (session != null) { for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();) { String next = (String)en.nextElement(); session_attributes.put(next, session.getAttribute(next)); } } System.out.println("Request Attributes\n------------------\n" + request_attributes); System.out.println("Request Parameters\n------------------\n" + request_parameters); System.out.println("Request Headers\n---------------\n" + request_headers); System.out.println("Session Attributes\n------------------\n" + session_attributes);
} // End dumpRequestSessionData()
-------------- SessionUtils : --------------
package org.apache.commons.servlet;
/*
* Copyright 2002,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import javax.servlet.http.HttpSession; import java.security.AccessController; import java.security.PrivilegedAction; import java.lang.reflect.Field; import java.io.ByteArrayOutputStream; import java.util.Enumeration; import java.io.ObjectOutputStream; import java.io.NotSerializableException;
/** * This class provides utilities for getting information from * javax.servlet.http.HttpSession. * * @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a> */ public class SessionUtils {
/**
* This method is used to get the total size of a current, valid
HTTPSession
* object it is passed.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a>
* @param session A valid HTTPSession object
* @return The total size of session in bytes
*/
public static int getSessionSize(HttpSession session) {
Enumeration en = session.getAttributeNames(); String name = null; Object obj = null; String serialOut; String SIZE_DELIMITER = "size="; int sizeIndex; int objSize; int totalSize = 0; while (en.hasMoreElements()) { name = (String)en.nextElement(); obj = session.getAttribute(name); serialOut = serializiableTest(obj); if ((sizeIndex = serialOut.lastIndexOf(SIZE_DELIMITER)) > 0) { objSize = Integer.parseInt(serialOut.substring(sizeIndex + SIZE_DELIMITER.length(), serialOut.lastIndexOf(')'))); totalSize += objSize; } } return totalSize;
} // End getSessionSize()
/**
* This method is used by the getSessionSize() method to determine if a
* given object is serializable.
*
* @param Object The object to test
* @return String A response string detailing the outcome of the test
*/
private static String serializiableTest(Object obj) {
String ans = "ok"; if (obj == null) { return "Object is null"; } else { try { ByteArrayOutputStream bastream = new ByteArrayOutputStream(); ObjectOutputStream p = new ObjectOutputStream(bastream); p.writeObject(obj); ans = "OK (size=" + bastream.size() + ")"; } catch (NotSerializableException ex) { Field[] fields = obj.getClass().getDeclaredFields(); ans = "NOT SERIALIZABLE (fields=" + fields + ")"; ex.printStackTrace(); Object fldObj = null; if (fields != null && (fields.length != 0)) { StringBuffer sb = new StringBuffer("\n" + ans + "["); for (int i = 0; i < fields.length; i++) { sb.append(fields[i].getName()); try { if (obj != null) { fldObj = getFieldWithPrivilege(fields[i], obj); } sb.append("::"); if (fldObj == null) { sb.append("<field null>"); } else { sb.append(serializiableTest(fldObj)); } } catch (IllegalArgumentException aex) { sb.append("::"); sb.append("ILLEGAL ARGUMENT EXCEPTION"); } if (i != fields.length - 1) { sb.append('\n'); } } sb.append("]"); ans = sb.toString(); } } catch (Exception ex) { ans = "EXCEPTION: " + ex.getMessage(); } } return obj.getClass().getName() + " is " + ans;
} // End serializiableTest()
/** * This method is used by the serializiableTest() method to get the * needed priveleges on a given field of a given object needed to * perform the serializable test. * * @param Field The field to get priveleges on Object The object to test * @return Object A Priveleged reference to the field essentially */ private static Object getFieldWithPrivilege(Field fld, Object obj) {
final Object obj2 = obj; final Field fld2 = fld; return AccessController.doPrivileged( new PrivilegedAction() { public Object run() { try { return fld2.get(obj2); } catch (IllegalAccessException ex) { ex.printStackTrace(); return null; } } } );
} // End getFieldWithPrivilege()
} // End SessionUtils class
--------------------------------------------------------------------------------
package org.apache.commons.servlet;
/*
* Copyright 2002,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import javax.servlet.http.HttpSession; import java.security.AccessController; import java.security.PrivilegedAction; import java.lang.reflect.Field; import java.io.ByteArrayOutputStream; import java.util.Enumeration; import java.io.ObjectOutputStream; import java.io.NotSerializableException;
/** * This class provides utilities for getting information from * javax.servlet.http.HttpSession. * * @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a> */ public class SessionUtils {
/**
* This method is used to get the total size of a current, valid HTTPSession
* object it is passed.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a>
* @param session A valid HTTPSession object
* @return The total size of session in bytes
*/
public static int getSessionSize(HttpSession session) {
Enumeration en = session.getAttributeNames();
String name = null;
Object obj = null;
String serialOut;
String SIZE_DELIMITER = "size=";
int sizeIndex;
int objSize;
int totalSize = 0;
while (en.hasMoreElements()) {
name = (String)en.nextElement();
obj = session.getAttribute(name);
serialOut = serializiableTest(obj);
if ((sizeIndex = serialOut.lastIndexOf(SIZE_DELIMITER)) > 0) {
objSize = Integer.parseInt(serialOut.substring(sizeIndex + SIZE_DELIMITER.length(), serialOut.lastIndexOf(')')));
totalSize += objSize;
}
}
return totalSize;
} // End getSessionSize()
/** * This method is used by the getSessionSize() method to determine if a * given object is serializable. * * @param Object The object to test * @return String A response string detailing the outcome of the test */ private static String serializiableTest(Object obj) {
String ans = "ok"; if (obj == null) { return "Object is null"; } else { try { ByteArrayOutputStream bastream = new ByteArrayOutputStream(); ObjectOutputStream p = new ObjectOutputStream(bastream); p.writeObject(obj); ans = "OK (size=" + bastream.size() + ")"; } catch (NotSerializableException ex) { Field[] fields = obj.getClass().getDeclaredFields(); ans = "NOT SERIALIZABLE (fields=" + fields + ")"; ex.printStackTrace(); Object fldObj = null; if (fields != null && (fields.length != 0)) { StringBuffer sb = new StringBuffer("\n" + ans + "["); for (int i = 0; i < fields.length; i++) { sb.append(fields[i].getName()); try { if (obj != null) { fldObj = getFieldWithPrivilege(fields[i], obj); } sb.append("::"); if (fldObj == null) { sb.append("<field null>"); } else { sb.append(serializiableTest(fldObj)); } } catch (IllegalArgumentException aex) { sb.append("::"); sb.append("ILLEGAL ARGUMENT EXCEPTION"); } if (i != fields.length - 1) { sb.append('\n'); } } sb.append("]"); ans = sb.toString(); } } catch (Exception ex) { ans = "EXCEPTION: " + ex.getMessage(); } } return obj.getClass().getName() + " is " + ans;
} // End serializiableTest()
/** * This method is used by the serializiableTest() method to get the * needed priveleges on a given field of a given object needed to * perform the serializable test. * * @param Field The field to get priveleges on Object The object to test * @return Object A Priveleged reference to the field essentially */ private static Object getFieldWithPrivilege(Field fld, Object obj) {
final Object obj2 = obj; final Field fld2 = fld; return AccessController.doPrivileged( new PrivilegedAction() { public Object run() { try { return fld2.get(obj2); } catch (IllegalAccessException ex) { ex.printStackTrace(); return null; } } } );
} // End getFieldWithPrivilege()
} // End SessionUtils class
--------------------------------------------------------------------------------
import java.util.Enumeration; import java.util.HashMap; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletRequest;
/**
* This method is used to dump to stdout all request headers, parameter and
* attributes, and all session attributes (if a session exists).
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a>
* @param request A valid HTTPServletRequest object
*/
public static void dumpRequestSessionData(HttpServletRequest request) {
HashMap request_attributes = new HashMap();
for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) {
String next = (String)en.nextElement();
request_attributes.put(next, request.getAttribute(next));
}
HashMap request_parameters = new HashMap();
for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) {
String next = (String)en.nextElement();
request_parameters.put(next, request.getParameter(next));
}
HashMap request_headers = new HashMap();
for (Enumeration en = request.getHeaderNames(); en.hasMoreElements();) {
String next = (String)en.nextElement();
request_headers.put(next, request.getHeader(next));
}
HttpSession session = request.getSession();
HashMap session_attributes = new HashMap();
if (session != null) {
for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();)
{
String next = (String)en.nextElement();
session_attributes.put(next, session.getAttribute(next));
}
}
System.out.println("Request Attributes\n------------------\n" + request_attributes);
System.out.println("Request Parameters\n------------------\n" + request_parameters);
System.out.println("Request Headers\n---------------\n" + request_headers);
System.out.println("Session Attributes\n------------------\n" + session_attributes);
} // End dumpRequestSessionData()
--------------------------------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]