View Javadoc

1   /*
2    * Copyright 2004-2007 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.util;
17  
18  import java.util.Collections;
19  import java.util.Enumeration;
20  import java.util.Iterator;
21  import java.util.Locale;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  import javax.servlet.ServletContext;
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  /**
32   * @author manhole
33   */
34  public class RequestDumpUtil {
35  
36      @SuppressWarnings("unchecked")
37  	public static void dumpRequestHeaders(final StringBuffer sb,
38              final HttpServletRequest request, final String lf,
39              final String indent) {
40          for (final Iterator it = toSortedSet(request.getHeaderNames())
41                  .iterator(); it.hasNext();) {
42              final String name = (String) it.next();
43              final String value = request.getHeader(name);
44              sb.append(indent);
45              sb.append("[header]").append(name);
46              sb.append("=").append(value);
47              sb.append(lf);
48          }
49      }
50  
51      @SuppressWarnings("unchecked")
52  	public static void dumpContextAttributes(final StringBuffer sb,
53              final ServletContext context, final String lf, final String indent) {
54          if (context == null) {
55              return;
56          }
57          for (final Iterator it = toSortedSet(context.getAttributeNames())
58                  .iterator(); it.hasNext();) {
59              final String name = (String) it.next();
60              final Object attr = context.getAttribute(name);
61              sb.append(indent);
62              sb.append("[context]").append(name).append("=").append(attr);
63              sb.append(lf);
64          }
65      }
66  
67      public static void dumpCookies(final StringBuffer sb,
68              final HttpServletRequest request, final String lf,
69              final String indent) {
70          final Cookie cookies[] = request.getCookies();
71          if (cookies == null) {
72              return;
73          }
74          for (int i = 0; i < cookies.length; i++) {
75              sb.append(indent);
76              sb.append("[cookie]").append(cookies[i].getName());
77              sb.append("=").append(cookies[i].getValue());
78              sb.append(lf);
79          }
80      }
81  
82      @SuppressWarnings("unchecked")
83  	public static void dumpRequestAttributes(final StringBuffer sb,
84              final HttpServletRequest request, final String lf,
85              final String indent) {
86          for (final Iterator it = toSortedSet(request.getAttributeNames())
87                  .iterator(); it.hasNext();) {
88              final String name = (String) it.next();
89              final Object attr = request.getAttribute(name);
90              sb.append(indent);
91              sb.append("[request]").append(name).append("=").append(attr);
92              sb.append(lf);
93          }
94      }
95  
96      @SuppressWarnings("unchecked")
97  	public static void dumpSessionAttributes(final StringBuffer sb,
98              final HttpServletRequest request, final String lf,
99              final String indent) {
100         final HttpSession session = request.getSession(false);
101         if (session == null) {
102             return;
103         }
104         for (final Iterator it = toSortedSet(session.getAttributeNames())
105                 .iterator(); it.hasNext();) {
106             final String name = (String) it.next();
107             final Object attr = session.getAttribute(name);
108             sb.append(indent);
109             sb.append("[session]").append(name).append("=").append(attr);
110             sb.append(lf);
111         }
112     }
113 
114     @SuppressWarnings("unchecked")
115 	private static SortedSet toSortedSet(final Enumeration<?> enu) {
116         final SortedSet set = new TreeSet();
117         set.addAll(Collections.list(enu));
118         return set;
119     }
120 
121     @SuppressWarnings("unchecked")
122 	public static void dumpRequestParameters(final StringBuffer sb,
123             final HttpServletRequest request, final String lf,
124             final String indent) {
125         for (final Iterator it = toSortedSet(request.getParameterNames())
126                 .iterator(); it.hasNext();) {
127             final String name = (String) it.next();
128             sb.append(indent);
129             sb.append("[param]").append(name).append("=");
130             final String values[] = request.getParameterValues(name);
131             for (int i = 0; i < values.length; i++) {
132                 if (i > 0) {
133                     sb.append(", ");
134                 }
135                 sb.append(values[i]);
136             }
137             sb.append(lf);
138         }
139     }
140 
141     @SuppressWarnings("unchecked")
142 	public static void dumpRequestProperties(final StringBuffer sb,
143             final HttpServletRequest request, final String lf,
144             final String indent) {
145 
146         sb.append(indent);
147         sb.append("Request class=" + request.getClass().getName()).append(
148                 ", instance=").append(request.toString().trim());
149 
150         sb.append(lf);
151         sb.append(indent);
152 
153         sb.append("RequestedSessionId=")
154                 .append(request.getRequestedSessionId());
155 
156         sb.append(lf);
157         sb.append(indent);
158 
159         sb.append("REQUEST_URI=").append(request.getRequestURI());
160         sb.append(", SERVLET_PATH=").append(request.getServletPath());
161 
162         sb.append(lf);
163         sb.append(indent);
164         sb.append("CharacterEncoding=" + request.getCharacterEncoding());
165         sb.append(", ContentLength=").append(request.getContentLength());
166         sb.append(", ContentType=").append(request.getContentType());
167         sb.append(", Locale=").append(request.getLocale());
168         sb.append(", Locales=");
169         final Enumeration locales = request.getLocales();
170         boolean first = true;
171         while (locales.hasMoreElements()) {
172             final Locale locale = (Locale) locales.nextElement();
173             if (first) {
174                 first = false;
175             } else {
176                 sb.append(", ");
177             }
178             sb.append(locale.toString());
179         }
180         sb.append(", Scheme=").append(request.getScheme());
181         sb.append(", isSecure=").append(request.isSecure());
182 
183         sb.append(lf).append(indent);
184 
185         sb.append("SERVER_PROTOCOL=").append(request.getProtocol());
186         sb.append(", REMOTE_ADDR=").append(request.getRemoteAddr());
187         sb.append(", REMOTE_HOST=").append(request.getRemoteHost());
188         sb.append(", SERVER_NAME=").append(request.getServerName());
189         sb.append(", SERVER_PORT=").append(request.getServerPort());
190 
191         sb.append(lf).append(indent);
192 
193         sb.append("ContextPath=").append(request.getContextPath());
194         sb.append(", REQUEST_METHOD=").append(request.getMethod());
195         sb.append(", QUERY_STRING=").append(request.getQueryString());
196         sb.append(", PathInfo=").append(request.getPathInfo());
197         sb.append(", RemoteUser=").append(request.getRemoteUser());
198 
199         sb.append(lf);
200 
201     }
202 
203     public static void dumpContextProperties(final StringBuffer sb,
204             final ServletContext context, final String lf, final String indent) {
205         sb.append(indent);
206         sb.append("ContextRealPath=").append(context.getRealPath("/"));
207 
208         sb.append(lf).append(indent);
209 
210         sb.append("SERVER_SOFTWARE=").append(context.getServerInfo());
211         sb.append(", ServletContextName=").append(
212                 context.getServletContextName());
213         sb.append(", MajorVersion=").append(context.getMajorVersion());
214         sb.append(", MinorVersion=").append(context.getMinorVersion());
215     }
216 
217     public static void dumpSessionProperties(final StringBuffer sb,
218             final HttpServletRequest request, final String lf,
219             final String indent) {
220         final HttpSession session = request.getSession(false);
221         if (session == null) {
222             return;
223         }
224         sb.append(indent);
225         sb.append("Session SessionId=").append(session.getId());
226         sb.append(lf).append(indent);
227         sb.append("Session :: CreationTime=").append(session.getCreationTime());
228         sb.append(", LastAccessedTime=").append(session.getLastAccessedTime());
229         sb.append(", MaxInactiveInterval=").append(
230                 session.getMaxInactiveInterval());
231         sb.append(lf);
232     }
233 
234     public static void dumpResponseProperties(final StringBuffer sb,
235             final HttpServletResponse response, final String lf,
236             final String indent) {
237         sb.append(indent);
238         sb.append("Response class=" + response.getClass().getName()).append(
239                 ", instance=").append(response.toString().trim());
240         sb.append(lf);
241     }
242 
243 }