1 package com.diasparsoftware.javax.servlet.http; 2 3 import java.util.*; 4 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.http.*; 7 8 import org.apache.catalina.connector.HttpRequestBase; 9 10 import com.diasparsoftware.java.util.*; 11 12 public class HttpUtil { 13 /*** 14 * Create an HttpServletRequest from the specified URI and request 15 * parameters. 16 * 17 * @param uri 18 * @param parameters 19 * @return 20 */ 21 public static HttpServletRequest makeRequestIgnoreSession( 22 String uri, 23 Map parameters) { 24 25 final HttpRequestBase httpServletRequest = 26 new HttpRequestBase() { 27 28 public HttpSession getSession(boolean create) { 29 return new FakeHttpSession(Collections.EMPTY_MAP); 30 } 31 32 public RequestDispatcher getRequestDispatcher(String path) { 33 return new RequestDispatcherAdapter(); 34 } 35 }; 36 37 httpServletRequest.setRequestURI(uri); 38 httpServletRequest.clearParameters(); 39 40 CollectionUtil.forEachDo(parameters, new MapEntryClosure() { 41 public void eachMapEntry(Object key, Object value) { 42 httpServletRequest.addParameter( 43 (String) key, 44 (String[]) value); 45 } 46 }); 47 48 return httpServletRequest; 49 } 50 51 /*** 52 * Provides a human-readable string representation of an <code>HttpSession</code>. 53 * 54 * @param session 55 * @return For now, just the session attributes. 56 */ 57 public static String sessionToString(HttpSession session) { 58 StringBuffer buffer = new StringBuffer("HttpSession {"); 59 boolean needComma = false; 60 61 for (Enumeration e = session.getAttributeNames(); 62 e.hasMoreElements(); 63 ) { 64 65 String eachName = (String) e.nextElement(); 66 Object eachValue = session.getAttribute(eachName); 67 68 if (needComma) 69 buffer.append(","); 70 71 buffer.append(eachName).append("=").append(eachValue); 72 73 needComma = true; 74 } 75 76 buffer.append("}"); 77 return buffer.toString(); 78 } 79 }