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