You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2012/02/02 12:53:34 UTC
svn commit: r1239560 -
/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/
Author: fmeschbe
Date: Thu Feb 2 11:53:33 2012
New Revision: 1239560
URL: http://svn.apache.org/viewvc?rev=1239560&view=rev
Log:
SLING-2389 Formating and clean up
Modified:
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/FileRequestLog.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogServiceFacade.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogger.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerRequest.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerService.java
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/CustomLogFormat.java Thu Feb 2 11:53:33 2012
@@ -32,23 +32,32 @@ import java.util.Locale;
import javax.servlet.http.Cookie;
import org.apache.sling.engine.impl.request.RequestData;
-import org.osgi.service.http.HttpContext;
/**
* The <code>CustomLogFormat</code> class implements the support for log format
* strings similar to the Apache httpd CustomLog configuration.
+ *
+ * @see <a
+ * href="http://sling.apache.org/site/client-request-logging.html">Client
+ * Request Logging</a> for documentation of supported formats.
*/
class CustomLogFormat {
+ /*
+ * NOTE: Documentation at
+ * https://cwiki.apache.org/confluence/display/SLINGxSITE
+ * /Client+Request+Logging should be kept in sync with this class !
+ */
+
/**
- * The parsed list of log format parts whose <code>print</code> method
- * is called when building the log message line.
+ * The parsed list of log format parts whose <code>print</code> method is
+ * called when building the log message line.
*/
Parameter[] logParameters;
/**
* Creates a new instance from of this class parsing the log format pattern.
- *
+ *
* @param pattern The pattern to be parsed.
*/
CustomLogFormat(String pattern) {
@@ -60,14 +69,13 @@ class CustomLogFormat {
/**
* Creates a log message from the given <code>request</code> and
- * <code>response</code> objects according to the log format from which
- * this instance has been created.
- *
- * @param request The {@link org.apache.sling.component.ComponentResponse}
- * used to extract values for the log message.
- * @param response The {@link SlingHttpServletResponseImpl} used to extract values for the
- * log message.
- *
+ * <code>response</code> objects according to the log format from which this
+ * instance has been created.
+ *
+ * @param request The {@link RequestLoggerRequest} used to extract values
+ * for the log message.
+ * @param response The {@link RequestLoggerResponse} used to extract values
+ * for the log message.
* @return The formatted log message or <code>null</code> if this log
* formatter has not been initialized with a valid log format
* pattern.
@@ -75,7 +83,7 @@ class CustomLogFormat {
String format(RequestLoggerRequest request, RequestLoggerResponse response) {
if (this.logParameters != null) {
StringBuilder buf = new StringBuilder();
- for (int i=0; i < this.logParameters.length; i++) {
+ for (int i = 0; i < this.logParameters.length; i++) {
this.logParameters[i].print(buf, request, response);
}
return buf.toString();
@@ -88,27 +96,27 @@ class CustomLogFormat {
* Returns a string representation of this log format instance. The returned
* String is actually rebuilt from the parsed format string and may be used
* to create another instance of this class with the same format string.
- *
+ *
* @return String representation of this instance.
*/
public String toString() {
- StringBuffer buf = new StringBuffer();
- for (int i=0; this.logParameters != null && i < this.logParameters.length; i++) {
+ StringBuilder buf = new StringBuilder();
+ for (int i = 0; this.logParameters != null && i < this.logParameters.length; i++) {
buf.append(this.logParameters[i]);
}
return buf.toString();
}
- //---------- Parsing the format pattern -----------------------------------
+ // ---------- Parsing the format pattern -----------------------------------
private Parameter[] parse(String pattern) {
List<Parameter> parameterList = new ArrayList<Parameter>();
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
CharacterIterator sr = new StringCharacterIterator(pattern);
- for (int c=sr.first(); c != CharacterIterator.DONE; c=sr.next()) {
+ for (int c = sr.first(); c != CharacterIterator.DONE; c = sr.next()) {
if (c == '%') {
int c1 = sr.next();
if (c1 != '%') {
@@ -161,7 +169,7 @@ class CustomLogFormat {
// read name
String name;
if (c == '{') {
- StringBuffer nameBuf = new StringBuffer();
+ StringBuilder nameBuf = new StringBuilder();
for (c = sr.next(); c != CharacterIterator.DONE && c != '}'; c = sr.next()) {
nameBuf.append((char) c);
}
@@ -185,7 +193,7 @@ class CustomLogFormat {
case 'b':
case 'B':
- param = new ByteCountParameter((char) c);
+ param = new ByteCountParameter();
break;
case 'C':
@@ -298,11 +306,11 @@ class CustomLogFormat {
}
private int[] parseStatusCodes(CharacterIterator sr, int c) {
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
buf.append((char) c);
List<Integer> numbers = new ArrayList<Integer>();
- for (c=sr.next(); c != CharacterIterator.DONE; c=sr.next()) {
+ for (c = sr.next(); c != CharacterIterator.DONE; c = sr.next()) {
if (c == ',') {
int num = 0;
try {
@@ -341,13 +349,13 @@ class CustomLogFormat {
}
int[] statusCodes = new int[numbers.size()];
- for (int i=0; i < numbers.size(); i++) {
+ for (int i = 0; i < numbers.size(); i++) {
statusCodes[i] = (numbers.get(i)).intValue();
}
return statusCodes;
}
- //---------- Parameter support --------------------------------------------
+ // ---------- Parameter support --------------------------------------------
static interface Parameter {
void print(StringBuilder dest, RequestLoggerRequest request, RequestLoggerResponse response);
@@ -355,13 +363,15 @@ class CustomLogFormat {
static class PlainTextParameter implements Parameter {
private String value;
+
PlainTextParameter(String value) {
this.value = value;
}
- public void print(StringBuilder dest, RequestLoggerRequest request,
- RequestLoggerResponse response) {
+
+ public void print(StringBuilder dest, RequestLoggerRequest request, RequestLoggerResponse response) {
dest.append(this.value);
}
+
public String toString() {
return this.value;
}
@@ -369,9 +379,13 @@ class CustomLogFormat {
abstract static class BaseParameter implements Parameter {
private int[] statusLimits;
+
private boolean required;
+
private char parName;
+
private final String parParam;
+
private final boolean isRequest;
protected BaseParameter(String parParam, boolean isRequest) {
@@ -392,6 +406,7 @@ class CustomLogFormat {
}
protected abstract String getValue(RequestLoggerRequest request);
+
protected abstract String getValue(RequestLoggerResponse response);
public final void print(StringBuilder dest, RequestLoggerRequest request, RequestLoggerResponse response) {
@@ -406,7 +421,7 @@ class CustomLogFormat {
return true;
}
- for (int i=0; i < this.statusLimits.length; i++) {
+ for (int i = 0; i < this.statusLimits.length; i++) {
if (status == this.statusLimits[i]) {
return this.required;
}
@@ -424,14 +439,14 @@ class CustomLogFormat {
}
public String toString() {
- StringBuffer result = new StringBuffer("%");
+ StringBuilder result = new StringBuilder("%");
if (this.statusLimits != null) {
if (!this.required) {
result.append('!');
}
- for (int i=0; i < this.statusLimits.length; i++) {
+ for (int i = 0; i < this.statusLimits.length; i++) {
if (i > 0) {
result.append(',');
}
@@ -446,7 +461,8 @@ class CustomLogFormat {
return result.toString();
}
- //--------- helper ----------------------------------------------------
+
+ // --------- helper ----------------------------------------------------
private static boolean isPrint(char c) {
return c >= 0x20 && c < 0x7f && c != '\\' && c != '"';
@@ -459,7 +475,7 @@ class CustomLogFormat {
}
// find the first non-printable
- int i=0;
+ int i = 0;
while (i < value.length() && isPrint(value.charAt(i))) {
i++;
}
@@ -471,7 +487,7 @@ class CustomLogFormat {
// otherwise copy the printable first part in a string buffer
// and start encoding
- StringBuffer buf = new StringBuffer(value.substring(0, i));
+ StringBuilder buf = new StringBuilder(value.substring(0, i));
while (i < value.length()) {
char c = value.charAt(i);
if (isPrint(c)) {
@@ -486,17 +502,17 @@ class CustomLogFormat {
buf.append("\\f");
} else if (c == '\b') { // BSP
buf.append("\\b");
- } else if (c == '"') { // "
+ } else if (c == '"') { // "
buf.append("\\\"");
} else if (c == '\\') { // \
buf.append("\\\\");
- } else { // encode
+ } else { // encode
buf.append("\\u");
if (c < 0x10) {
- buf.append('0'); // leading zero
+ buf.append('0'); // leading zero
}
if (c < 0x100) {
- buf.append('0'); // leading zero
+ buf.append('0'); // leading zero
}
if (c < 0x1000) {
buf.append('0'); // leading zero
@@ -570,7 +586,7 @@ class CustomLogFormat {
}
static class ByteCountParameter extends BaseParameter {
- public ByteCountParameter(char c) {
+ public ByteCountParameter() {
super(null, false);
}
@@ -591,8 +607,7 @@ class CustomLogFormat {
static class TimeParameter extends BaseParameter {
/** date format - see access logging in service() */
- private static final SimpleDateFormat accessLogFmt = new SimpleDateFormat(
- "dd/MMM/yyyy:HH:mm:ss ", Locale.US);
+ private static final SimpleDateFormat accessLogFmt = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss ", Locale.US);
/** time format for GMT offset - see access logging in service() */
private static final DecimalFormat dfmt = new DecimalFormat("+0000;-0000");
@@ -615,7 +630,7 @@ class CustomLogFormat {
public TimeParameter(String parParam) {
super(parParam, false);
- this.requestStart = parParam== null || !parParam.equals("end");
+ this.requestStart = parParam == null || !parParam.equals("end");
}
protected String getValue(RequestLoggerRequest request) {
@@ -627,18 +642,18 @@ class CustomLogFormat {
return timeFormatted(time);
}
- // ---------- internal -----------------------------------------------------
+ // ---------- internal
+ // -----------------------------------------------------
static String timeFormatted(long time) {
if (time / 1000 != lastTimeFormattedSeconds) {
lastTimeFormattedSeconds = time / 1000;
Date date = new Date(time);
- StringBuffer buf = new StringBuffer(accessLogFmt.format(date));
+ StringBuilder buf = new StringBuilder(accessLogFmt.format(date));
if (time / 3600000 != lastZoneOffsetHour) {
lastZoneOffsetHour = time / 3600000;
calendar.setTime(date);
- int tzOffset = calendar.get(Calendar.ZONE_OFFSET)
- + calendar.get(Calendar.DST_OFFSET);
+ int tzOffset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
tzOffset /= (60 * 1000);
tzOffset = ((tzOffset / 60) * 100) + (tzOffset % 60);
@@ -653,6 +668,7 @@ class CustomLogFormat {
static class DurationParameter extends BaseParameter {
private final boolean seconds;
+
public DurationParameter(boolean seconds) {
super(null, false);
this.seconds = seconds;
@@ -768,8 +784,7 @@ class CustomLogFormat {
String query = request.getQueryString();
query = (query == null || query.length() == 0) ? "" : "?" + query;
- return request.getMethod() + " " + request.getRequestURI() + query
- + " " + request.getProtocol();
+ return request.getMethod() + " " + request.getRequestURI() + query + " " + request.getProtocol();
}
protected String getValue(RequestLoggerResponse response) {
@@ -840,12 +855,8 @@ class CustomLogFormat {
}
protected String getValue(RequestLoggerRequest request) {
- final Object user = request.getAttribute(HttpContext.REMOTE_USER);
- if (user instanceof String) {
- return (String) user;
- }
-
- return request.getRemoteUser();
+ final String user = request.getRemoteUser();
+ return (user == null) ? null : escape(user);
}
protected String getValue(RequestLoggerResponse response) {
@@ -859,12 +870,8 @@ class CustomLogFormat {
}
protected String getValue(RequestLoggerRequest request) {
- final Object authType = request.getAttribute(HttpContext.AUTHENTICATION_TYPE);
- if (authType instanceof String) {
- return (String) authType;
- }
-
- return request.getAuthType();
+ final String authType = request.getAuthType();
+ return (authType == null) ? null : escape(authType);
}
protected String getValue(RequestLoggerResponse response) {
@@ -888,22 +895,15 @@ class CustomLogFormat {
static class CookieParameter extends BaseParameter {
private String cookieName;
+
CookieParameter(String cookieName, boolean isRequest) {
super(cookieName, isRequest);
this.cookieName = cookieName;
}
protected String getValue(RequestLoggerRequest request) {
- final Cookie[] cookies = request.getCookies();
- if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- if (cookies[i].getName().equals(cookieName)) {
- return escape(cookies[i].toString());
- }
- }
- }
-
- return null;
+ Cookie cookie = request.getCookie(this.cookieName);
+ return (cookie == null) ? null : escape(cookie.toString());
}
protected String getValue(RequestLoggerResponse response) {
@@ -915,6 +915,7 @@ class CustomLogFormat {
static class HeaderParameter extends BaseParameter {
private String headerName;
+
HeaderParameter(String headerName, boolean isRequest) {
super(headerName, isRequest);
this.headerName = headerName;
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/FileRequestLog.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/FileRequestLog.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/FileRequestLog.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/FileRequestLog.java Thu Feb 2 11:53:33 2012
@@ -35,15 +35,9 @@ import org.apache.sling.engine.RequestLo
* internal map of log files is kept and access to the <code>PrintWriter</code>s
* to write to the file is synchronized.
* <p>
- * This class has a defined lifecycle to ensure correct operation: Before using
- * the class to instantiate it, the {@link #init(String)} method should be
- * called mainly to set the root directory for relative log file paths. It is
- * not recommended to call the {@link #init(String)} method multiple times. When
- * the class is not used any more the {@link #dispose()} method should be called
- * to clean up, namely to close all open files. This lifecycle behaviour is
- * forced by the {@link RequestLogger} component, which calls the
- * {@link #init(String)} method on filter component activation and calls the
- * {@link #dispose()} metod on filter component deactivation.
+ * This class has a defined lifecycle to ensure correct operation: To ensure no
+ * log files are kept open, the {@link RequestLoggerFilter} object calls
+ * {@link #dispose()} when it is being deactivated.
* <p>
* Note: Currently, each log file is kept open from the moment the log file is
* first moment until the {@link #dispose()} method is called. Future
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogServiceFacade.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogServiceFacade.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogServiceFacade.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogServiceFacade.java Thu Feb 2 11:53:33 2012
@@ -57,17 +57,16 @@ class RequestLogServiceFacade implements
/**
* Creates an instance of this facade class calling request log services
* with the given <code>serviceName</code>.
- *
- * @param context The <code>BundleContext</code> used to acquire the
- * request log services.
+ *
+ * @param context The <code>BundleContext</code> used to acquire the request
+ * log services.
* @param serviceName The name of the services used for logging. This value
* is used to check the {@link RequestLog#REQUEST_LOG_NAME}
* service property for service selection.
*/
public RequestLogServiceFacade(BundleContext context, String serviceName) {
- String filter = "(&(" + Constants.OBJECTCLASS + "="
- + RequestLog.class.getName() + ")(" + RequestLog.REQUEST_LOG_NAME
- + "=" + serviceName + "))";
+ String filter = "(&(" + Constants.OBJECTCLASS + "=" + RequestLog.class.getName() + ")("
+ + RequestLog.REQUEST_LOG_NAME + "=" + serviceName + "))";
this.requestLogTracker = new ServiceTracker(context, filter, null);
this.requestLogTracker.open();
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogger.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogger.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogger.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLogger.java Thu Feb 2 11:53:33 2012
@@ -23,7 +23,9 @@ import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
+import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyOption;
@@ -31,44 +33,38 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
/**
- * The <code>RequestLogger</code> is a request level filter, which
- * provides customizable logging or requests handled by Sling. This filter is
- * inserted as the first filter in the request level filter chain and therefore
- * is the first filter called when processing a request and the last filter
- * acting just before the request handling terminates.
- *
+ * The <code>RequestLogger</code> just registers {@link RequestLoggerService}
+ * instance on behalf of the provided configuration.
*/
-@Component(metatype=true,label="%request.log.name",description="%request.log.description")
+@Component(metatype = true, label = "%request.log.name", description = "%request.log.description")
@Properties({
- @Property(name="service.description",value="Request Logger"),
- @Property(name="service.vendor",value="The Apache Software Foundation")
+ @Property(name = "service.description", value = "Request Logger"),
+ @Property(name = "service.vendor", value = "The Apache Software Foundation")
})
public class RequestLogger {
- @Property(value="logs/request.log")
+ @Property(value = "logs/request.log")
public static final String PROP_REQUEST_LOG_OUTPUT = "request.log.output";
- @Property(intValue=0,options={
- @PropertyOption(name = "0", value = "Logger Name"),
- @PropertyOption(name = "1", value = "File Name"),
- @PropertyOption(name = "2", value = "RequestLog Service")
+ @Property(intValue = 0, options = {
+ @PropertyOption(name = "0", value = "Logger Name"), @PropertyOption(name = "1", value = "File Name"),
+ @PropertyOption(name = "2", value = "RequestLog Service")
})
public static final String PROP_REQUEST_LOG_OUTPUT_TYPE = "request.log.outputtype";
- @Property(boolValue=true)
+ @Property(boolValue = true)
public static final String PROP_REQUEST_LOG_ENABLED = "request.log.enabled";
- @Property(value="logs/access.log")
+ @Property(value = "logs/access.log")
public static final String PROP_ACCESS_LOG_OUTPUT = "access.log.output";
- @Property(intValue=0,options={
- @PropertyOption(name = "0", value = "Logger Name"),
- @PropertyOption(name = "1", value = "File Name"),
- @PropertyOption(name = "2", value = "RequestLog Service")
+ @Property(intValue = 0, options = {
+ @PropertyOption(name = "0", value = "Logger Name"), @PropertyOption(name = "1", value = "File Name"),
+ @PropertyOption(name = "2", value = "RequestLog Service")
})
public static final String PROP_ACCESS_LOG_OUTPUT_TYPE = "access.log.outputtype";
- @Property(boolValue=true)
+ @Property(boolValue = true)
public static final String PROP_ACCESS_LOG_ENABLED = "access.log.enabled";
/**
@@ -91,27 +87,6 @@ public class RequestLogger {
private static final String ACCESS_LOG_FORMAT = "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"";
/**
- * A special request logger service, which writes the request log message at
- * request start time. This logger logs a message with the format
- * {@link #REQUEST_LOG_ENTRY_FORMAT}.
- */
- private RequestLoggerService requestLogEntry;
-
- /**
- * A special request logger service, which writes the request log message at
- * request termination time. This logger logs a message with the format
- * {@link #REQUEST_LOG_EXIT_FORMAT}.
- */
- private RequestLoggerService requestLogExit;
-
- /**
- * A special request logger service, which writes the access log message at
- * request termination time. This logger logs a message with the format
- * {@link #ACCESS_LOG_FORMAT NCSA extended/combined log format}.
- */
- private RequestLoggerService accessLog;
-
- /**
* RequestLoggerService instances created on behalf of the static
* configuration.
*/
@@ -119,16 +94,7 @@ public class RequestLogger {
// ---------- SCR Integration ----------------------------------------------
- /**
- * Activates this component by setting up the special request entry and exit
- * request loggers and the access logger as configured in the context
- * properties. In addition the <code>FileRequestLog</code> class is
- * initialized with the value of the <code>sling.home</code> context
- * property to resolve relative log file names.
- *
- * @param osgiContext The OSGi Component Context providing the configuration
- * data and access into the system.
- */
+ @Activate
protected void activate(BundleContext bundleContext, Map<String, Object> props) {
// prepare the request loggers if a name is configured and the
@@ -154,15 +120,7 @@ public class RequestLogger {
}
}
- /**
- * Deactivates this component by unbinding and shutting down all loggers
- * setup during activation and finally dispose off the
- * <code>FileRequestLog</code> class to make sure all shared writers are
- * closed.
- *
- * @param osgiContext The OSGi Component Context providing the configuration
- * data and access into the system.
- */
+ @Deactivate
protected void deactivate() {
for (Entry<ServiceRegistration, RequestLoggerService> entry : services.entrySet()) {
entry.getKey().unregister();
@@ -171,28 +129,6 @@ public class RequestLogger {
services.clear();
}
- /**
- * Create a {@link RequestLoggerService} instance from the given
- * configuration data. This method creates a <code>Properties</code>
- * object from the data which may be handled by the
- * {@link RequestLoggerService#RequestLoggerService(BundleContext, Dictionary)}
- * constructor to set itself up.
- *
- * @param bundleContext The <code>BundleContext</code> used to setup a
- * <code>ServiceTracker</code> should the output be a
- * <code>RequestLog</code> service.
- * @param onEntry Whether the logger is to be called on request entry (true)
- * or not (false).
- * @param format The log format string. This is expected to be a String.
- * @param output The name of the output, which may be an SLF4J logger, a
- * relative or absolute file name or the name of a
- * <code>RequestLog</code> service. This is expected to be a
- * String.
- * @param outputType The type of output, 0 for SLF4J logger, 1 for file name
- * and 2 for a service name. This is expected to be an Integer.
- * @return The functional and prepared <code>RequestLoggerService</code>
- * instance.
- */
private static void createRequestLoggerService(Map<ServiceRegistration, RequestLoggerService> services,
BundleContext bundleContext, boolean onEntry, Object format, Object output, Object outputType) {
final Hashtable<String, Object> config = new Hashtable<String, Object>();
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java Thu Feb 2 11:53:33 2012
@@ -36,15 +36,8 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
+import org.slf4j.LoggerFactory;
-/**
- * The <code>RequestLogger</code> is a request level filter, which
- * provides customizable logging or requests handled by Sling. This filter is
- * inserted as the first filter in the request level filter chain and therefore
- * is the first filter called when processing a request and the last filter
- * acting just before the request handling terminates.
- *
- */
@Component(immediate = true, policy = ConfigurationPolicy.IGNORE)
@Properties({
@Property(name = "service.description", value = "Request Logger Filter"),
@@ -61,18 +54,8 @@ public final class RequestLoggerFilter i
private static final RequestLoggerService[] NONE = new RequestLoggerService[0];
- /**
- * The list of {@link RequestLoggerService} called when the request enters
- * processing. The order of the services in this list determined by the
- * registration order.
- */
private RequestLoggerService[] requestEntry = NONE;
- /**
- * The list of {@link RequestLoggerService} called when the request is about
- * to exit processing. The order of the services in this list determined by
- * the registration order.
- */
private RequestLoggerService[] requestExit = NONE;
public void init(FilterConfig filterConfig) {
@@ -94,20 +77,13 @@ public final class RequestLoggerFilter i
}
public void destroy() {
+ FileRequestLog.dispose();
}
// ---------- SCR Integration ----------------------------------------------
- /**
- * Binds a <code>RequestLoggerService</code> to be used during request
- * filter.
- *
- * @param requestLoggerService The <code>RequestLoggerService</code> to
- * use.
- */
@SuppressWarnings("unused")
- private void bindRequestLoggerService(
- RequestLoggerService requestLoggerService) {
+ private void bindRequestLoggerService(RequestLoggerService requestLoggerService) {
if (requestLoggerService.isOnEntry()) {
this.requestEntry = this.addService(this.requestEntry, requestLoggerService);
} else {
@@ -115,16 +91,8 @@ public final class RequestLoggerFilter i
}
}
- /**
- * Binds a <code>RequestLoggerService</code> to be used during request
- * filter.
- *
- * @param requestLoggerService The <code>RequestLoggerService</code> to
- * use.
- */
@SuppressWarnings("unused")
- private void unbindRequestLoggerService(
- RequestLoggerService requestLoggerService) {
+ private void unbindRequestLoggerService(RequestLoggerService requestLoggerService) {
if (requestLoggerService.isOnEntry()) {
this.requestEntry = this.removeService(this.requestEntry, requestLoggerService);
} else {
@@ -132,21 +100,11 @@ public final class RequestLoggerFilter i
}
}
- /**
- * Creates a new list of request logger services from the existing list
- * appending the new logger. This method does not check, whether the logger
- * has already been added or not and so may add the the logger multiple
- * times. It is the responsibility of the caller to make sure to not add
- * services multiple times.
- *
- * @param list The list to add the new service to
- * @param requestLoggerService The service to append to the list
- * @param A new list with the added service at the end.
- */
- private RequestLoggerService[] addService(RequestLoggerService[] list,
- RequestLoggerService requestLoggerService) {
- if (list == NONE) {
- return new RequestLoggerService[] { requestLoggerService };
+ private RequestLoggerService[] addService(RequestLoggerService[] list, RequestLoggerService requestLoggerService) {
+ if (list.length == 0) {
+ return new RequestLoggerService[] {
+ requestLoggerService
+ };
}
// add the service to the list, must not be in the list yet due to
@@ -158,21 +116,7 @@ public final class RequestLoggerFilter i
return newList;
}
- /**
- * Creates a new list of request logger services from the existing list by
- * removing the named logger. The logger is searched for by referential
- * equality (comparing the object references) and not calling the
- * <code>equals</code> method. If the last element is being removed from
- * the list, <code>NONE</code> is returned instead of an empty list.
- *
- * @param list The list from which the service is to be removed.
- * @param requestLoggerService The service to remove.
- * @return The list without the service. This may be the same list if the
- * service is not in the list or may be <code>NONE</code> if the
- * last service has just been removed from the list.
- */
- private RequestLoggerService[] removeService(RequestLoggerService[] list,
- RequestLoggerService requestLoggerService) {
+ private RequestLoggerService[] removeService(RequestLoggerService[] list, RequestLoggerService requestLoggerService) {
RequestLoggerService[] newList = NONE;
for (int i = 0; i < list.length; i++) {
@@ -186,8 +130,7 @@ public final class RequestLoggerFilter i
// if not the last element, shift rest to the left
if (i < list.length - 1) {
- System.arraycopy(list, i + 1, newList, 0, newList.length
- - i);
+ System.arraycopy(list, i + 1, newList, 0, newList.length - i);
}
}
}
@@ -198,8 +141,12 @@ public final class RequestLoggerFilter i
private void log(RequestLoggerService[] services, final RequestLoggerRequest request,
final RequestLoggerResponse response) {
- for (int i = 0; i < services.length; i++) {
- services[i].log(request, response);
+ for (RequestLoggerService service : services) {
+ try {
+ service.log(request, response);
+ } catch (Exception e) {
+ LoggerFactory.getLogger(getClass()).debug("log: RequestLoggerService failed logging", e);
+ }
}
}
}
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerRequest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerRequest.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerRequest.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerRequest.java Thu Feb 2 11:53:33 2012
@@ -18,13 +18,47 @@
*/
package org.apache.sling.engine.impl.log;
+import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
+import org.osgi.service.http.HttpContext;
+
class RequestLoggerRequest extends HttpServletRequestWrapper {
RequestLoggerRequest(HttpServletRequest request) {
super(request);
}
+ @Override
+ public String getRemoteUser() {
+ final Object user = getAttribute(HttpContext.REMOTE_USER);
+ if (user instanceof String) {
+ return (String) user;
+ }
+
+ return super.getRemoteUser();
+ }
+
+ public String getAuthType() {
+ final Object authType = getAttribute(HttpContext.AUTHENTICATION_TYPE);
+ if (authType instanceof String) {
+ return (String) authType;
+ }
+
+ return super.getAuthType();
+ }
+
+ Cookie getCookie(final String name) {
+ final Cookie[] cookies = getCookies();
+ if (cookies != null) {
+ for (int i = 0; i < cookies.length; i++) {
+ if (cookies[i].getName().equals(name)) {
+ return cookies[i];
+ }
+ }
+ }
+
+ return null;
+ }
}
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java Thu Feb 2 11:53:33 2012
@@ -35,7 +35,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
-public class RequestLoggerResponse extends HttpServletResponseWrapper {
+class RequestLoggerResponse extends HttpServletResponseWrapper {
// the content type header name
private static final String HEADER_CONTENT_TYPE = "Content-Type";
@@ -44,8 +44,8 @@ public class RequestLoggerResponse exten
private static final String HEADER_CONTENT_LENGTH = "Content-Length";
/** format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT" */
- private final static SimpleDateFormat RFC1123_FORMAT = new SimpleDateFormat(
- "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
+ private final static SimpleDateFormat RFC1123_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
+ Locale.US);
/**
* The counter for request gone through this filter. As this is the first
@@ -89,7 +89,7 @@ public class RequestLoggerResponse exten
// headers
private Map<String, Object> headers;
- public RequestLoggerResponse(HttpServletResponse response) {
+ RequestLoggerResponse(HttpServletResponse response) {
super(response);
this.requestId = requestCounter.getAndIncrement();
@@ -101,20 +101,16 @@ public class RequestLoggerResponse exten
* currently sets the request end time returned by {@link #getRequestEnd()}
* and which is used to calculate the request duration.
*/
- public void requestEnd() {
+ void requestEnd() {
this.requestEnd = System.currentTimeMillis();
}
-// protected final RequestData getRequestData() {
-// return requestData;
-// }
-
- //---------- SlingHttpServletResponse interface
+ // ---------- SlingHttpServletResponse interface
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (this.out == null) {
- ServletOutputStream sos = getResponse().getOutputStream();
+ ServletOutputStream sos = super.getOutputStream();
this.out = new LoggerResponseOutputStream(sos);
}
return this.out;
@@ -123,7 +119,7 @@ public class RequestLoggerResponse exten
@Override
public PrintWriter getWriter() throws IOException {
if (this.writer == null) {
- PrintWriter pw = getResponse().getWriter();
+ PrintWriter pw = super.getWriter();
this.writer = new LoggerResponseWriter(pw);
}
return this.writer;
@@ -131,7 +127,6 @@ public class RequestLoggerResponse exten
// ---------- Error handling through Sling Error Resolver -----------------
-
@Override
public void sendRedirect(String location) throws IOException {
super.sendRedirect(location);
@@ -293,11 +288,11 @@ public class RequestLoggerResponse exten
/**
* Stores the name header-value pair in the header map. The name is
* converted to lower-case before using it as an index in the map.
- *
+ *
* @param name The name of the header to register
* @param value The value of the header to register
- * @param add If <code>true</code> the header value is added to the list
- * of potentially existing header values. Otherwise the new value
+ * @param add If <code>true</code> the header value is added to the list of
+ * potentially existing header values. Otherwise the new value
* replaces any existing values.
*/
@SuppressWarnings("unchecked")
@@ -335,10 +330,9 @@ public class RequestLoggerResponse exten
* Converts the time value given as the number of milliseconds since January
* 1, 1970 to a date and time string compliant with RFC 1123 date
* specification. The resulting string is compliant with section 3.3.1, Full
- * Date, of <a href="http://www.faqs.org/rfcs/rfc2616.html">RFC 2616</a>
- * and may thus be used as the value of date header such as
- * <code>Date</code>.
- *
+ * Date, of <a href="http://www.faqs.org/rfcs/rfc2616.html">RFC 2616</a> and
+ * may thus be used as the value of date header such as <code>Date</code>.
+ *
* @param date The date value to convert to a string
* @return The string representation of the date and time value.
*/
@@ -348,7 +342,7 @@ public class RequestLoggerResponse exten
}
}
- //---------- byte/character counting output channels ----------------------
+ // ---------- byte/character counting output channels ----------------------
// byte transfer counting ServletOutputStream
private static class LoggerResponseOutputStream extends ServletOutputStream {
@@ -391,8 +385,7 @@ public class RequestLoggerResponse exten
// character transfer counting PrintWriter
private static class LoggerResponseWriter extends PrintWriter {
- private static final int LINE_SEPARATOR_LENGTH = System.getProperty(
- "line.separator").length();
+ private static final int LINE_SEPARATOR_LENGTH = System.getProperty("line.separator").length();
private int count;
Modified: sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerService.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerService.java?rev=1239560&r1=1239559&r2=1239560&view=diff
==============================================================================
--- sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerService.java (original)
+++ sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerService.java Thu Feb 2 11:53:33 2012
@@ -36,33 +36,33 @@ import org.osgi.framework.BundleContext;
/**
* The <code>RequestLoggerService</code> is a factory component which gets
* configuration to register loggers for the {@link RequestLogger}.
- *
*/
-@Component(metatype=true,label="%request.log.service.name",
- description="%request.log.service.description", configurationFactory=true,
- policy=ConfigurationPolicy.REQUIRE)
+@Component(
+ metatype = true,
+ label = "%request.log.service.name",
+ description = "%request.log.service.description",
+ configurationFactory = true,
+ policy = ConfigurationPolicy.REQUIRE)
@Properties({
- @Property(name="service.description",value="Factory for configuration based request/access loggers"),
- @Property(name="service.vendor",value="The Apache Software Foundation")
+ @Property(name = "service.description", value = "Factory for configuration based request/access loggers"),
+ @Property(name = "service.vendor", value = "The Apache Software Foundation")
})
-@Service(value=RequestLoggerService.class)
-
+@Service(value = RequestLoggerService.class)
public class RequestLoggerService {
@Property
public static final String PARAM_FORMAT = "request.log.service.format";
- @Property(value="request.log")
+ @Property(value = "request.log")
public static final String PARAM_OUTPUT = "request.log.service.output";
- @Property(intValue=0,options={
- @PropertyOption(name = "0", value = "Logger Name"),
- @PropertyOption(name = "1", value = "File Name"),
- @PropertyOption(name = "2", value = "RequestLog Service")
+ @Property(intValue = 0, options = {
+ @PropertyOption(name = "0", value = "Logger Name"), @PropertyOption(name = "1", value = "File Name"),
+ @PropertyOption(name = "2", value = "RequestLog Service")
})
public static final String PARAM_OUTPUT_TYPE = "request.log.service.outputtype";
- @Property(boolValue=false)
+ @Property(boolValue = false)
public static final String PARAM_ON_ENTRY = "request.log.service.onentry";
private static final int OUTPUT_TYPE_LOGGER = 0;
@@ -91,9 +91,7 @@ public class RequestLoggerService {
void setup(BundleContext bundleContext, Map<String, Object> configuration) {
// whether to log on request entry or request exit
Object onEntryObject = configuration.get(PARAM_ON_ENTRY);
- this.onEntry = (onEntryObject instanceof Boolean)
- ? ((Boolean) onEntryObject).booleanValue()
- : false;
+ this.onEntry = (onEntryObject instanceof Boolean) ? ((Boolean) onEntryObject).booleanValue() : false;
// shared or private CustomLogFormat
Object format = configuration.get(PARAM_FORMAT);
@@ -132,8 +130,7 @@ public class RequestLoggerService {
return this.onEntry;
}
- private RequestLog getLog(BundleContext bundleContext, String output,
- int outputType) {
+ private RequestLog getLog(BundleContext bundleContext, String output, int outputType) {
switch (outputType) {
case OUTPUT_TYPE_FILE:
// file logging