Author: bayard Date: Tue Mar 8 16:51:49 2005 New Revision: 156588 URL: http://svn.apache.org/viewcvs?view=rev&rev=156588 Log: fixed the build (somewhat, still need to edit buld.properties.sample), added the new ideas from Frank Zammetti (Bugzilla: #33676) and fixed the existing code so it actually compiles.
Added: jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/SessionUtils.java Modified: jakarta/commons/sandbox/servlet/trunk/build.properties.sample jakarta/commons/sandbox/servlet/trunk/build.xml jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/RequestUtils.java Modified: jakarta/commons/sandbox/servlet/trunk/build.properties.sample URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/servlet/trunk/build.properties.sample?view=diff&r1=156587&r2=156588 ============================================================================== --- jakarta/commons/sandbox/servlet/trunk/build.properties.sample (original) +++ jakarta/commons/sandbox/servlet/trunk/build.properties.sample Tue Mar 8 16:51:49 2005 @@ -4,3 +4,5 @@ # The pathname of the "junit.jar" JAR file junit.jar = ${junit.home}/junit.jar + +servlet.jar=${user.home}/.maven/repository/servletapi/jars/servletapi-2.2.jar Modified: jakarta/commons/sandbox/servlet/trunk/build.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/servlet/trunk/build.xml?view=diff&r1=156587&r2=156588 ============================================================================== --- jakarta/commons/sandbox/servlet/trunk/build.xml (original) +++ jakarta/commons/sandbox/servlet/trunk/build.xml Tue Mar 8 16:51:49 2005 @@ -76,6 +76,7 @@ <!-- Construct compile classpath --> <path id="compile.classpath"> <pathelement location="${build.home}/classes"/> + <pathelement location="${servlet.jar}"/> </path> Modified: jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/RequestUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/RequestUtils.java?view=diff&r1=156587&r2=156588 ============================================================================== --- jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/RequestUtils.java (original) +++ jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/RequestUtils.java Tue Mar 8 16:51:49 2005 @@ -18,13 +18,16 @@ import javax.servlet.http.HttpServletRequest; -import org.apache.commons.lang.Strings; + +import java.util.Enumeration; +import java.util.HashMap; /** * This class provides utilities for getting information from * javax.servlet.http.HttpServletRequest. * * @author <a href="mailto:[EMAIL PROTECTED]">Lance Lavandowska</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Frank W. Zammetti</a> * @version $Id$ */ public class RequestUtils @@ -53,7 +56,7 @@ if (headerValue == null) { // dash w/ title case - header = Strings.replace(header, "_", "-"); + header = header.replaceAll("_", "-"); headerValue = request.getHeader(header); if (headerValue == null) { @@ -68,7 +71,7 @@ if (headerValue == null) { // underscore w/ all lower - header = Strings.replace(header, "-", "_"); + header = header.replaceAll("-", "_"); headerValue = request.getHeader(header); } } @@ -107,4 +110,77 @@ } return header; } + + + /** + * This method is a convenience method that calls the other three and + * returns a HashMap containing all request attributes, parameters and headers + * + * @param request A valid HTTPServletRequest object + */ + public static HashMap getAllRequestInfo(HttpServletRequest request) { + + HashMap hm = new HashMap(); + hm.put("Request Attributes", getRequestAttributes(request)); + hm.put("Request Parameters", getRequestParameters(request)); + hm.put("Request Headers", getRequestHeaders(request)); + return hm; + + } + + + /** + * This method will return a HashMap that can be displayed which contains + * all request attributes. + * + * @param request A valid HTTPServletRequest object + */ + public static HashMap getRequestAttributes(HttpServletRequest request) { + + HashMap hm = new HashMap(); + for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) { + String next = (String)en.nextElement(); + hm.put(next, request.getAttribute(next)); + } + return hm; + + } + + + /** + * This method will return a HashMap that can be displayed which contains + * all request parameters. + * + * @param request A valid HTTPServletRequest object + */ + public static HashMap getRequestParameters(HttpServletRequest request) { + + HashMap hm = new HashMap(); + for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) { + String next = (String)en.nextElement(); + hm.put(next, request.getParameter(next)); + } + return hm; + + } + + + /** + * This method will return a HashMap that can be displayed which contains + * all request headers. + * + * @param request A valid HTTPServletRequest object + */ + public static HashMap getRequestHeaders(HttpServletRequest request) { + + HashMap hm = new HashMap(); + for (Enumeration en = request.getHeaderNames(); en.hasMoreElements();) { + String next = (String)en.nextElement(); + hm.put(next, request.getHeader(next)); + } + return hm; + + } + + } Added: jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/SessionUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/SessionUtils.java?view=auto&rev=156588 ============================================================================== --- jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/SessionUtils.java (added) +++ jakarta/commons/sandbox/servlet/trunk/src/java/org/apache/commons/servlet/SessionUtils.java Tue Mar 8 16:51:49 2005 @@ -0,0 +1,181 @@ +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 javax.servlet.http.HttpServletRequest; +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; + +import java.util.Enumeration; +import java.util.HashMap; + + +/** + * 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 will return a HashMap that can be displayed which contains + * all session attributes. + * + * @param request A valid HttpServletRequest object + */ + public static HashMap getSessionAttributes(HttpServletRequest request) { + + 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)); + } + } + return session_attributes; + } + + /** + * This method is used to get the total size of a current, valid HttpSession + * object it is passed. + * + * @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 obj The object to test + * @return 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 fld The field to get priveleges on + * @param obj The object to test + * @return 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 + --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]