Skip to content
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

Add xapi-server sample #79

Merged
merged 11 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<module>get-activity-profiles</module>
<!-- About Resource -->
<module>get-about</module>
<!-- Using xapi-model in server sider -->
<module>xapi-server</module>
</modules>
<build>
<plugins>
Expand Down
22 changes: 22 additions & 0 deletions samples/xapi-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.learning.xapi.samples</groupId>
<artifactId>xapi-samples-build</artifactId>
<version>1.0.8-SNAPSHOT</version>
thomasturrell marked this conversation as resolved.
Show resolved Hide resolved
</parent>
<artifactId>xapi-server</artifactId>
<name>xAPI Server Sample</name>
<description>Basic xAPI Statement Resource implementation</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>dev.learning.xapi</groupId>
<artifactId>xapi-model</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.samples.xapiserver;

import dev.learning.xapi.model.Statement;
import jakarta.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* StatementsController.
* Basic implementation of xAPI statements PUT and POST endpoint.
* </p>
*
* @see <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#20-resources">xAPI resources</a>
* @author István Rátkai (Selindek)
*/
@Validated
@RestController
@RequestMapping(value = "/xapi/statements")
public class StatementController {

Logger log = LoggerFactory.getLogger(StatementController.class);

@Autowired
private StatementService statementService;

/**
* <p>
* Put Statement.
* </p>
*
* @param statementId the {@link UUID} of the statement.
* @param statement the body of the statement.
* @return a {@link org.springframework.http.ResponseEntity} object.
* @see <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#211-put-statements">xAPI put statements endpoint</a>
*/
@PutMapping(params = { "statementId" }, consumes = { "application/json" })
public ResponseEntity<Void> putStatement(@RequestParam(required = true) UUID statementId,
@Valid @RequestBody Statement statement) {

log.debug("PUT statement: {} Statement ID: {}", statement, statementId);

statementService.processStatement(statementId, statement);

return new ResponseEntity<>(HttpStatus.OK);
}

/**
* <p>
* Post Statements.
* </p>
*
* @param statements an array of {@link io.launchlearning.xapi.model.Statement} objects.
* @return a {@link org.springframework.http.ResponseEntity} object.
* @see <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#212-post-statements">xAPI post statements endpoint</a>
*/
@PostMapping(consumes = { "application/json" })
public ResponseEntity<Collection<UUID>>
postStatements(@Valid @RequestBody List<Statement> statements) {

log.debug("POST statements");

statements.forEach(s -> statementService.processStatement(s.getId(), s));

return new ResponseEntity<>(HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.samples.xapiserver;

import dev.learning.xapi.model.Statement;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* StatementService.
*
* @author István Rátkai (Selindek)
*/
@Service
public class StatementService {

private Logger log = LoggerFactory.getLogger(StatementController.class);

/**
* Processes an incoming statement.
*
* @param statementId the id of the statement
* @param statement the Statement itself
*/
public void processStatement(UUID statementId, Statement statement) {

log.info("processing statement: {}", statement);

// add custom logic here...
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.samples.xapiserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* <p>
* Sample of creating an xAPI server using the xAPI model.
* </p>
*
* <p>
* The xapi-model package can be used on server side too.
* Most commercial xAPI LRS implementations provide some kind of statement-forwarding feature.
* Using this feature you can easily implement custom functionalities which can be triggered
* when an xAPI statement is sent to the LRS.
* Eg. creating unique reports, storing the statement or some of its properties in our own
* database, sending email or text notifications, etc.
* In certain cases you don't even need an LRS, you can redirect the statements directly to
* your custom xAPI server.
* Eventually you can even build a fully functional LRS too.
* </p>
*
* @author István Rátkai (Selindek)
*/
@SpringBootApplication
public class XapiServerApplication {

/**
* <p>
* main.
* </p>
*
* @param args an array of {@link java.lang.String} objects.
*/
public static void main(String[] args) {
SpringApplication.run(XapiServerApplication.class, args);
}


}