1 18 package org.apache.beehive.netui.util.internal; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.http.HttpSession ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletResponse ; 25 import javax.servlet.ServletRequest ; 26 import java.io.PrintStream ; 27 import java.util.Enumeration ; 28 29 public class ServletUtils 30 { 31 39 public static void dumpRequest( ServletRequest request, PrintStream output ) 40 { 41 if ( output == null ) 42 { 43 output = System.err; 44 } 45 46 output.println( "*** ServletRequest " + request ); 47 48 if ( request instanceof HttpServletRequest ) 49 { 50 output.println( " uri = " + ( ( HttpServletRequest ) request ).getRequestURI() ); 51 } 52 53 for ( Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) 54 { 55 String name = ( String ) e.nextElement(); 56 output.println( " parameter " + name + " = " + request.getParameter( name ) ); 57 } 58 59 for ( Enumeration e = request.getAttributeNames(); e.hasMoreElements(); ) 60 { 61 String name = ( String ) e.nextElement(); 62 output.println( " attribute " + name + " = " + request.getAttribute( name ) ); 63 } 64 65 if ( request instanceof HttpServletRequest ) 66 { 67 HttpSession session = ( ( HttpServletRequest ) request ).getSession( false ); 68 69 if ( session != null ) 70 { 71 for ( Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) 72 { 73 String name = ( String ) e.nextElement(); 74 output.println( " session attribute " + name + " = " + session.getAttribute( name ) ); 75 } 76 } 77 } 78 } 79 80 88 public static void dumpServletContext( ServletContext context, PrintStream output ) 89 { 90 if ( output == null ) 91 { 92 output = System.err; 93 } 94 95 output.println( "*** ServletContext " + context ); 96 97 for ( Enumeration e = context.getAttributeNames(); e.hasMoreElements(); ) 98 { 99 String name = ( String ) e.nextElement(); 100 output.println( " attribute " + name + " = " + context.getAttribute( name ) ); 101 } 102 } 103 104 109 public static void preventCache( ServletResponse response ) 110 { 111 if ( response instanceof HttpServletResponse ) 112 { 113 HttpServletResponse httpResponse = ( HttpServletResponse ) response; 114 httpResponse.setHeader( "Pragma", "No-cache" ); 115 httpResponse.setHeader( "Cache-Control", "no-cache,no-store,max-age=0" ); 116 httpResponse.setDateHeader( "Expires", 1 ); 117 } 118 } 119 120 126 public static String getBaseName( String uri ) 127 { 128 int lastSlash = uri.lastIndexOf( '/' ); 129 assert lastSlash != -1 : uri; 130 assert lastSlash < uri.length() - 1 : "URI must not end with a slash: " + uri; 131 return uri.substring( lastSlash + 1 ); 132 } 133 134 140 public static String getDirName( String uri ) 141 { 142 int lastSlash = uri.lastIndexOf( '/' ); 143 assert lastSlash != -1 : uri; 144 assert uri.length() > 1 : uri; 145 assert lastSlash < uri.length() - 1 : "URI must not end with a slash: " + uri; 146 return uri.substring( 0, lastSlash ); 147 } 148 } 149 | Popular Tags |