44
55package dev .learning .xapi .samples .xapiserver ;
66
7- import com .fasterxml .jackson .core .JsonProcessingException ;
8- import com .fasterxml .jackson .databind .JsonNode ;
9- import com .fasterxml .jackson .databind .ObjectMapper ;
107import dev .learning .xapi .model .Agent ;
8+ import jakarta .validation .Valid ;
119import java .util .List ;
1210import java .util .UUID ;
1311import org .slf4j .Logger ;
1412import org .slf4j .LoggerFactory ;
1513import org .springframework .http .HttpStatus ;
14+ import org .springframework .http .MediaType ;
1615import org .springframework .http .ResponseEntity ;
1716import org .springframework .validation .annotation .Validated ;
1817import org .springframework .web .bind .annotation .DeleteMapping ;
@@ -41,19 +40,9 @@ public class StateController {
4140 Logger log = LoggerFactory .getLogger (StateController .class );
4241
4342 private final StateService stateService ;
44- private final ObjectMapper objectMapper ;
4543
46- public StateController (StateService stateService , ObjectMapper objectMapper ) {
44+ public StateController (StateService stateService ) {
4745 this .stateService = stateService ;
48- this .objectMapper = objectMapper ;
49- }
50-
51- private Agent parseAgent (String agentJson ) {
52- try {
53- return objectMapper .readValue (agentJson , Agent .class );
54- } catch (JsonProcessingException e ) {
55- throw new IllegalArgumentException ("Invalid agent JSON" , e );
56- }
5746 }
5847
5948 /**
@@ -70,17 +59,17 @@ private Agent parseAgent(String agentJson) {
7059 * Document</a>
7160 */
7261 @ GetMapping (params = {"activityId" , "agent" , "stateId" })
73- public ResponseEntity <JsonNode > getState (@ RequestParam (required = true ) String activityId ,
74- @ RequestParam (required = true ) String agent ,
62+ public ResponseEntity <String > getState (@ RequestParam (required = true ) String activityId ,
63+ @ Valid @ RequestParam (required = true ) Agent agent ,
7564 @ RequestParam (required = true ) String stateId ,
7665 @ RequestParam (required = false ) UUID registration ) {
7766
7867 log .debug ("GET state" );
7968
80- final Agent agentObj = parseAgent (agent );
81- final var state = stateService .getState (activityId , agentObj , stateId , registration );
69+ final var state = stateService .getState (activityId , agent , stateId , registration );
8270
83- return state .map (ResponseEntity ::ok ).orElseGet (() -> ResponseEntity .notFound ().build ());
71+ return state .map (s -> ResponseEntity .ok ().contentType (MediaType .APPLICATION_JSON ).body (s ))
72+ .orElseGet (() -> ResponseEntity .notFound ().build ());
8473 }
8574
8675 /**
@@ -98,13 +87,12 @@ public ResponseEntity<JsonNode> getState(@RequestParam(required = true) String a
9887 @ GetMapping (params = {"activityId" , "agent" , "!stateId" })
9988 public ResponseEntity <List <String >> getStateIds (
10089 @ RequestParam (required = true ) String activityId ,
101- @ RequestParam (required = true ) String agent ,
90+ @ Valid @ RequestParam (required = true ) Agent agent ,
10291 @ RequestParam (required = false ) UUID registration ) {
10392
10493 log .debug ("GET state ids" );
10594
106- final Agent agentObj = parseAgent (agent );
107- final var stateIds = stateService .getStateIds (activityId , agentObj , registration );
95+ final var stateIds = stateService .getStateIds (activityId , agent , registration );
10896
10997 return ResponseEntity .ok (stateIds );
11098 }
@@ -123,16 +111,15 @@ public ResponseEntity<List<String>> getStateIds(
123111 * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single
124112 * Document</a>
125113 */
126- @ PutMapping (params = {"activityId" , "agent" , "stateId" }, consumes = { "application/json" } )
114+ @ PutMapping (params = {"activityId" , "agent" , "stateId" })
127115 public ResponseEntity <Void > putState (@ RequestParam (required = true ) String activityId ,
128- @ RequestParam (required = true ) String agent ,
116+ @ Valid @ RequestParam (required = true ) Agent agent ,
129117 @ RequestParam (required = true ) String stateId ,
130- @ RequestParam (required = false ) UUID registration , @ RequestBody JsonNode stateDocument ) {
118+ @ RequestParam (required = false ) UUID registration , @ RequestBody String stateDocument ) {
131119
132120 log .debug ("PUT state" );
133121
134- final Agent agentObj = parseAgent (agent );
135- stateService .putState (activityId , agentObj , stateId , registration , stateDocument );
122+ stateService .putState (activityId , agent , stateId , registration , stateDocument );
136123
137124 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
138125 }
@@ -151,16 +138,15 @@ public ResponseEntity<Void> putState(@RequestParam(required = true) String activ
151138 * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single
152139 * Document</a>
153140 */
154- @ PostMapping (params = {"activityId" , "agent" , "stateId" }, consumes = { "application/json" } )
141+ @ PostMapping (params = {"activityId" , "agent" , "stateId" })
155142 public ResponseEntity <Void > postState (@ RequestParam (required = true ) String activityId ,
156- @ RequestParam (required = true ) String agent ,
143+ @ Valid @ RequestParam (required = true ) Agent agent ,
157144 @ RequestParam (required = true ) String stateId ,
158- @ RequestParam (required = false ) UUID registration , @ RequestBody JsonNode stateDocument ) {
145+ @ RequestParam (required = false ) UUID registration , @ RequestBody String stateDocument ) {
159146
160147 log .debug ("POST state" );
161148
162- final Agent agentObj = parseAgent (agent );
163- stateService .postState (activityId , agentObj , stateId , registration , stateDocument );
149+ stateService .postState (activityId , agent , stateId , registration , stateDocument );
164150
165151 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
166152 }
@@ -180,14 +166,13 @@ public ResponseEntity<Void> postState(@RequestParam(required = true) String acti
180166 */
181167 @ DeleteMapping (params = {"activityId" , "agent" , "stateId" })
182168 public ResponseEntity <Void > deleteState (@ RequestParam (required = true ) String activityId ,
183- @ RequestParam (required = true ) String agent ,
169+ @ Valid @ RequestParam (required = true ) Agent agent ,
184170 @ RequestParam (required = true ) String stateId ,
185171 @ RequestParam (required = false ) UUID registration ) {
186172
187173 log .debug ("DELETE state" );
188174
189- final Agent agentObj = parseAgent (agent );
190- stateService .deleteState (activityId , agentObj , stateId , registration );
175+ stateService .deleteState (activityId , agent , stateId , registration );
191176
192177 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
193178 }
@@ -206,13 +191,12 @@ public ResponseEntity<Void> deleteState(@RequestParam(required = true) String ac
206191 */
207192 @ DeleteMapping (params = {"activityId" , "agent" , "!stateId" })
208193 public ResponseEntity <Void > deleteStates (@ RequestParam (required = true ) String activityId ,
209- @ RequestParam (required = true ) String agent ,
194+ @ Valid @ RequestParam (required = true ) Agent agent ,
210195 @ RequestParam (required = false ) UUID registration ) {
211196
212197 log .debug ("DELETE states" );
213198
214- final Agent agentObj = parseAgent (agent );
215- stateService .deleteStates (activityId , agentObj , registration );
199+ stateService .deleteStates (activityId , agent , registration );
216200
217201 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
218202 }
0 commit comments