日期:2014-05-16 浏览次数:20501 次
RequestDumpUtil.java
packageexamples.jsf.util;
importjava.util.Collections;
importjava.util.Enumeration;
importjava.util.Iterator;
importjava.util.Locale;
importjava.util.SortedSet;
importjava.util.TreeSet;
importjavax.servlet.ServletContext;
importjavax.servlet.http.Cookie;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
publicclassRequestDumpUtil{
publicstaticvoiddumpRequestHeaders(finalStringBuffersb,
finalHttpServletRequestrequest,finalStringlf,
finalStringindent){
for(finalIteratorit=toSortedSet(request.getHeaderNames())
.iterator();it.hasNext();){
finalStringname=(String)it.next();
finalStringvalue=request.getHeader(name);
sb.append(indent);
sb.append("[header]").append(name);
sb.append("=").append(value);
sb.append(lf);
}
}
publicstaticvoiddumpContextAttributes(finalStringBuffersb,
finalServletContextservletContext,finalStringlf,
finalStringindent){
if(servletContext==null){
return;
}
for(finalIteratorit=toSortedSet(servletContext.getAttributeNames())
.iterator();it.hasNext();){
finalStringname=(String)it.next();
finalObjectattr=servletContext.getAttribute(name);
sb.append(indent);
sb.append("[context]").append(name).append("=").append(attr);
sb.append(lf);
}
}
publicstaticvoiddumpCookies(finalStringBuffersb,
finalHttpServletRequestrequest,finalStringlf,
finalStringindent){
Cookiecookies[]=request.getCookies();
if(cookies==null){
cookies=newCookie[0];
}
for(inti=0;i<cookies.length;i++){
sb.append(indent);
sb.append("[cookie]").append(cookies[i].getName());
sb.append("=").append(cookies[i].getValue());
sb.append(lf);
}
}
publicstaticvoiddumpRequestAttributes(finalStringBuffersb,
finalHttpServletRequestrequest,finalStringlf,
finalStringindent){
for(finalIteratorit=toSortedSet(request.getAttributeNames())
.iterator();it.hasNext();){
finalStringname=(String)it.next();
finalObjectattr=request.getAttribute(name);
sb.append(indent);
sb.append("[request]").append(name).append("=").append(attr);
sb.append(lf);
}
}
publicstaticvoiddumpSessionAttributes(finalStringBuffersb,
finalHttpServletRequestrequest,finalStringlf,
finalStringindent){
finalHttpSessionsession=request.getSession(false);
if(session==null){
return;
}
for(finalIteratorit=toSortedSet(session.getAttributeNames())
.iterator();it.hasNext();){
finalStringname=(String)it.next();
finalObjectattr=session.getAttribute(name);
sb.append(indent);
sb.append("[session]").append(name).append("=").append(attr);
sb.append(lf);
}
}
privatestaticSortedSettoSortedSet(finalEnumerationenu){
finalSortedSetset=newTreeSet();
set.addAll(Collections.list(enu));
returnset;
}
publicstaticvoiddumpRequestParameters(finalStringBuffersb,
finalHttpServletRequestrequest,finalStringlf,
finalStringindent){
for(finalIteratorit=toSortedSet(request.getParameterNames())
.iterator();it.hasNext();){
finalStringname=(String)it.next();
sb.append(indent);
sb.append("[param]").append(name).append("=");
finalStringvalues[]=request.getParameterValues(name);
for(inti=0;i<values.length;i++){
if(i>0){
sb.append(",");
}
sb.append(values[i]);
}
sb.append(lf);
}
}
publicstaticvoiddumpRequestAndContextProperties(finalStringBuffersb,
finalHt