KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > ServletUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletResponse JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 public class ServletUtils
30 {
31     /**
32      * Print parameters and attributes in the given request.
33      *
34      * @param request the current HttpServletRequest.
35      * @param output a PrintStream to which to output request parameters and request/session
36      * attributes; if <code>null</null>, <code>System.err</code> is used.
37      *
38      */

39     public static void dumpRequest( ServletRequest JavaDoc request, PrintStream JavaDoc output )
40     {
41         if ( output == null )
42         {
43             output = System.err;
44         }
45
46         output.println( "*** ServletRequest " + request );
47         
48         if ( request instanceof HttpServletRequest JavaDoc )
49         {
50             output.println( " uri = " + ( ( HttpServletRequest JavaDoc ) request ).getRequestURI() );
51         }
52
53         for ( Enumeration JavaDoc e = request.getParameterNames(); e.hasMoreElements(); )
54         {
55             String JavaDoc name = ( String JavaDoc ) e.nextElement();
56             output.println( " parameter " + name + " = " + request.getParameter( name ) );
57         }
58
59         for ( Enumeration JavaDoc e = request.getAttributeNames(); e.hasMoreElements(); )
60         {
61             String JavaDoc name = ( String JavaDoc ) e.nextElement();
62             output.println( " attribute " + name + " = " + request.getAttribute( name ) );
63         }
64
65         if ( request instanceof HttpServletRequest JavaDoc )
66         {
67             HttpSession JavaDoc session = ( ( HttpServletRequest JavaDoc ) request ).getSession( false );
68             
69             if ( session != null )
70             {
71                 for ( Enumeration JavaDoc e = session.getAttributeNames(); e.hasMoreElements(); )
72                 {
73                     String JavaDoc name = ( String JavaDoc ) e.nextElement();
74                     output.println( " session attribute " + name + " = " + session.getAttribute( name ) );
75                 }
76             }
77         }
78     }
79
80     /**
81      * Print attributes in the given ServletContext.
82      *
83      * @param context the current ServletContext.
84      * @param output a PrintStream to which to output ServletContext attributes; if <code>null</null>,
85                      <code>System.err</code> is used.
86      *
87      */

88     public static void dumpServletContext( ServletContext JavaDoc context, PrintStream JavaDoc output )
89     {
90         if ( output == null )
91         {
92             output = System.err;
93         }
94
95         output.println( "*** ServletContext " + context );
96
97         for ( Enumeration JavaDoc e = context.getAttributeNames(); e.hasMoreElements(); )
98         {
99             String JavaDoc name = ( String JavaDoc ) e.nextElement();
100             output.println( " attribute " + name + " = " + context.getAttribute( name ) );
101         }
102     }
103     
104     /**
105      * Set response headers to prevent caching of the response by the browser.
106      *
107      * @param response the current ServletResponse
108      */

109     public static void preventCache( ServletResponse JavaDoc response )
110     {
111         if ( response instanceof HttpServletResponse JavaDoc )
112         {
113             HttpServletResponse JavaDoc httpResponse = ( HttpServletResponse JavaDoc ) 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     /**
121      * Get the base filename of the given URI.
122      *
123      * @param uri the URI from which to get the base filename.
124      * @return a String containing everything after the last slash of the given URI.
125      */

126     public static String JavaDoc getBaseName( String JavaDoc 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     /**
135      * Get the directory path of the given URI.
136      *
137      * @param uri the URI from which to get the directory path.
138      * @return a String containing everything before the last slash of the given URI.
139      */

140     public static String JavaDoc getDirName( String JavaDoc 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