You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Frank W. Zammetti" <fz...@omnytex.com> on 2004/12/28 19:00:23 UTC
[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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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
Re: [SERVLET] Code submission
Posted by Martin Cooper <mf...@gmail.com>.
On Wed, 29 Dec 2004 02:07:36 -0500, Henri Yandell <fl...@gmail.com> wrote:
> Servlet's basically been inactive for a couple of years as there just
> wasn't enough there to warrant a component. 2 methods in fact :)
>
> When we broke up the [util] project (lang, io, codec, various others
> were all spawned), the code was filed under servlet and left until
> more could join it.
>
> I'm happily +1 for your code to be added, but I suspect it would just
> linger. We tend not to release code without unit tests, and I know
> that I personally don't have any great itch to dig out a mock-servlet
> implementation to get the methods tested.
My thought in suggesting this to Frank was that we might breathe life
into the servlet component. ;-) As for mock objects, they already
exist in the FileUpload component, and are used for its unit tests. I
suspect HttpClient might have something too. Hmm, maybe that's fertile
ground for a sandbox component, collecting the various testing bits
and pieces from different components...
--
Martin Cooper
> Hen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [SERVLET] Code submission
Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Yep, that was the impression I got of the status. I'm going to be
adding these things to the struts applications SF project, today if I
can get my kids to not bug me for a bit, so that'll be their home (I
think I may combine them into a ServletUtilities class there, not sure
yet). If you or anyone wants to add these to servlet anyway I have no
objection. I would think a suitable test harness would already exist
for the package in general though, no?
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
Henri Yandell wrote:
> Servlet's basically been inactive for a couple of years as there just
> wasn't enough there to warrant a component. 2 methods in fact :)
>
> When we broke up the [util] project (lang, io, codec, various others
> were all spawned), the code was filed under servlet and left until
> more could join it.
>
> I'm happily +1 for your code to be added, but I suspect it would just
> linger. We tend not to release code without unit tests, and I know
> that I personally don't have any great itch to dig out a mock-servlet
> implementation to get the methods tested.
>
> Hen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [SERVLET] Code submission
Posted by Henri Yandell <fl...@gmail.com>.
Servlet's basically been inactive for a couple of years as there just
wasn't enough there to warrant a component. 2 methods in fact :)
When we broke up the [util] project (lang, io, codec, various others
were all spawned), the code was filed under servlet and left until
more could join it.
I'm happily +1 for your code to be added, but I suspect it would just
linger. We tend not to release code without unit tests, and I know
that I personally don't have any great itch to dig out a mock-servlet
implementation to get the methods tested.
Hen
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [SERVLET] Code submission
Posted by James Mitchell <jm...@apache.org>.
Well, if it's something you want to see actively developed and released, the
invitation is still open to you at struts on sf.net. I and others have
released many items from there. Simply post the release to the lists of
your choice and watch the download meter. If there are people actually
interested, you'll get a lot of hits and (more importantly) a lot of
questions/faq in your inbox.
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" <fz...@omnytex.com>
To: "Jakarta Commons Developers List" <co...@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"
>> <fz...@omnytex.com>
>> To: "Commons Developer" <co...@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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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: commons-dev-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
>>
>>
>>
>>
>>
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [SERVLET] Code submission
Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
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"
> <fz...@omnytex.com>
> To: "Commons Developer" <co...@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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [SERVLET] Code submission
Posted by James Mitchell <jm...@apache.org>.
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" <fz...@omnytex.com>
To: "Commons Developer" <co...@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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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:fzammetti@omnytex.com">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: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org