-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStatement.java
More file actions
378 lines (302 loc) · 10.3 KB
/
Statement.java
File metadata and controls
378 lines (302 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import dev.learning.xapi.model.Agent.AgentObjectType;
import dev.learning.xapi.model.validation.constraints.ValidActor;
import dev.learning.xapi.model.validation.constraints.ValidAuthority;
import dev.learning.xapi.model.validation.constraints.ValidStatementPlatform;
import dev.learning.xapi.model.validation.constraints.ValidStatementRevision;
import dev.learning.xapi.model.validation.constraints.ValidStatementVerb;
import dev.learning.xapi.model.validation.constraints.Variant;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.lang.UnknownClassException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import java.net.URI;
import java.security.PrivateKey;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.With;
/**
* This class represents the xAPI Statement object.
*
* @author Thomas Turrell-Croft
* @author István Rátkai (Selindek)
* @see <a href=
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#statement-properties">xAPI
* Statement</a>
*/
@With
@Value
@ValidStatementPlatform
@ValidStatementRevision
@ValidStatementVerb
@Builder(toBuilder = true)
@JsonInclude(Include.NON_EMPTY)
@EqualsAndHashCode(of = {"actor", "verb", "object", "result", "context"})
public class Statement implements CoreStatement {
/** UUID assigned by LRS if not set by the Learning Record Provider. */
@Variant(2)
private UUID id;
/** Whom the Statement is about, as an Agent or Group Object. */
@NotNull @Valid @ValidActor private Actor actor;
/** Action taken by the Actor. */
@NotNull @Valid private Verb verb;
/** Activity, Agent, or another Statement that is the Object of the Statement. */
@NotNull @Valid @ValidActor private StatementObject object;
/** Result Object, further details representing a measured outcome. */
@Valid private Result result;
/** Context that gives the Statement more meaning. */
@Valid private Context context;
/** Timestamp of when the events described within this Statement occurred. */
private Instant timestamp;
/** Timestamp of when this Statement was recorded. */
private Instant stored;
/** Agent or Group who is asserting this Statement is true. */
@Valid @ValidActor @ValidAuthority private Actor authority;
/** The Statement’s associated xAPI version. */
@Pattern(regexp = "^1\\.0(\\.\\d)?$")
private String version;
/** Headers for Attachments to the Statement. */
@Valid
@JsonFormat(without = {JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY})
private List<Attachment> attachments;
// **Warning** do not add fields that are not required by the xAPI specification.
/** Builder for Statement. */
public static class Builder {
// This static class extends the lombok builder.
/**
* Special build method for signing and building a {@link Statement}.
*
* <p>An signature attachment is automatically added to the Statement's attachments.
*
* @param privateKey a {@link PrivateKey} for signing the {@link Statement}.
* @return an immutable, signed {@link Statement} object.
* @see <a href=
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#26-signed-statements">
* Signed Statements</a>
*/
public Statement signAndBuild(PrivateKey privateKey) {
final Map<String, Object> claims = new HashMap<>();
// Put only the significant properties into the signature payload
// https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#statement-comparision-requirements
claims.put("actor", this.actor);
claims.put("verb", this.verb);
claims.put("object", this.object);
claims.put("result", this.result);
claims.put("context", this.context);
try {
final var token =
Jwts.builder().claims(claims).signWith(privateKey, Jwts.SIG.RS512).compact();
addAttachment(
a ->
a.usageType(URI.create("http://adlnet.gov/expapi/attachments/signature"))
.addDisplay(Locale.ENGLISH, "JSW signature")
.content(token)
.length(token.length())
.contentType("application/octet-stream"));
} catch (final UnknownClassException | ExceptionInInitializerError e) {
throw new IllegalStateException(
"""
Statement cannot be signed, because an optional dependency was NOT provided.
Please add the following dependencies into your project:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
</dependency>
""",
e);
}
return build();
}
/**
* Consumer Builder for agent.
*
* @param agent The Consumer Builder for agent
* @return This builder
* @see Statement#actor
*/
public Builder agentActor(Consumer<Agent.Builder<?, ?>> agent) {
final Agent.Builder<?, ?> builder = Agent.builder();
agent.accept(builder);
return actor(builder.build());
}
/**
* Consumer Builder for group.
*
* @param group The Consumer Builder for group
* @return This builder
* @see Statement#actor
*/
public Builder groupActor(Consumer<Group.Builder<?, ?>> group) {
final Group.Builder<?, ?> builder = Group.builder();
group.accept(builder);
return actor(builder.build());
}
/**
* Consumer Builder for verb.
*
* @param verb The Consumer Builder for verb
* @return This builder
* @see Statement#verb
*/
public Builder verb(Consumer<Verb.Builder> verb) {
final var builder = Verb.builder();
verb.accept(builder);
return verb(builder.build());
}
/**
* Sets the verb.
*
* @param verb The definition of the Statement
* @return This builder
* @see Statement#verb
*/
public Builder verb(Verb verb) {
this.verb = verb;
return this;
}
/**
* Consumer Builder for result.
*
* @param result The Consumer Builder for result
* @return This builder
* @see Statement#result
*/
public Builder result(Consumer<Result.Builder> result) {
final var builder = Result.builder();
result.accept(builder);
return result(builder.build());
}
/**
* Sets the result.
*
* @param result The result of the Statement
* @return This builder
* @see Statement#result
*/
public Builder result(Result result) {
this.result = result;
return this;
}
/**
* Consumer Builder for context.
*
* @param authority The Consumer Builder for authority
* @return This builder
* @see Statement#authority
*/
public Builder agentAuthority(Consumer<Agent.Builder<?, ?>> authority) {
final Agent.Builder<?, ?> builder = Agent.builder();
authority.accept(builder);
return authority(builder.build());
}
/**
* Sets the object. <b> This custom setter makes sure that if the object is an Agent then its
* objectType property was set properly. </b>
*
* @param object The object of the Statement.
* @return This builder.
*/
public Builder object(StatementObject object) {
if (object instanceof final Agent agent && AgentObjectType.AGENT != agent.getObjectType()) {
this.object = agent.toBuilder().objectType(AgentObjectType.AGENT).build();
} else {
this.object = object;
}
return this;
}
/**
* Consumer Builder for activity object.
*
* @param activity The Consumer Builder for activity object
* @return This builder
* @see Statement#object
*/
public Builder activityObject(Consumer<Activity.Builder> activity) {
final var builder = Activity.builder();
activity.accept(builder);
return object(builder.build());
}
/**
* Consumer Builder for statement reference object.
*
* @param statementReference The Consumer Builder for statement reference object
* @return This builder
* @see Statement#object
*/
public Builder statementReferenceObject(
Consumer<StatementReference.Builder> statementReference) {
final var builder = StatementReference.builder();
statementReference.accept(builder);
return object(builder.build());
}
/**
* Consumer Builder for context.
*
* @param context The Consumer Builder for context
* @return This builder
* @see Statement#context
*/
public Builder context(Consumer<Context.Builder> context) {
final var builder = Context.builder();
context.accept(builder);
return context(builder.build());
}
/**
* Sets the context.
*
* @param context The context of the Statement
* @return This builder
* @see Statement#context
*/
public Builder context(Context context) {
this.context = context;
return this;
}
/**
* Adds an attachment.
*
* @param attachment An {@link Attachment} object.
* @return This builder
* @see Statement#attachments
*/
public Builder addAttachment(Attachment attachment) {
if (this.attachments == null) {
this.attachments = new ArrayList<>();
}
this.attachments.add(attachment);
return this;
}
/**
* Consumer Builder for attachment.
*
* @param attachment The Consumer Builder for attachment
* @return This builder
* @see Statement#attachments
*/
public Builder addAttachment(Consumer<Attachment.Builder> attachment) {
final var builder = Attachment.builder();
attachment.accept(builder);
return addAttachment(builder.build());
}
}
}