From: <mr...@us...> - 2003-01-08 05:54:33
|
Update of /cvsroot/struts/struts-resume/src/web/org/appfuse/webapp/util In directory sc8-pr-cvs1:/tmp/cvs-serv30082 Added Files: BaseUtil.java ConvertUtil.java RequestUtil.java SslUtil.java Log Message: --- NEW FILE: BaseUtil.java --- package org.appfuse.webapp.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * BaseUtil is used as a parent class for all other classes in the * webapp.utils package. */ public class BaseUtil { //~ Instance fields ======================================================== /** The <code>Log</code> instance for this package. */ protected Log log = LogFactory.getLog("org.appfuse.webapp.util"); } --- NEW FILE: ConvertUtil.java --- package org.appfuse.webapp.util; import org.apache.commons.beanutils.ConvertUtils; //import org.apache.commons.beanutils.converters.StringConverter; //import org.apache.commons.beanutils.converters.BlankStringToNullIntegerConverter; /** * Conversion utility to convert ActionForm's String properties * to real datatypes. * * <p> * <a href="ConvertUtil.java.html"><i>View Source</i></a> * </p> * * @author Matt Raible * @version $Revision: 1.1 $ $Date: 2003/01/08 05:54:31 $ */ public class ConvertUtil extends ConvertUtils { /** singleton's private instance */ private static ConvertUtil me; static { me = new ConvertUtil(); } private ConvertUtil() { } /** returns the Service Locator instance */ static public ConvertUtil getInstance() { return me; } } --- NEW FILE: RequestUtil.java --- /** * RequestUtil utility class Good ol' copy-n-paste from <a * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt"> * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a> * which is referenced in the following article: <a * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html"> * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a> */ package org.appfuse.webapp.util; import org.apache.struts.util.RequestUtils; // java imports import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RequestUtil extends BaseUtil { //~ Static fields/initializers ============================================= private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed"; //~ Methods ================================================================ //private static String ALGORITHM; /** * Creates query String from request body parameters * * @param aRequest DOCUMENT ME! * * @return DOCUMENT ME! */ public static String getRequestParameters(HttpServletRequest aRequest) { // set the ALGORIGTHM as defined for the application //ALGORITHM = (String) aRequest.getAttribute(Constants.ENC_ALGORITHM); Map m = aRequest.getParameterMap(); return createQueryStringFromMap(m, "&").toString(); } /** * Builds a query string from a given map of parameters * * @param m A map of parameters * @param ampersand String to use for ampersands (e.g. "&" or "&" ) * * @return query string (with no leading "?") */ public static StringBuffer createQueryStringFromMap(Map m, String ampersand) { StringBuffer aReturn = new StringBuffer(""); Set aEntryS = m.entrySet(); Iterator aEntryI = aEntryS.iterator(); while (aEntryI.hasNext()) { Map.Entry aEntry = (Map.Entry) aEntryI.next(); Object o = aEntry.getValue(); if (o == null) { append(aEntry.getKey(), "", aReturn, ampersand); } else if (o instanceof String) { append(aEntry.getKey(), o, aReturn, ampersand); } else if (o instanceof String[]) { String[] aValues = (String[]) o; for (int i = 0; i < aValues.length; i++) { append(aEntry.getKey(), aValues[i], aReturn, ampersand); } } else { append(aEntry.getKey(), o, aReturn, ampersand); } } return aReturn; } /** * Appends new key and value pair to query string * * @param key parameter name * @param value value of parameter * @param queryString existing query string * @param ampersand string to use for ampersand (e.g. "&" or "&") * * @return query string (with no leading "?") */ private static StringBuffer append(Object key, Object value, StringBuffer queryString, String ampersand) { if (queryString.length() > 0) { queryString.append(ampersand); } // Use encodeURL from Struts' RequestUtils class - it's JDK 1.3 and 1.4 compliant queryString.append(RequestUtils.encodeURL(key.toString())); queryString.append("="); queryString.append(RequestUtils.encodeURL(value.toString())); return queryString; } /** * Stores request attributes in session * * @param aRequest DOCUMENT ME! */ public static void stowRequestAttributes(HttpServletRequest aRequest) { if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) { return; } Enumeration enum = aRequest.getAttributeNames(); Map map = new HashMap(); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); map.put(name, aRequest.getAttribute(name)); } aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map); } /** * Returns request attributes from session to request * * @param aRequest DOCUMENT ME! */ public static void reclaimRequestAttributes(HttpServletRequest aRequest) { Map map = (Map) aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS); if (map == null) { return; } Iterator itr = map.keySet().iterator(); while (itr.hasNext()) { String name = (String) itr.next(); aRequest.setAttribute(name, map.get(name)); } aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS); } /** * Convenience method to get a cookie by name * * @param request DOCUMENT ME! * @param name DOCUMENT ME! * * @return DOCUMENT ME! */ public static Cookie getCookie(HttpServletRequest request, String name) { Cookie[] cookies = request.getCookies(); Cookie returnCookie = null; for (int i = 0; i < cookies.length; i++) { Cookie thisCookie = cookies[i]; if (thisCookie.getName().equals(name)) { returnCookie = thisCookie; break; } } return returnCookie; } /** * Convenience method for deleting a cookie by name * * @param response DOCUMENT ME! * @param cookie DOCUMENT ME! * * @return DOCUMENT ME! */ public static HttpServletResponse deleteCookie(HttpServletResponse response, Cookie cookie) { // Delete the cookie by setting its maximum age to zero cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); return response; } } --- NEW FILE: SslUtil.java --- package org.appfuse.webapp.util; import org.appfuse.common.Constants; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; /** * SslUtil utility class Good ol' copy-n-paste from <a * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt"> * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a> * which is referenced in the following article: <a * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html"> * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a> */ public class SslUtil extends BaseUtil { //~ Static fields/initializers ============================================= public static final String HTTP = "http"; public static final String HTTPS = "https"; public static final String HTTP_PORT_PARAM = "listenPort_http"; public static final String HTTPS_PORT_PARAM = "listenPort_https"; private static String HTTP_PORT = null; private static String HTTPS_PORT = null; public static final String STD_HTTP_PORT = "80"; public static final String STD_HTTPS_PORT = "443"; //~ Methods ================================================================ static public String getRedirectString(HttpServletRequest request, ServletContext servletContext, boolean isSecure) { // get the port numbers from the application context HTTP_PORT = (String) servletContext.getAttribute(Constants.HTTP_PORT); HTTPS_PORT = (String) servletContext.getAttribute(Constants.HTTPS_PORT); // get the scheme we want to use for this page and // get the scheme used in this request String desiredScheme = isSecure ? HTTPS : HTTP; String usingScheme = request.getScheme(); // Determine the port number we want to use // and the port number we used in this request String desiredPort = isSecure ? HTTPS_PORT : HTTP_PORT; String usingPort = String.valueOf(request.getServerPort()); String urlString = null; // Must also check ports, because of IE multiple redirect problem if (!desiredScheme.equals(usingScheme) || !desiredPort.equals(usingPort)) { urlString = buildNewUrlString(request, desiredScheme, usingScheme, desiredPort, usingPort); // Temporarily store attributes in session RequestUtil.stowRequestAttributes(request); } else { // Retrieve attributes from session RequestUtil.reclaimRequestAttributes(request); } return urlString; } /** * Builds the URL that we will redirect to * * @param request DOCUMENT ME! * @param desiredScheme DOCUMENT ME! * @param usingScheme DOCUMENT ME! * @param desiredPort DOCUMENT ME! * @param usingPort DOCUMENT ME! * * @return DOCUMENT ME! */ private static String buildNewUrlString(HttpServletRequest request, String desiredScheme, String usingScheme, String desiredPort, String usingPort) { StringBuffer url = request.getRequestURL(); url.replace(0, usingScheme.length(), desiredScheme); // Find the port used within the URL string int startIndex = url.toString().indexOf(usingPort); if (startIndex == -1) { // Port not found in URL if ((!(STD_HTTPS_PORT.equals(desiredPort) && HTTPS.equals(desiredScheme))) && (!(STD_HTTP_PORT.equals(desiredPort) && HTTP.equals(desiredScheme)))) { startIndex = url.toString().indexOf("/", url.toString().indexOf("/", url.toString() .indexOf("/") + 1) + 1); url.insert(startIndex, ":" + desiredPort); } } else { // Port found in URL if ((STD_HTTPS_PORT.equals(desiredPort) && HTTPS.equals(desiredScheme)) || (STD_HTTP_PORT.equals(desiredPort) && HTTP.equals(desiredScheme))) { url.delete(startIndex - 1, startIndex + usingPort.length()); } else { // desired port is not a default port // Replace requested port with desired port number in URL string url.replace(startIndex, startIndex + usingPort.length(), desiredPort); } } // add query string, if any String queryString = request.getQueryString(); if ((queryString != null) && (queryString.length() != 0)) { url.append("?" + queryString); } else { queryString = RequestUtil.getRequestParameters(request); if ((queryString != null) && (queryString.length() != 0)) { url.append("?" + queryString); } } return url.toString(); } } |