com.jamonapi.http.HttpMonFactory Maven / Gradle / Ivy
package com.jamonapi.http;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/** Base class that monitors a httpServletRequest, and HttpServletResponse by returning an HttpMonRequest object per page request. Although this
* class can be used directly it will more often be used transparently by classes such as JAMonTomcat55Valve, JAMonServletFilter and JAMonJettyHandler.
* To get a list of many possible (though not all) HttpServletRequest and HttpServletResponse labels pass 'demo' to the valve, handler or servlet filter.
* You can also see these same possibiliteis by calling HttpMonFactory.getDemoLabels(). Note jetty and tomcat handler and valve respectively
* are based on objects that inherit from HttpServletRequest and HttpServletResponse respectively and all methods that these subclasses
* implement are available too.
*
* Representative values returned from response methods
*
* - response.getBufferSize()=8192
*
- response.getCharacterEncoding()=utf-8
*
- response.getContentCount()=985
*
- response.getContentType()=text/html;charset=utf-8
*
- response.getLocale()=en_US
*
*
* Representative values returned from request methods
*
* - request.getAuthType()=null
*
- request.getCharacterEncoding()=null
*
- request.getContentLength()=-1
*
- request.getCharacterEncoding()=null
*
- request.getContentType()=null
*
- request.getContextPath()=/jamon
*
- request.getLocalAddr()=127.0.0.1
*
- request.getLocale()=en_US
*
- request.getLocalName()=localhost
*
- request.getLocalPort()=8080
*
- request.getMethod()=GET
*
- request.getPathInfo()=null
*
- request.getPathTranslated()=null
*
- request.getProtocol()=HTTP/1.1
*
- request.getQueryString()=name=steve%20souza&id=9898
*
- request.getRemoteAddr()=127.0.0.1
*
- request.getRemoteHost()=127.0.0.1
*
- request.getRemotePort()=1454
*
- request.getRemoteUser()=null
*
- request.getRequestedSessionId()=670BFE2B4A7C7C77D9825EFA753D2058
*
- request.getRequestURI()=/jamon/ZZZZ
*
- request.getRequestURL()=http://localhost:8080/jamon/ZZZZ
*
- request.getScheme()=http
*
- request.getServerName()=localhost
*
- request.getServerPort()=8080
*
- request.getServletPath()=/ZZZZ
*
- request.getUserPrincipal()=null
*
- request.isRequestedSessionIdFromCookie()=true
*
- request.isRequestedSessionIdFromURL()=false
*
- request.isRequestedSessionIdValid()=false
*
- request.isSecure()=false
*
*
*/
public class HttpMonFactory implements HttpMonManage, Serializable {
private static final String DEFAULT_SUMMARY="request.getRequestURI().ms as allPages, request.getRequestURI().value.ms as page";
private static final long serialVersionUID = 278L;
private String jamonSummaryLabels="default";// will do the above monitors if the word default is used in this variable.
private Collection httpMonItemsHolder=new ArrayList();// Holds HttpMonItems
private boolean ignoreHttpParams=true;// ignore http params if getRequestURI, or getRequestURL are called. This done to primarily to prevent
// jsessionid from becoming part of a jamon label. By default params are removed (i.e. true)
private String labelPrefix; // prefix used for jamon labels
private boolean enabled=true; //Enable/Disable httpMonitoring. By default it is on
private int numTimeMons=0;// The Number of monitors that are time based ones.
// The size value will not allow any more http stats to be put into jamon if the total number of jamon entries exceeds 5000 entries.
// This value may be changed to anything. Note jamon entries can still be added via standard jamon calls (might want to add this feature
// there too). This is to prevent a buffer overflow should someone keep submitting invalid pages when a record is created for each page.
private int size=5000;
private static final HttpMon NULL_HTTP_MON=new HttpMonNull();// used when monitoring is disabled.
/** Create an HttpMonFactory by passing in text that occurs at the beginning of all jamon labels. ex com.jamonapi.http.JAMonTomcatValve */
public HttpMonFactory(String labelPrefix) {
this.labelPrefix=labelPrefix;
}
/** Pass a series of things (HttpServletRequest/HttpServletResponse methods) to monitor. If the word 'default' is passed then the default
* values will be used. If 'demo' is passed you will see a list of possibilities (This should not be done in production as too much data would be
* generated). You can add to the default by doing passing "default, request.getStatus().httpStatus". Each time this method is called any previously
* set items that were being monitored will not longer be monitored. See http://www.jamonapi.com for more examples.
*/
public void setSummaryLabels(String jamonSummaryLabels) {
this.jamonSummaryLabels="";
this.httpMonItemsHolder=new ArrayList();
this.numTimeMons=0;
if (jamonSummaryLabels==null)
return;
// replace the word 'demo' with a good sampling of possibilities
jamonSummaryLabels=jamonSummaryLabels.replaceAll("(?i)demo", getDemoLabels());
// replace string 'default' with the actual default values
jamonSummaryLabels=replaceDefault(jamonSummaryLabels, DEFAULT_SUMMARY);
// tokenize the string and add each of the HttpMonItems
String[] summaryLabelsArr=split(jamonSummaryLabels);
for (int i=0;i