|
18 | 18 |
|
19 | 19 | package org.audit4j.core;
|
20 | 20 |
|
21 |
| -import java.lang.annotation.Annotation; |
22 |
| -import java.lang.reflect.Method; |
23 |
| -import java.util.ArrayList; |
24 |
| -import java.util.List; |
25 |
| - |
26 |
| -import org.audit4j.core.annotation.Audit; |
27 |
| -import org.audit4j.core.annotation.AuditField; |
28 |
| -import org.audit4j.core.annotation.DeIdentify; |
29 |
| -import org.audit4j.core.annotation.IgnoreAudit; |
30 | 21 | import org.audit4j.core.dto.AnnotationAuditEvent;
|
31 | 22 | import org.audit4j.core.dto.AuditEvent;
|
32 |
| -import org.audit4j.core.dto.Field; |
33 | 23 |
|
34 | 24 | /**
|
35 |
| - * The Class AnnotationTransformer use to transform annotation information in to |
36 |
| - * simple audit event. |
| 25 | + * Interface for different annotation transformer implementations. This can be |
| 26 | + * used to transform annotation information in to simple audit event. |
37 | 27 | *
|
38 | 28 | * @author <a href="mailto:[email protected]">Janith Bandara</a>
|
39 | 29 | *
|
40 |
| - * @since 2.0.0 |
| 30 | + * @since 2.4.1 |
41 | 31 | */
|
42 |
| -public class AnnotationTransformer { |
43 |
| - |
44 |
| - private final static String ACTION = "action"; |
45 |
| - |
46 |
| - // Default Fields serializer |
47 |
| - private ObjectSerializer serializer = new ObjectToFieldsSerializer(); |
48 |
| - |
49 |
| - |
50 |
| - /** |
51 |
| - * Transform annotation informations to Audit Event object. |
52 |
| - * |
53 |
| - * @param annotationEvent |
54 |
| - * the annotation event |
55 |
| - * @return the audit event |
56 |
| - * @since 2.0.0 |
57 |
| - */ |
58 |
| - public AuditEvent transformToEvent(AnnotationAuditEvent annotationEvent) { |
59 |
| - AuditEvent event = null; |
60 |
| - |
61 |
| - if (annotationEvent.getClazz().isAnnotationPresent(Audit.class) |
62 |
| - && !annotationEvent.getMethod().isAnnotationPresent(IgnoreAudit.class)) { |
63 |
| - event = new AuditEvent(); |
64 |
| - Audit audit = annotationEvent.getClazz().getAnnotation(Audit.class); |
65 |
| - |
66 |
| - // Extract fields |
67 |
| - event.setFields(getFields(annotationEvent.getMethod(), annotationEvent.getArgs())); |
68 |
| - |
69 |
| - // Extract Actor |
70 |
| - String annotationAction = audit.action(); |
71 |
| - if (ACTION.equals(annotationAction)) { |
72 |
| - event.setAction(annotationEvent.getMethod().getName()); |
73 |
| - } else { |
74 |
| - event.setAction(annotationAction); |
75 |
| - } |
76 |
| - |
77 |
| - // Extract repository |
78 |
| - event.setRepository(audit.repository()); |
79 |
| - |
80 |
| - event.setActor(annotationEvent.getActor()); |
81 |
| - event.setOrigin(annotationEvent.getOrigin()); |
82 |
| - } else if (!annotationEvent.getClazz().isAnnotationPresent(Audit.class) |
83 |
| - && annotationEvent.getMethod().isAnnotationPresent(Audit.class)) { |
84 |
| - event = new AuditEvent(); |
85 |
| - Audit audit = annotationEvent.getMethod().getAnnotation(Audit.class); |
86 |
| - |
87 |
| - // Extract fields |
88 |
| - event.setFields(getFields(annotationEvent.getMethod(), annotationEvent.getArgs())); |
89 |
| - |
90 |
| - // Extract Actor |
91 |
| - String annotationAction = audit.action(); |
92 |
| - if (ACTION.equals(annotationAction)) { |
93 |
| - event.setAction(annotationEvent.getMethod().getName()); |
94 |
| - } else { |
95 |
| - event.setAction(annotationAction); |
96 |
| - } |
97 |
| - |
98 |
| - // Extract repository |
99 |
| - event.setRepository(audit.repository()); |
100 |
| - |
101 |
| - event.setActor(annotationEvent.getActor()); |
102 |
| - event.setOrigin(annotationEvent.getOrigin()); |
103 |
| - } |
104 |
| - |
105 |
| - return event; |
106 |
| - } |
107 |
| - |
108 |
| - /** |
109 |
| - * Extract fields based on annotations. |
110 |
| - * |
111 |
| - * @param method |
112 |
| - * : Class method with annotations. |
113 |
| - * @param params |
114 |
| - * : Method parameter values. |
115 |
| - * |
116 |
| - * @return list of fields extracted from method. |
117 |
| - * |
118 |
| - * @since 2.4.1 |
119 |
| - */ |
120 |
| - private List<Field> getFields(final Method method, final Object[] params) { |
121 |
| - final Annotation[][] parameterAnnotations = method.getParameterAnnotations(); |
122 |
| - |
123 |
| - final List<Field> fields = new ArrayList<Field>(); |
124 |
| - |
125 |
| - int i = 0; |
126 |
| - String paramName = null; |
127 |
| - for (final Annotation[] annotations : parameterAnnotations) { |
128 |
| - final Object object = params[i++]; |
129 |
| - boolean ignoreFlag = false; |
130 |
| - DeIdentify deidentify = null; |
131 |
| - for (final Annotation annotation : annotations) { |
132 |
| - if (annotation instanceof IgnoreAudit) { |
133 |
| - ignoreFlag = true; |
134 |
| - break; |
135 |
| - } |
136 |
| - if (annotation instanceof AuditField) { |
137 |
| - final AuditField field = (AuditField) annotation; |
138 |
| - paramName = field.field(); |
139 |
| - } |
140 |
| - if (annotation instanceof DeIdentify) { |
141 |
| - deidentify = (DeIdentify) annotation; |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - if (ignoreFlag) { |
146 |
| - |
147 |
| - } else { |
148 |
| - if (null == paramName) { |
149 |
| - paramName = "arg" + i; |
150 |
| - } |
151 |
| - serializer.serialize(fields, object, paramName, deidentify); |
152 |
| - } |
153 |
| - |
154 |
| - paramName = null; |
155 |
| - } |
156 |
| - return fields; |
157 |
| - } |
158 |
| - |
159 |
| - |
160 |
| - public void setSerializer(ObjectSerializer serializer) { |
161 |
| - this.serializer = serializer; |
162 |
| - } |
| 32 | +public interface AnnotationTransformer { |
| 33 | + |
| 34 | + /** |
| 35 | + * Transform to event. |
| 36 | + * |
| 37 | + * @param annotationEvent |
| 38 | + * input Annotation AuditEvent |
| 39 | + * @return transform audit event |
| 40 | + */ |
| 41 | + AuditEvent transformToEvent(AnnotationAuditEvent annotationEvent); |
163 | 42 | }
|
0 commit comments