FileDocCategorySizeDatePackage
DefaultAccessLogFormatterImpl.javaAPI DocGlassfish v2 API28665Wed Jun 20 22:06:04 BST 2007com.sun.enterprise.web.accesslog

DefaultAccessLogFormatterImpl

public class DefaultAccessLogFormatterImpl extends AccessLogFormatter
Access log formatter using the SJSAS format.

Fields Summary
private static final Logger
_logger
private static final ResourceBundle
_rb
private static final String
QUOTE
private static final String
HTTP_HEADER_ACCEPT
private static final String
HTTP_HEADER_AUTHORIZATION
private static final String
HTTP_HEADER_DATE
private static final String
HTTP_HEADER_IF_MODIFIED_SINCE
private static final String
ATTRIBUTE_BY_NAME_PREFIX
private static final int
ATTRIBUTE_BY_NAME_PREFIX_LEN
private static final String
AUTH_USER_NAME
private static final String
CLIENT_DNS
private static final String
CLIENT_NAME
private static final String
COOKIE
private static final String
COOKIES
private static final String
COOKIE_VALUE
private static final String
COOKIE_BY_NAME_PREFIX
private static final int
COOKIE_BY_NAME_PREFIX_LEN
private static final String
COOKIES_BY_NAME_PREFIX
private static final int
COOKIES_BY_NAME_PREFIX_LEN
private static final String
DATE_TIME
private static final String
HEADER_ACCEPT
private static final String
HEADER_BY_NAME_PREFIX
private static final int
HEADER_BY_NAME_PREFIX_LEN
private static final String
HEADERS_BY_NAME_PREFIX
private static final int
HEADERS_BY_NAME_PREFIX_LEN
private static final String
HEADER_AUTH
private static final String
HEADER_DATE
private static final String
HEADER_IF_MOD_SINCE
private static final String
HEADER_USER_AGENT
private static final String
HEADER_REFERER
private static final String
HTTP_METHOD
private static final String
HTTP_URI
private static final String
HTTP_VERSION
private static final String
QUERY_STR
private static final String
REFERER
private static final String
REQUEST
private static final String
RESPONSE_LENGTH
private static final String
STATUS
private static final String
TIME_TAKEN
private static final String
USER_AGENT
private static final String
VS_ID
private org.apache.catalina.Container
container
private LinkedList
patternComponents
List of access log pattern components
Constructors Summary
public DefaultAccessLogFormatterImpl(String pattern, org.apache.catalina.Container container)
Constructor.

param
pattern The access log pattern
param
container The container associated with the access log valve



                          
         

        super();

        this.patternComponents = parsePattern(pattern);
        this.container = container;

        dayFormatter = new SimpleDateFormat("dd");
        dayFormatter.setTimeZone(tz);
        monthFormatter = new SimpleDateFormat("MM");
        monthFormatter.setTimeZone(tz);
        yearFormatter = new SimpleDateFormat("yyyy");
        yearFormatter.setTimeZone(tz);
        timeFormatter = new SimpleDateFormat("HH:mm:ss");
        timeFormatter.setTimeZone(tz);
    
Methods Summary
private voidappendAttributeByName(java.nio.CharBuffer cb, java.lang.String attributeName, javax.servlet.http.HttpServletRequest hreq)

        if (attributeName == null) {
            throw new IllegalArgumentException("Null request attribute name");
        }

        cb.put(QUOTE);
        Object attrValue = hreq.getAttribute(attributeName);
        if (attrValue != null) {
            cb.put(attrValue.toString());
        } else {
            cb.put("NULL-ATTRIBUTE-" + attributeName.toUpperCase());
        }
        cb.put(QUOTE);
    
private voidappendAuthUserName(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String user = hreq.getRemoteUser();
        if (user == null) {
            user = "NULL-AUTH-USER";
        }
        cb.put(user);
        cb.put(QUOTE);
    
private voidappendClientDNS(java.nio.CharBuffer cb, javax.servlet.ServletRequest req)

        cb.put(QUOTE);
        String value = req.getRemoteAddr();
        if (value == null) {
            value = "NULL-CLIENT-DNS";
        }
        cb.put(value);
        cb.put(QUOTE);
    
private voidappendClientName(java.nio.CharBuffer cb, javax.servlet.ServletRequest req)

        cb.put(QUOTE);
        String value = req.getRemoteHost();
        if (value == null) {
            value = "NULL-CLIENT-NAME";
        }
        cb.put(value);
        cb.put(QUOTE);
    
private voidappendCookie(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String cookie = "NULL-COOKIE";
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null && cookies.length > 0) {
            cookie = cookies[0].getName() + "=" + cookies[0].getValue();
        }
        cb.put(cookie);
        cb.put(QUOTE);
    
private voidappendCookieByName(java.nio.CharBuffer cb, java.lang.String cookieName, javax.servlet.http.HttpServletRequest hreq)

        if (cookieName == null) {
            throw new IllegalArgumentException("Null request cookie name");
        }

        cb.put(QUOTE);
        String cookieValue = null;
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())) {
                    cookieValue = cookie.getValue();
                    break;
                }
            }
        }
        if (cookieValue == null) {
            cookieValue = "NULL-COOKIE-" + cookieName.toUpperCase();
        }

        cb.put(cookieValue);
        cb.put(QUOTE);
    
private voidappendCookieValue(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String cookieValue = "NULL-COOKIE-VALUE";
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null && cookies.length > 0) {
            cookieValue = cookies[0].getValue();
        }
        cb.put(cookieValue);
        cb.put(QUOTE);
    
private voidappendCookies(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (int i=0; i<cookies.length; i++) {
                cb.put(cookies[i].getName() + "=" + cookies[i].getValue());
                if (i<cookies.length-1) {
                    cb.put(";");
                }
            }
        } else {
            cb.put("NULL-COOKIES");
        }
        cb.put(QUOTE);
    
private voidappendCookiesByName(java.nio.CharBuffer cb, java.lang.String cookieName, javax.servlet.http.HttpServletRequest hreq)

        if (cookieName == null) {
            throw new IllegalArgumentException("Null request cookie name");
        }

        cb.put(QUOTE);
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null && cookies.length > 0) {
            boolean first = true;
            for (int i=0; i<cookies.length; i++) {
                if (cookieName.equals(cookies[i].getName())) {
                    if (first) {
                        first = false;
                    } else {
                        cb.put(";");
		    }
                    cb.put(cookies[i].getValue());
                }
            }
        } else {
            cb.put("NULL-COOKIES-" + cookieName.toUpperCase());
        }
        cb.put(QUOTE);
    
private voidappendCurrentDate(java.nio.CharBuffer cb)

        cb.put(QUOTE);
        Date date = getDate();
        cb.put(dayFormatter.format(date));           // Day
        cb.put('/");
        cb.put(lookup(monthFormatter.format(date))); // Month
        cb.put('/");
        cb.put(yearFormatter.format(date));          // Year
        cb.put(':");
        cb.put(timeFormatter.format(date));          // Time
        cb.put(SPACE);
        cb.put(timeZone);                            // Time Zone
        cb.put(QUOTE);
    
private voidappendHTTPMethod(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String method = hreq.getMethod();
        if (method == null) {
            method = "NULL-HTTP-METHOD";
        }
        cb.put(method);
        cb.put(QUOTE);
    
private voidappendHTTPUri(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String uri = hreq.getRequestURI();
        if (uri == null) {
            uri = "NULL-HTTP-URI";
        }
        cb.put(uri);
        cb.put(QUOTE);
    
private voidappendHTTPVersion(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String protocol = hreq.getProtocol();
        if (protocol == null) {
            protocol = "NULL-HTTP-PROTOCOL";
        }
        cb.put(protocol);
        cb.put(QUOTE);
    
private voidappendHeaderAccept(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String accept = hreq.getHeader(HTTP_HEADER_ACCEPT);
        if (accept == null) {
            accept = "NULL-HEADER-ACCEPT";
        }
        cb.put(accept);
        cb.put(QUOTE);
    
private voidappendHeaderAuth(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String auth = hreq.getHeader(HTTP_HEADER_AUTHORIZATION);
        if (auth == null) {
            auth = "NULL-HEADER-AUTHORIZATION";
        }
        cb.put(auth);
        cb.put(QUOTE);
    
private voidappendHeaderByName(java.nio.CharBuffer cb, java.lang.String headerName, javax.servlet.http.HttpServletRequest hreq)

        if (headerName == null) {
            throw new IllegalArgumentException("Null request header name");
        }

        cb.put(QUOTE);
        String value = hreq.getHeader(headerName);
        if (value == null) {
            value = "NULL-HEADER-" + headerName.toUpperCase();
        }
        cb.put(value);
        cb.put(QUOTE);
    
private voidappendHeaderDate(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String date = hreq.getHeader(HTTP_HEADER_DATE);
        if (date == null) {
            date = "NULL-HEADER-DATE";
        }
        cb.put(date);
        cb.put(QUOTE);
    
private voidappendHeaderIfModSince(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String ifModSince = hreq.getHeader(HTTP_HEADER_IF_MODIFIED_SINCE);
        if (ifModSince == null) {
            ifModSince = "NULL-HEADER-IF-MODIFIED-SINCE";
        }
        cb.put(ifModSince);
        cb.put(QUOTE);
    
private voidappendHeadersByName(java.nio.CharBuffer cb, java.lang.String headerName, javax.servlet.http.HttpServletRequest hreq)

        if (headerName == null) {
            throw new IllegalArgumentException("Null request header name");
        }

        cb.put(QUOTE);
        Enumeration e = hreq.getHeaders(headerName);
        if (e != null) {
            boolean first = true;
            while (e.hasMoreElements()) {
                if (first) {
                    first = false;
                } else {
                    cb.put(";");
                }
                cb.put(e.nextElement().toString());
            }
            if (first) {
                cb.put("NULL-HEADERS-" + headerName.toUpperCase());
            }
        } else {
            cb.put("NULL-HEADERS-" + headerName.toUpperCase());
        }
        cb.put(QUOTE);
    
public voidappendLogEntry(org.apache.catalina.Request request, org.apache.catalina.Response response, java.nio.CharBuffer charBuffer)
Appends an access log entry line, with info obtained from the given request and response objects, to the given CharBuffer.

param
request The request object from which to obtain access log info
param
response The response object from which to obtain access log info
param
charBuffer The CharBuffer to which to append access log info


        ServletRequest req = request.getRequest();
        HttpServletRequest hreq = (HttpServletRequest) req;

        for (int i=0; i<patternComponents.size(); i++) {
            String pc = patternComponents.get(i);
            if (pc.startsWith(ATTRIBUTE_BY_NAME_PREFIX)) {
                appendAttributeByName(charBuffer,
                                      pc.substring(ATTRIBUTE_BY_NAME_PREFIX_LEN),
                                      hreq);
            } else if (AUTH_USER_NAME.equals(pc)) {
                appendAuthUserName(charBuffer, hreq);
            } else if (CLIENT_DNS.equals(pc)) {
                appendClientDNS(charBuffer, req);
            } else if (CLIENT_NAME.equals(pc)) {
                appendClientName(charBuffer, req);
            } else if (COOKIE.equals(pc)) {
                appendCookie(charBuffer, hreq);
            } else if (COOKIES.equals(pc)) {
                appendCookies(charBuffer, hreq);
            } else if (COOKIE_VALUE.equals(pc)) {
                appendCookieValue(charBuffer, hreq);
            } else if (pc.startsWith(COOKIE_BY_NAME_PREFIX)) {
                appendCookieByName(charBuffer,
                                   pc.substring(COOKIE_BY_NAME_PREFIX_LEN),
                                   hreq);
            } else if (pc.startsWith(COOKIES_BY_NAME_PREFIX)) {
                appendCookiesByName(charBuffer,
                                   pc.substring(COOKIES_BY_NAME_PREFIX_LEN),
                                   hreq);
            } else if (DATE_TIME.equals(pc)) {
                appendCurrentDate(charBuffer);       
            } else if (HEADER_ACCEPT.equals(pc)) {
                appendHeaderAccept(charBuffer, hreq);
            } else if (HEADER_AUTH.equals(pc)) {
                appendHeaderAuth(charBuffer, hreq);
            } else if (HEADER_DATE.equals(pc)) {
                appendHeaderDate(charBuffer, hreq);
            } else if (HEADER_IF_MOD_SINCE.equals(pc)) {
                appendHeaderIfModSince(charBuffer, hreq);
            } else if (HEADER_USER_AGENT.equals(pc)) {
                appendUserAgent(charBuffer, hreq);
            } else if (HEADER_REFERER.equals(pc)) {
                appendReferer(charBuffer, hreq);
            } else if (HTTP_METHOD.equals(pc)) {
                appendHTTPMethod(charBuffer, hreq);
            } else if (HTTP_URI.equals(pc)) {
                appendHTTPUri(charBuffer, hreq);
            } else if (HTTP_VERSION.equals(pc)) {
                appendHTTPVersion(charBuffer, hreq);
            } else if (QUERY_STR.equals(pc)) {
                appendQueryString(charBuffer, hreq);
            } else if (REFERER.equals(pc)) {
                appendReferer(charBuffer, hreq);
            } else if (REQUEST.equals(pc)) {
                appendRequestInfo(charBuffer, hreq);
            } else if (RESPONSE_LENGTH.equals(pc)) {
                appendResponseLength(charBuffer, response);
            } else if (STATUS.equals(pc)) {
                appendResponseStatus(charBuffer, response);
            } else if (TIME_TAKEN.equals(pc)) {
                appendTimeTaken(charBuffer, request);
            } else if (USER_AGENT.equals(pc)) {
                appendUserAgent(charBuffer, hreq);
            } else if (VS_ID.equals(pc)) {
                appendVirtualServerId(charBuffer);
            } else if (pc.startsWith(HEADER_BY_NAME_PREFIX)) {
                appendHeaderByName(charBuffer,
                                   pc.substring(HEADER_BY_NAME_PREFIX_LEN),
                                   hreq);
            } else if (pc.startsWith(HEADERS_BY_NAME_PREFIX)) {
                appendHeadersByName(charBuffer,
                                    pc.substring(HEADERS_BY_NAME_PREFIX_LEN),
                                    hreq);
            }

            charBuffer.put(SPACE);
        }
    
private voidappendQueryString(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String query = hreq.getQueryString();
        if (query == null) {
            query = "NULL-QUERY";
        }
        cb.put(query);
        cb.put(QUOTE);
    
private voidappendReferer(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String referer = hreq.getHeader("referer");
        if (referer == null) {
            referer = "NULL-REFERER";
        }
        cb.put(referer);
        cb.put(QUOTE);
    
private voidappendRequestInfo(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        cb.put(hreq.getMethod());
        cb.put(SPACE);
        cb.put(hreq.getRequestURI());
        if (hreq.getQueryString() != null) {
            cb.put('?");
            cb.put(hreq.getQueryString());
        }
        cb.put(SPACE);
        cb.put(hreq.getProtocol());
        cb.put(QUOTE);
    
private voidappendResponseLength(java.nio.CharBuffer cb, org.apache.catalina.Response response)

        cb.put("" + response.getContentCount());
    
private voidappendResponseStatus(java.nio.CharBuffer cb, org.apache.catalina.Response response)

        cb.put(String.valueOf(((HttpResponse) response).getStatus()));
    
private voidappendTimeTaken(java.nio.CharBuffer cb, org.apache.catalina.Request req)


        String timeTaken = "NULL-TIME-TAKEN";

        cb.put(QUOTE);
        Long startTimeObj = (Long) req.getNote(
            Constants.REQUEST_START_TIME_NOTE);
        if (startTimeObj != null) {
            long startTime = startTimeObj.longValue();
            long endTime = System.currentTimeMillis();
            timeTaken = String.valueOf(endTime - startTime);
        } 
        cb.put(timeTaken);
        cb.put(QUOTE);
    
private voidappendUserAgent(java.nio.CharBuffer cb, javax.servlet.http.HttpServletRequest hreq)

        cb.put(QUOTE);
        String ua = hreq.getHeader("user-agent");
        if (ua == null) {
            ua = "NULL-USER-AGENT";
        }
        cb.put(ua);
        cb.put(QUOTE);
    
private voidappendVirtualServerId(java.nio.CharBuffer cb)

        String vsId = "NULL-VIRTUAL-SERVER";
        if (container != null) {
            vsId = container.getName();
        }
        cb.put(vsId);
    
private java.util.LinkedListparsePattern(java.lang.String pattern)


        LinkedList list = new LinkedList();

        int from = 0;
        int end = -1;
        int index = -1;

        if (pattern == null || pattern.indexOf('%") < 0) {
            _logger.log(Level.SEVERE,
                        "peaccesslogvalve.invalidAccessLogPattern",
                        pattern);
        }

        while ((index = pattern.indexOf('%", from)) >= 0) {
            end = pattern.indexOf('%", index+1);
            if (end < 0) {
                _logger.log(
                    Level.SEVERE,
                    "peaccesslogvalve.missingAccessLogPatternEndDelimiter",
                    pattern);
            }
            String component = pattern.substring(index+1, end);

            if (!component.startsWith(ATTRIBUTE_BY_NAME_PREFIX)
                    && !AUTH_USER_NAME.equals(component) 
                    && !CLIENT_DNS.equals(component) 
                    && !CLIENT_NAME.equals(component) 
                    && !COOKIE.equals(component) 
                    && !COOKIES.equals(component) 
                    && !COOKIE_VALUE.equals(component)
                    && !component.startsWith(COOKIE_BY_NAME_PREFIX)
                    && !component.startsWith(COOKIES_BY_NAME_PREFIX)
                    && !DATE_TIME.equals(component) 
                    && !HEADER_ACCEPT.equals(component) 
                    && !HEADER_AUTH.equals(component) 
                    && !HEADER_DATE.equals(component) 
                    && !HEADER_IF_MOD_SINCE.equals(component) 
                    && !HEADER_USER_AGENT.equals(component) 
                    && !HEADER_REFERER.equals(component) 
                    && !HTTP_METHOD.equals(component) 
                    && !HTTP_URI.equals(component) 
                    && !HTTP_VERSION.equals(component) 
                    && !QUERY_STR.equals(component) 
                    && !REFERER.equals(component) 
                    && !REQUEST.equals(component) 
                    && !RESPONSE_LENGTH.equals(component) 
                    && !STATUS.equals(component) 
                    && !TIME_TAKEN.equals(component) 
                    && !USER_AGENT.equals(component) 
                    && !VS_ID.equals(component)
                    && !component.startsWith(HEADER_BY_NAME_PREFIX)
                    && !component.startsWith(HEADERS_BY_NAME_PREFIX)) {
                _logger.log(
                    Level.SEVERE,
                    "peaccesslogvalve.invalidAccessLogPatternComponent",
                    new Object[] { component, pattern });
            }

            if (TIME_TAKEN.equals(component)) {
                needTimeTaken = true;
            }

            list.add(component);
            from = end + 1;    
        }

        return list;