-
Notifications
You must be signed in to change notification settings - Fork 4
Add state endpoints to xAPI sample server #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/add-state-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d66a50a
Initial plan
Copilot 9ce7a71
Add state endpoints to sample server with tests
Copilot 8a05dfa
Fix DELETE states endpoint and update README with examples
Copilot fc96e44
Use Agent object directly, support non-JSON documents, and use JpaRep…
Copilot 5f45902
Store and retrieve content type with state documents
Copilot c5669e7
Default registration parameter to zero UUID
Copilot c04eebf
Merge branch 'main' into copilot/add-state-endpoints
thomasturrell 1b63c4a
Add registration to composite key and use JpaRepository named methods
Copilot 6ced8aa
Only merge JSON documents in POST, replace non-JSON documents
Copilot 78ccbeb
Normalize agent JSON serialization for consistent composite keys
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/AgentConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2016-2025 Berry Cloud Ltd. All rights reserved. | ||
| */ | ||
|
|
||
| package dev.learning.xapi.samples.xapiserver; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import dev.learning.xapi.model.Agent; | ||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Converter for deserializing Agent from JSON string in request parameters. | ||
| * | ||
| * @author Thomas Turrell-Croft | ||
| */ | ||
| @Component | ||
| public class AgentConverter implements Converter<String, Agent> { | ||
|
|
||
| private final ObjectMapper objectMapper; | ||
|
|
||
| public AgentConverter(ObjectMapper objectMapper) { | ||
| this.objectMapper = objectMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public Agent convert(String source) { | ||
| try { | ||
| return objectMapper.readValue(source, Agent.class); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Invalid agent JSON: " + source, e); | ||
| } | ||
| } | ||
| } |
218 changes: 218 additions & 0 deletions
218
samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| /* | ||
| * Copyright 2016-2025 Berry Cloud Ltd. All rights reserved. | ||
| */ | ||
|
|
||
| package dev.learning.xapi.samples.xapiserver; | ||
|
|
||
| import dev.learning.xapi.model.Agent; | ||
| import jakarta.validation.Valid; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * Basic implementation of xAPI state GET, PUT, POST and DELETE resources. | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#23-state-resource">xAPI | ||
| * State Resource</a> | ||
| * | ||
| * @author Thomas Turrell-Croft | ||
| */ | ||
| @Validated | ||
| @RestController | ||
| @RequestMapping(value = "/xapi/activities/state") | ||
| public class StateController { | ||
|
|
||
| private static final UUID ZERO_UUID = new UUID(0L, 0L); | ||
|
|
||
| Logger log = LoggerFactory.getLogger(StateController.class); | ||
|
|
||
| private final StateService stateService; | ||
|
|
||
| public StateController(StateService stateService) { | ||
| this.stateService = stateService; | ||
| } | ||
|
|
||
| /** | ||
| * Get a single State document. | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param stateId the stateId | ||
| * @param registration the optional registration | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single | ||
| * Document</a> | ||
| */ | ||
| @GetMapping(params = {"activityId", "agent", "stateId"}) | ||
| public ResponseEntity<String> getState(@RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(required = true) String stateId, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration) { | ||
|
|
||
| log.debug("GET state"); | ||
|
|
||
| final var stateEntity = stateService.getState(activityId, agent, stateId, registration); | ||
|
|
||
| return stateEntity | ||
| .map(entity -> ResponseEntity.ok() | ||
| .contentType(MediaType.parseMediaType( | ||
| entity.getContentType() != null ? entity.getContentType() | ||
| : MediaType.APPLICATION_JSON_VALUE)) | ||
| .body(entity.getStateDocument())) | ||
| .orElseGet(() -> ResponseEntity.notFound().build()); | ||
| } | ||
|
|
||
| /** | ||
| * Get all stateIds for an activity and agent. | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param registration the optional registration | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#multiple-document-get">Multiple | ||
| * Document GET</a> | ||
| */ | ||
| @GetMapping(params = {"activityId", "agent", "!stateId"}) | ||
| public ResponseEntity<List<String>> getStateIds( | ||
| @RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration) { | ||
|
|
||
| log.debug("GET state ids"); | ||
|
|
||
| final var stateIds = stateService.getStateIds(activityId, agent, registration); | ||
|
|
||
| return ResponseEntity.ok(stateIds); | ||
| } | ||
|
|
||
| /** | ||
| * Put a State document. | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param stateId the stateId | ||
| * @param registration the optional registration | ||
| * @param stateDocument the state document | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single | ||
| * Document</a> | ||
| */ | ||
| @PutMapping(params = {"activityId", "agent", "stateId"}) | ||
| public ResponseEntity<Void> putState(@RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(required = true) String stateId, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration, | ||
| @RequestHeader(value = HttpHeaders.CONTENT_TYPE, | ||
| defaultValue = MediaType.APPLICATION_JSON_VALUE) String contentType, | ||
| @RequestBody String stateDocument) { | ||
|
|
||
| log.debug("PUT state"); | ||
|
|
||
| stateService.putState(activityId, agent, stateId, registration, stateDocument, contentType); | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
|
|
||
| /** | ||
| * Post a State document (merges with existing). | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param stateId the stateId | ||
| * @param registration the optional registration | ||
| * @param stateDocument the state document | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single | ||
| * Document</a> | ||
| */ | ||
| @PostMapping(params = {"activityId", "agent", "stateId"}) | ||
| public ResponseEntity<Void> postState(@RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(required = true) String stateId, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration, | ||
| @RequestHeader(value = HttpHeaders.CONTENT_TYPE, | ||
| defaultValue = MediaType.APPLICATION_JSON_VALUE) String contentType, | ||
| @RequestBody String stateDocument) { | ||
|
|
||
| log.debug("POST state"); | ||
|
|
||
| stateService.postState(activityId, agent, stateId, registration, stateDocument, contentType); | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
|
|
||
| /** | ||
| * Delete a single State document. | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param stateId the stateId | ||
| * @param registration the optional registration | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#single-document-put--post--get--delete">Single | ||
| * Document</a> | ||
| */ | ||
| @DeleteMapping(params = {"activityId", "agent", "stateId"}) | ||
| public ResponseEntity<Void> deleteState(@RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(required = true) String stateId, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration) { | ||
|
|
||
| log.debug("DELETE state"); | ||
|
|
||
| stateService.deleteState(activityId, agent, stateId, registration); | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
|
|
||
| /** | ||
| * Delete all State documents for an activity and agent. | ||
| * | ||
| * @param activityId the activityId | ||
| * @param agent the agent | ||
| * @param registration the optional registration | ||
| * @return the ResponseEntity | ||
| * | ||
| * @see <a href= | ||
| * "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#multiple-document-delete">Multiple | ||
| * Document DELETE</a> | ||
| */ | ||
| @DeleteMapping(params = {"activityId", "agent", "!stateId"}) | ||
| public ResponseEntity<Void> deleteStates(@RequestParam(required = true) String activityId, | ||
| @Valid @RequestParam(required = true) Agent agent, | ||
| @RequestParam(defaultValue = "00000000-0000-0000-0000-000000000000") UUID registration) { | ||
|
|
||
| log.debug("DELETE states"); | ||
|
|
||
| stateService.deleteStates(activityId, agent, registration); | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.