com.github.jerryxia.devhelper.snoop.support.servlet.SnoopServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devhelper Show documentation
Show all versions of devhelper Show documentation
This is a helper tool library in developing or testing java web application.
package com.github.jerryxia.devhelper.snoop.support.servlet;
import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.jerryxia.devhelper.snoop.JvmMemoryInfo;
import com.github.jerryxia.devhelper.snoop.MemoryPoolMXBeanInfo;
import com.github.jerryxia.devhelper.snoop.Monitor;
/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class SnoopServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JvmMemoryInfo jvmMemoryInfo = Monitor.currentMonitor().jvmMemoryInfo();
RequestInformation requestInfo = buildRequestInfo(req);
StringBuffer sb = new StringBuffer(1024 * 64);
sb.append("");
sb.append("");
sb.append("");
sb.append("");
sb.append("Servlet Snoop Page");
sb.append(" ");
sb.append("");
sb.append("");
sb.append("");
sb.append("JVM Memory Monitor
");
sb.append("");
sb.append("");
sb.append("");
sb.append("Memory MXBean");
sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");
sb.append("Heap Memory Usage ");
sb.append("");
sb.append(jvmMemoryInfo.getMemoryMXBeanInfo().getHeapMemoryUsag());
sb.append(" ");
sb.append("");
sb.append("Non-Heap Memory Usage ");
sb.append("");
sb.append(jvmMemoryInfo.getMemoryMXBeanInfo().getNonHeapMemoryUsag());
sb.append(" ");
sb.append("");
sb.append("
");
sb.append("Memory Pool MXBeans
");
for(MemoryPoolMXBeanInfo memoryPoolMXBeanInfo: jvmMemoryInfo.getMemoryPoolMXBeansInfo()) {
sb.append("");
sb.append("");
sb.append("");
sb.append(memoryPoolMXBeanInfo.getName());
sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");
sb.append("Type ");
sb.append("");
sb.append(memoryPoolMXBeanInfo.getType());
sb.append(" ");
sb.append("");
sb.append("Memory Usage ");
sb.append("");
sb.append(memoryPoolMXBeanInfo.getMemoryUsage());
sb.append(" ");
sb.append("");
sb.append("Peak Usage ");
sb.append("");
sb.append(memoryPoolMXBeanInfo.getPeakMemoryUsage());
sb.append(" ");
sb.append("");
sb.append("Collection Usage ");
sb.append("");
sb.append(memoryPoolMXBeanInfo.getCollectionUsage());
sb.append(" ");
sb.append("");
sb.append("
");
}
sb.append("Request information
");
sb.append("");
sb.append("");
sb.append("");sb.append("Requested URL");sb.append(" ");
sb.append("");sb.append(requestInfo.getRequestURL());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Request Method");sb.append(" ");
sb.append("");sb.append(requestInfo.getMethod());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Request URI");sb.append(" ");
sb.append("");sb.append(requestInfo.getRequestURI());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Request protocol");sb.append(" ");
sb.append("");sb.append(requestInfo.getProtocol());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Servlet path");sb.append(" ");
sb.append("");sb.append(requestInfo.getServletPath());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Path info");sb.append(" ");
sb.append("");sb.append(requestInfo.getPathInfo());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Path translated");sb.append(" ");
sb.append("");sb.append(requestInfo.getPathTranslated());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Query string");sb.append(" ");
sb.append("");sb.append(requestInfo.getQueryString().replaceAll("<", "<").replaceAll(">", ">"));sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Content length");sb.append(" ");
sb.append("");sb.append(requestInfo.getContentLength());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Content type");sb.append(" ");
sb.append("");sb.append(requestInfo.getContentType());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Server name");sb.append(" ");
sb.append("");sb.append(requestInfo.getServerName());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Server port");sb.append(" ");
sb.append("");sb.append(requestInfo.getServerPort());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Remote user");sb.append(" ");
sb.append("");sb.append(requestInfo.getRemoteUser());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Remote address");sb.append(" ");
sb.append("");sb.append(requestInfo.getRemoteAddr());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Remote host");sb.append(" ");
sb.append("");sb.append(requestInfo.getRemoteHost());sb.append(" ");
sb.append(" ");
sb.append("");
sb.append("");sb.append("Authorization scheme");sb.append(" ");
sb.append("");sb.append(requestInfo.getAuthType());sb.append(" ");
sb.append(" ");
sb.append("
");
sb.append("Request headers
");
sb.append("");
for(Entry item : requestInfo.getRequestHeaders().entrySet()) {
for(String val : item.getValue()) {
sb.append("");
sb.append("");sb.append(item.getKey());sb.append(" ");
sb.append("");sb.append(val);sb.append(" ");
sb.append(" ");
}
}
sb.append("
");
sb.append("Request parameters
");
sb.append("");
for(Entry item : requestInfo.getRequestParameters().entrySet()) {
for(String val : item.getValue()) {
sb.append("");
sb.append("");sb.append(item.getKey().replaceAll("<", "<").replaceAll(">", ">"));sb.append(" ");
sb.append("");sb.append(val.replaceAll("<", "<").replaceAll(">", ">"));sb.append(" ");
sb.append(" ");
}
}
sb.append("
");
sb.append("Request Attributes
");
sb.append("");
for(Entry item : requestInfo.getRequestAttributes().entrySet()) {
sb.append("");
sb.append("");sb.append(item.getKey().replaceAll("<", "<").replaceAll(">", ">"));sb.append(" ");
sb.append("");sb.append(item.getValue().toString().replaceAll("<", "<").replaceAll(">", ">"));sb.append(" ");
sb.append(" ");
}
sb.append("
");
sb.append("Init parameters
");
sb.append("");
for(Entry item : requestInfo.getInitParameters().entrySet()) {
sb.append("");
sb.append("");sb.append(item.getKey());sb.append(" ");
sb.append("");sb.append(item.getValue());sb.append(" ");
sb.append(" ");
}
sb.append("
");
sb.append("");
sb.append("");
sb.append("");
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().print(sb.toString());
}
private RequestInformation buildRequestInfo(HttpServletRequest req) {
RequestInformation requestInfo = new RequestInformation();
requestInfo.setRequestURL(req.getRequestURL().toString());
requestInfo.setMethod(req.getMethod());
requestInfo.setRequestURI(req.getRequestURI());
requestInfo.setProtocol(req.getProtocol());
requestInfo.setServletPath(req.getServletPath());
requestInfo.setPathTranslated(req.getPathTranslated());
requestInfo.setQueryString(req.getQueryString() != null ? req.getQueryString() : "");
requestInfo.setContentLength(req.getContentLength());
requestInfo.setContentType(req.getContentType());
requestInfo.setServerName(req.getServerName());
requestInfo.setServerPort(req.getServerPort());
requestInfo.setRemoteUser(req.getRemoteUser());
requestInfo.setRemoteAddr(req.getRemoteAddr());
requestInfo.setRemoteHost(req.getRemoteHost());
requestInfo.setAuthType(req.getAuthType());
Map requestHeaders = new LinkedHashMap();
Enumeration headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headName = headerNames.nextElement();
LinkedList arrayVals = new LinkedList();
Enumeration vals = req.getHeaders(headName);
while(vals.hasMoreElements()) {
arrayVals.add(vals.nextElement());
}
requestHeaders.put(headName, arrayVals.toArray(new String[arrayVals.size()]));
}
requestInfo.setRequestHeaders(requestHeaders);
Map requestParameters = new LinkedHashMap(req.getParameterMap());
requestInfo.setRequestParameters(requestParameters);
Map requestAttributes = new LinkedHashMap();
Enumeration attributeNames = req.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
Object val = req.getAttribute(attributeName);
requestAttributes.put(attributeName, val);
}
requestInfo.setRequestAttributes(requestAttributes);
Map initParameters = new LinkedHashMap();
Enumeration initParameterNames = getServletConfig().getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String initParameterName = initParameterNames.nextElement();
String val = getServletConfig().getInitParameter(initParameterName);
initParameters.put(initParameterName, val);
}
requestInfo.setInitParameters(initParameters);
return requestInfo;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy