|
34 | 34 | import java.util.HashMap;
|
35 | 35 | import java.util.List;
|
36 | 36 |
|
37 |
| -public class DefaultSdk implements Sdk { |
38 |
| - private final Annotator[] annotators; |
39 |
| - private final SdkInfo config; |
40 |
| - private final StreamProvider stream; |
41 |
| - private final Logger logger; |
42 |
| - |
43 |
| - public DefaultSdk(Annotator[] annotators, SdkInfo config, Logger logger) throws StreamException { |
44 |
| - this.annotators = annotators; |
45 |
| - this.config = config; |
46 |
| - this.logger = logger; |
47 |
| - |
48 |
| - // init stream |
49 |
| - final StreamProviderFactory streamFactory = new StreamProviderFactory(); |
50 |
| - this.stream = streamFactory.getProvider(this.config.getStream()); |
51 |
| - this.stream.connect(); |
52 |
| - this.logger.debug("stream provider connected successfully."); |
53 |
| - } |
54 |
| - |
55 |
| - public void create(PropertyBag properties, byte[] data) throws AnnotatorException, |
56 |
| - StreamException { |
57 |
| - final List<Annotation> annotations = this.createAnnotations(properties, data); |
58 |
| - this.publishAnnotations(SdkAction.CREATE, annotations); |
59 |
| - this.logger.debug("data annotated and published successfully."); |
60 |
| - } |
61 |
| - |
62 |
| - public void create(byte[] data) throws AnnotatorException, StreamException { |
63 |
| - final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>()); |
64 |
| - this.create(properties, data); |
65 |
| - } |
66 |
| - |
67 |
| - public void mutate(PropertyBag properties, byte[] oldData, byte[] newData) throws |
68 |
| - AnnotatorException, StreamException { |
69 |
| - final List<Annotation> annotations = new ArrayList<Annotation>(); |
70 |
| - |
71 |
| - // source annotate the old data |
72 |
| - final AnnotatorFactory annotatorFactory = new AnnotatorFactory(); |
73 |
| - final Annotator sourceAnnotator = annotatorFactory.getAnnotator( |
74 |
| - new AnnotatorConfig(AnnotationType.SOURCE), |
75 |
| - this.config, |
76 |
| - this.logger |
77 |
| - ); |
78 |
| - |
79 |
| - String key; |
80 |
| - try { |
81 |
| - var hasher = new HashProviderFactory().getProvider(config.getHash().getType()); |
82 |
| - key = hasher.derive(oldData); |
83 |
| - } catch (HashTypeException e) { |
84 |
| - throw new RuntimeException(e); |
85 |
| - } |
86 |
| - |
87 |
| - final Annotation sourceAnnotation = sourceAnnotator.execute(properties, oldData, key); |
88 |
| - annotations.add(sourceAnnotation); |
89 |
| - |
90 |
| - // Add annotations for new data |
91 |
| - for (Annotation annotation : this.createAnnotations(properties, newData)) { |
92 |
| - // TLS is ignored in mutate to prevent needless penalization |
93 |
| - // See https://github.com/project-alvarium/alvarium-sdk-go/issues/19 |
94 |
| - if (annotation.getKind() != AnnotationType.TLS) { |
95 |
| - annotations.add(annotation); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - // publish to the stream provider |
100 |
| - this.publishAnnotations(SdkAction.MUTATE, annotations); |
101 |
| - this.logger.debug("data annotated and published successfully."); |
102 |
| - } |
103 |
| - |
104 |
| - public void mutate(byte[] oldData, byte[] newData) throws AnnotatorException, StreamException { |
105 |
| - final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>()); |
106 |
| - this.mutate(properties, oldData, newData); |
107 |
| - } |
108 | 37 |
|
109 |
| - public void transit(PropertyBag properties, byte[] data) throws AnnotatorException, |
110 |
| - StreamException { |
111 |
| - final List<Annotation> annotations = this.createAnnotations(properties, data); |
112 |
| - this.publishAnnotations(SdkAction.TRANSIT, annotations); |
113 |
| - this.logger.debug("data annotated and published successfully."); |
114 |
| - } |
115 |
| - |
116 |
| - public void transit(byte[] data) throws AnnotatorException, StreamException { |
117 |
| - final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>()); |
118 |
| - this.transit(properties, data); |
119 |
| - } |
120 |
| - |
121 |
| - public void publish(PropertyBag properties, byte[] data) throws AnnotatorException, |
122 |
| - StreamException { |
123 |
| - final List<Annotation> annotations = this.createAnnotations(properties, data); |
124 |
| - this.publishAnnotations(SdkAction.PUBLISH, annotations); |
125 |
| - this.logger.debug("data annotated and published successfully."); |
126 |
| - } |
127 |
| - |
128 |
| - public void publish(byte[] data) throws AnnotatorException, StreamException { |
129 |
| - final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>()); |
130 |
| - this.publish(properties, data); |
131 |
| - } |
132 |
| - |
133 |
| - public void close() throws StreamException { |
134 |
| - this.stream.close(); |
135 |
| - this.logger.debug("stream provider connection terminated successfully."); |
| 38 | +public class DefaultSdk implements Sdk { |
| 39 | + private final Annotator[] annotators; |
| 40 | + private final SdkInfo config; |
| 41 | + private final StreamProvider stream; |
| 42 | + private final Logger logger; |
| 43 | + |
| 44 | + public DefaultSdk(Annotator[] annotators, SdkInfo config, Logger logger) throws StreamException { |
| 45 | + this.annotators = annotators; |
| 46 | + this.config = config; |
| 47 | + this.logger = logger; |
| 48 | + |
| 49 | + // init stream |
| 50 | + final StreamProviderFactory streamFactory = new StreamProviderFactory(); |
| 51 | + this.stream = streamFactory.getProvider(this.config.getStream()); |
| 52 | + this.stream.connect(); |
| 53 | + this.logger.debug("stream provider connected successfully."); |
| 54 | + } |
| 55 | + |
| 56 | + public void create(PropertyBag properties, byte[] data) throws AnnotatorException, |
| 57 | + StreamException, HashTypeException { |
| 58 | + final List<Annotation> annotations = this.createAnnotations(properties, data); |
| 59 | + this.publishAnnotations(SdkAction.CREATE, annotations); |
| 60 | + this.logger.debug("data annotated and published successfully."); |
| 61 | + } |
| 62 | + |
| 63 | + public void create(byte[] data) throws AnnotatorException, StreamException, HashTypeException { |
| 64 | + final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>()); |
| 65 | + this.create(properties, data); |
| 66 | + } |
| 67 | + |
| 68 | + public void mutate(PropertyBag properties, byte[] oldData, byte[] newData) throws |
| 69 | + AnnotatorException, StreamException, HashTypeException { |
| 70 | + final List<Annotation> annotations = new ArrayList<Annotation>(); |
| 71 | + |
| 72 | + // source annotate the old data |
| 73 | + final AnnotatorFactory annotatorFactory = new AnnotatorFactory(); |
| 74 | + final Annotator sourceAnnotator = annotatorFactory.getAnnotator( |
| 75 | + new AnnotatorConfig(AnnotationType.SOURCE), |
| 76 | + this.config, |
| 77 | + this.logger |
| 78 | + ); |
| 79 | + |
| 80 | + String key; |
| 81 | + var hasher = new HashProviderFactory().getProvider(config.getHash().getType()); |
| 82 | + key = hasher.derive(oldData); |
| 83 | + |
| 84 | + final Annotation sourceAnnotation = sourceAnnotator.execute(properties, oldData, key); |
| 85 | + annotations.add(sourceAnnotation); |
| 86 | + |
| 87 | + // Add annotations for new data |
| 88 | + for (Annotation annotation : this.createAnnotations(properties, newData)) { |
| 89 | + // TLS is ignored in mutate to prevent needless penalization |
| 90 | + // See https://github.com/project-alvarium/alvarium-sdk-go/issues/19 |
| 91 | + if (annotation.getKind() != AnnotationType.TLS) { |
| 92 | + annotations.add(annotation); |
| 93 | + } |
136 | 94 | }
|
137 | 95 |
|
138 |
| - /** |
139 |
| - * Executes all the specified annotators and returns a list of all the created annotations |
140 |
| - * |
141 |
| - * @param properties |
142 |
| - * @param data |
143 |
| - * @return |
144 |
| - * @throws AnnotatorException |
145 |
| - */ |
146 |
| - private List<Annotation> createAnnotations(PropertyBag properties, byte[] data) |
147 |
| - throws AnnotatorException { |
148 |
| - final List<Annotation> annotations = new ArrayList<>(); |
149 |
| - |
150 |
| - String key; |
151 |
| - try { |
152 |
| - var hasher = new HashProviderFactory().getProvider(config.getHash().getType()); |
153 |
| - key = hasher.derive(data); |
154 |
| - } catch (HashTypeException e) { |
155 |
| - throw new RuntimeException(e); |
156 |
| - } |
157 |
| - |
158 |
| - // Annotate incoming data |
159 |
| - for (Annotator annotator : this.annotators) { |
160 |
| - final Annotation annotation = annotator.execute(properties, data, key); |
161 |
| - annotations.add(annotation); |
162 |
| - } |
163 |
| - |
164 |
| - return annotations; |
| 96 | + // publish to the stream provider |
| 97 | + this.publishAnnotations(SdkAction.MUTATE, annotations); |
| 98 | + this.logger.debug("data annotated and published successfully."); |
| 99 | + } |
| 100 | + |
| 101 | + public void mutate(byte[] oldData, byte[] newData) throws AnnotatorException, StreamException, HashTypeException { |
| 102 | + final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>()); |
| 103 | + this.mutate(properties, oldData, newData); |
| 104 | + } |
| 105 | + |
| 106 | + public void transit(PropertyBag properties, byte[] data) throws AnnotatorException, |
| 107 | + StreamException, HashTypeException { |
| 108 | + final List<Annotation> annotations = this.createAnnotations(properties, data); |
| 109 | + this.publishAnnotations(SdkAction.TRANSIT, annotations); |
| 110 | + this.logger.debug("data annotated and published successfully."); |
| 111 | + } |
| 112 | + |
| 113 | + public void transit(byte[] data) throws AnnotatorException, StreamException, HashTypeException { |
| 114 | + final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>()); |
| 115 | + this.transit(properties, data); |
| 116 | + } |
| 117 | + |
| 118 | + public void publish(PropertyBag properties, byte[] data) throws AnnotatorException, |
| 119 | + StreamException, HashTypeException { |
| 120 | + final List<Annotation> annotations = this.createAnnotations(properties, data); |
| 121 | + this.publishAnnotations(SdkAction.PUBLISH, annotations); |
| 122 | + this.logger.debug("data annotated and published successfully."); |
| 123 | + } |
| 124 | + |
| 125 | + public void publish(byte[] data) throws AnnotatorException, StreamException, HashTypeException { |
| 126 | + final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>()); |
| 127 | + this.publish(properties, data); |
| 128 | + } |
| 129 | + |
| 130 | + public void close() throws StreamException { |
| 131 | + this.stream.close(); |
| 132 | + this.logger.debug("stream provider connection terminated successfully."); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Executes all the specified annotators and returns a list of all the created annotations |
| 137 | + * |
| 138 | + * @param properties |
| 139 | + * @param data |
| 140 | + * @return |
| 141 | + * @throws AnnotatorException |
| 142 | + */ |
| 143 | + private List<Annotation> createAnnotations(PropertyBag properties, byte[] data) |
| 144 | + throws AnnotatorException, HashTypeException { |
| 145 | + final List<Annotation> annotations = new ArrayList<>(); |
| 146 | + |
| 147 | + String key; |
| 148 | + var hasher = new HashProviderFactory().getProvider(config.getHash().getType()); |
| 149 | + key = hasher.derive(data); |
| 150 | + |
| 151 | + // Annotate incoming data |
| 152 | + for (Annotator annotator : this.annotators) { |
| 153 | + final Annotation annotation = annotator.execute(properties, data, key); |
| 154 | + annotations.add(annotation); |
165 | 155 | }
|
166 | 156 |
|
167 |
| - /** |
168 |
| - * Wraps the annotation list with a publish wrapper that specifies the SDK action and the |
169 |
| - * content type |
170 |
| - * |
171 |
| - * @param action |
172 |
| - * @param annotations |
173 |
| - * @throws StreamException |
174 |
| - */ |
175 |
| - private void publishAnnotations(SdkAction action, List<Annotation> annotations) |
176 |
| - throws StreamException { |
177 |
| - final AnnotationList annotationList = new AnnotationList(annotations); |
178 |
| - |
179 |
| - // publish list of annotations to the StreamProvider |
180 |
| - final PublishWrapper wrapper = new PublishWrapper( |
181 |
| - action, |
182 |
| - annotationList.getClass().getName(), |
183 |
| - annotationList |
184 |
| - ); |
185 |
| - this.stream.publish(wrapper); |
186 |
| - } |
| 157 | + return annotations; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Wraps the annotation list with a publish wrapper that specifies the SDK action and the |
| 162 | + * content type |
| 163 | + * |
| 164 | + * @param action |
| 165 | + * @param annotations |
| 166 | + * @throws StreamException |
| 167 | + */ |
| 168 | + private void publishAnnotations(SdkAction action, List<Annotation> annotations) |
| 169 | + throws StreamException { |
| 170 | + final AnnotationList annotationList = new AnnotationList(annotations); |
| 171 | + |
| 172 | + // publish list of annotations to the StreamProvider |
| 173 | + final PublishWrapper wrapper = new PublishWrapper( |
| 174 | + action, |
| 175 | + annotationList.getClass().getName(), |
| 176 | + annotationList |
| 177 | + ); |
| 178 | + this.stream.publish(wrapper); |
| 179 | + } |
187 | 180 | }
|
0 commit comments