package org.wyona.yanel.servlet;

/**
 * First attempt to "standardize" access logging for resources
 */
public class AccessLog {

    /**
     * log4j category
     */
    public final static String CATEGORY = "Access";

    /**
     * Get log message
     *
     * @param requestURL Request URL
     * @param realm ID Realm ID
     * @param cookieValue Value/UUID of unique persistent cookie
     * @param referer Referer
     * @param userAgent User agent, e.g. Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.0.19) Gecko/2010031218 Firefox/3.0.19
     */
    public static String getLogMessage(String requestURL, String realmID, String cookieValue, String referer, String userAgent) {
        return requestURL + " r:" + realmID + " c:" + cookieValue + "ref:" + referer + " ua:" + userAgent;
    }
}

@@ -31,103 +31,427 @@
 t;%0A%0A
-/**%0A * First attempt to %22standardize%22 access logging for resources%0A */%0Apublic class AccessLog %7B
+import javax.servlet.http.Cookie;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport java.util.Date;%0A%0Aimport org.apache.log4j.Logger;%0A%0A/**%0A * First attempt to %22standardize%22 access logging for resources%0A */%0Apublic class AccessLog %7B%0A%0A    private static Logger log = Logger.getLogger(AccessLog.class);%0A%0A    private static String ANALYTICS_COOKIE_NAME = %22_yanel-analytics%22;
 %0A%0A  
@@ -1132,11 +1132,2363 @@
 t;%0A    %7D
+%0A%0A    /**%0A     * Get log message%0A     */%0A    public static String getLogMessage(HttpServletRequest request, HttpServletResponse response, String realmID) %7B%0A        Cookie cookie = getYanelAnalyticsCookie(request, response);%0A        return getLogMessage(request.getRequestURL().toString(), realmID, cookie.getValue(), request.getHeader(%22referer%22), request.getHeader(%22User-Agent%22));%0A    %7D%0A%0A    /**%0A     * Get log message%0A     */%0A    public static String getLogMessage(HttpServletRequest request, String realmID) %7B%0A        Cookie cookie = getYanelAnalyticsCookie(request);%0A        return getLogMessage(request.getRequestURL().toString(), realmID, cookie.getValue(), request.getHeader(%22referer%22), request.getHeader(%22User-Agent%22));%0A    %7D%0A%0A    /**%0A     * Get Yanel analytics cookie%0A     * @param request Client request%0A     */%0A    public static Cookie getYanelAnalyticsCookie(HttpServletRequest request) %7B%0A        Cookie%5B%5D cookies = request.getCookies();%0A        if (cookies != null) %7B%0A            for (int i = 0; i %3C cookies.length; i++) %7B%0A                if (cookies%5Bi%5D.getName().equals(ANALYTICS_COOKIE_NAME)) %7B // TODO: This code is not sufficient to make sure that only one cookie is being set, because Tomcat processes the requests in parallel and until the first cookie is registered, some more cookies might already be set!%0A                    //log.debug(%22Has already a Yanel analytics cookie: %22 + cookies%5Bi%5D.getValue());%0A                    return cookies%5Bi%5D;%0A                %7D%0A            %7D%0A        %7D%0A        log.warn(%22No Yanel analytics cookie '%22 + ANALYTICS_COOKIE_NAME + %22' set yet!%22);%0A        return null;%0A    %7D%0A%0A    /**%0A     * Get Yanel analytics cookie, whereas set cookie if it does not exist yet as persistent cookie%0A     * @param request Client request%0A     */%0A    public static Cookie getYanelAnalyticsCookie(HttpServletRequest request, HttpServletResponse response) %7B%0A        Cookie c = getYanelAnalyticsCookie(request);%0A        if (c != null) return c;%0A%0A        Cookie analyticsCookie = new Cookie(ANALYTICS_COOKIE_NAME, %22YA-%22 + new Date().getTime()); // TODO: getTime() is not unique!%0A        analyticsCookie.setMaxAge(31536000); // 1 year%0A        //analyticsCookie.setMaxAge(86400); // 1 day%0A        analyticsCookie.setPath(request.getContextPath());%0A        response.addCookie(analyticsCookie);%0A        return analyticsCookie;%0A    %7D
 %0A%7D%0A

set and get cookie moved