Move CSV-specific classes into CSV-renderer
Don't expose outside the CSV rendering class all the details
about the event-specific field rendering.
This is a preparation work to rename "rendering" to "formatting"
and thus simply the configuration to:
[plugin "audit-sl4j"]
format = JSON
Change-Id: Ib30791102f0f2ec5db31890d713c15d22d42e0f7
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditEventFormat.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditEventFormat.java
deleted file mode 100644
index cb07193..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditEventFormat.java
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright (C) 2012 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-import com.google.gerrit.audit.AuditEvent;
-import com.google.gerrit.audit.SshAuditEvent;
-
-public class AuditEventFormat implements AuditFormatter<SshAuditEvent> {
- public static final Class<?> CLASS = AuditEvent.class;
-
- @Override
- public String format(SshAuditEvent result) {
- return "";
- }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatter.java
deleted file mode 100644
index 67c2589..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatter.java
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright (C) 2012 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-public interface AuditFormatter<T> {
-
- String format(T result);
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatters.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatters.java
deleted file mode 100644
index 4e3798b..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditFormatters.java
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright (C) 2018 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-import java.text.SimpleDateFormat;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-
-class AuditFormatters {
- private static final SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSSS");
-
- @SuppressWarnings("serial")
- private static final Map<Class<?>, AuditFormatter<?>> AUDIT_FORMATTERS =
- Collections.unmodifiableMap(
- new HashMap<Class<?>, AuditFormatter<? extends Object>>() {
- {
- put(HttpAuditEventFormat.CLASS, new HttpAuditEventFormat());
- put(RpcAuditEventFormat.CLASS, new RpcAuditEventFormat());
- put(SshAuditEventFormat.CLASS, new SshAuditEventFormat());
- put(AuditEventFormat.CLASS, new AuditEventFormat());
- }
- });
-
- public static <T> String getFormattedAuditSingle(T result) {
- if (result == null) return "";
-
- @SuppressWarnings("unchecked")
- AuditFormatter<T> fmt = (AuditFormatter<T>) AUDIT_FORMATTERS.get(result.getClass());
- if (fmt == null) return result.toString();
-
- return fmt.format(result);
- }
-
- public static synchronized String getFormattedTS(long when) {
- return dateFmt.format(new Date(when));
- }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditRendererToCsv.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditRendererToCsv.java
index 041f846..1fec09b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditRendererToCsv.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/AuditRendererToCsv.java
@@ -14,17 +14,73 @@
package com.googlesource.gerrit.plugins.auditsl4j;
-import static com.googlesource.gerrit.plugins.auditsl4j.AuditFormatters.getFormattedAuditSingle;
-import static com.googlesource.gerrit.plugins.auditsl4j.AuditFormatters.getFormattedTS;
-
import com.google.common.collect.Multimap;
import com.google.gerrit.audit.AuditEvent;
+import com.google.gerrit.audit.ExtendedHttpAuditEvent;
+import com.google.gerrit.audit.HttpAuditEvent;
+import com.google.gerrit.audit.RpcAuditEvent;
+import com.google.gerrit.audit.SshAuditEvent;
+
+import java.text.SimpleDateFormat;
import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
public class AuditRendererToCsv implements AuditRenderer {
+
+ private static final SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSSS");
+
+ @SuppressWarnings("serial")
+ private static final Map<Class<?>, CsvFieldFormatter<?>> FIELD_CSV_FORMATTERS =
+ Collections.unmodifiableMap(
+ new HashMap<Class<?>, CsvFieldFormatter<? extends Object>>() {
+ {
+ put(HttpAuditEvent.class, new HttpAuditEventFormat());
+ put(ExtendedHttpAuditEvent.class, new HttpAuditEventFormat());
+ put(RpcAuditEvent.class, new RpcAuditEventFormat());
+ put(SshAuditEvent.class, new SshAuditEventFormat());
+ put(AuditEvent.class, new AuditEventFormat());
+ }
+ });
+
+ interface CsvFieldFormatter<T> {
+ String formatToCsv(T result);
+ }
+
+ static class RpcAuditEventFormat implements CsvFieldFormatter<RpcAuditEvent> {
+ @Override
+ public String formatToCsv(RpcAuditEvent result) {
+ return "RPC-" + result.httpMethod + ", Status:" + result.httpStatus;
+ }
+ }
+
+ static class HttpAuditEventFormat implements CsvFieldFormatter<HttpAuditEvent> {
+
+ @Override
+ public String formatToCsv(HttpAuditEvent result) {
+ return "HTTP-" + result.httpMethod + ", Status:" + result.httpStatus;
+ }
+ }
+
+ static class SshAuditEventFormat implements CsvFieldFormatter<SshAuditEvent> {
+ @Override
+ public String formatToCsv(SshAuditEvent result) {
+ return "SSH";
+ }
+ }
+
+ static class AuditEventFormat implements CsvFieldFormatter<SshAuditEvent> {
+
+ @Override
+ public String formatToCsv(SshAuditEvent result) {
+ return "";
+ }
+ }
@Override
public String render(AuditEvent auditEvent) {
@@ -33,11 +89,11 @@
auditEvent.uuid.uuid(),
getFormattedTS(auditEvent.when),
auditEvent.sessionId,
- getFormattedAuditSingle(auditEvent.who),
- getFormattedAuditSingle(auditEvent),
+ getFieldAsCsv(auditEvent.who),
+ getFieldAsCsv(auditEvent),
auditEvent.what,
getFormattedAuditList(auditEvent.params),
- getFormattedAuditSingle(auditEvent.result),
+ getFieldAsCsv(auditEvent.result),
getFormattedTS(auditEvent.timeAtStart),
auditEvent.elapsed);
}
@@ -79,7 +135,7 @@
if (numValues > 0) {
out.append(",");
}
- out.append(getFormattedAuditSingle(object));
+ out.append(getFieldAsCsv(object));
numValues++;
}
@@ -88,4 +144,19 @@
}
return out.toString();
}
+
+
+ public static <T> String getFieldAsCsv(T result) {
+ if (result == null) return "";
+
+ @SuppressWarnings("unchecked")
+ CsvFieldFormatter<T> fmt = (CsvFieldFormatter<T>) FIELD_CSV_FORMATTERS.get(result.getClass());
+ if (fmt == null) return result.toString();
+
+ return fmt.formatToCsv(result);
+ }
+
+ public static synchronized String getFormattedTS(long when) {
+ return dateFmt.format(new Date(when));
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/HttpAuditEventFormat.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/HttpAuditEventFormat.java
deleted file mode 100644
index 6551725..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/HttpAuditEventFormat.java
+++ /dev/null
@@ -1,26 +0,0 @@
-// Copyright (C) 2012 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-import com.google.gerrit.audit.HttpAuditEvent;
-
-public class HttpAuditEventFormat implements AuditFormatter<HttpAuditEvent> {
- protected static final Class<?> CLASS = HttpAuditEvent.class;
-
- @Override
- public String format(HttpAuditEvent result) {
- return "HTTP-" + result.httpMethod + ", Status:" + result.httpStatus;
- }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/RpcAuditEventFormat.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/RpcAuditEventFormat.java
deleted file mode 100644
index 053f302..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/RpcAuditEventFormat.java
+++ /dev/null
@@ -1,26 +0,0 @@
-// Copyright (C) 2012 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-import com.google.gerrit.audit.RpcAuditEvent;
-
-public class RpcAuditEventFormat implements AuditFormatter<RpcAuditEvent> {
- protected static final Class<?> CLASS = RpcAuditEvent.class;
-
- @Override
- public String format(RpcAuditEvent result) {
- return "RPC-" + result.httpMethod + ", Status:" + result.httpStatus;
- }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/SshAuditEventFormat.java b/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/SshAuditEventFormat.java
deleted file mode 100644
index 9a15f77..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/auditsl4j/SshAuditEventFormat.java
+++ /dev/null
@@ -1,26 +0,0 @@
-// Copyright (C) 2012 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.auditsl4j;
-
-import com.google.gerrit.audit.SshAuditEvent;
-
-public class SshAuditEventFormat implements AuditFormatter<SshAuditEvent> {
- protected static final Class<?> CLASS = SshAuditEvent.class;
-
- @Override
- public String format(SshAuditEvent result) {
- return "SSH";
- }
-}