summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/stileeducation/markr')
-rw-r--r--src/main/java/com/stileeducation/markr/config/WebConfig.java17
-rw-r--r--src/main/java/com/stileeducation/markr/controller/TestResultsController.java4
-rw-r--r--src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java43
3 files changed, 62 insertions, 2 deletions
diff --git a/src/main/java/com/stileeducation/markr/config/WebConfig.java b/src/main/java/com/stileeducation/markr/config/WebConfig.java
new file mode 100644
index 0000000..6d9c1a0
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/config/WebConfig.java
@@ -0,0 +1,17 @@
+package com.stileeducation.markr.config;
+
+import com.stileeducation.markr.converter.XmlMarkrMessageConverter;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.util.List;
+
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+ @Override
+ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
+ converters.add(new XmlMarkrMessageConverter());
+ }
+}
diff --git a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
index f5f8a44..5667027 100644
--- a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
+++ b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
@@ -20,8 +20,8 @@ public class TestResultsController {
this.testResultsService = testResultsService;
}
- // TODO - update to consume text/xml+markr, consider return value
- @PostMapping(value = IMPORT_ENDPOINT, consumes = "application/xml", produces = "application/json")
+ // TODO consider return value
+ @PostMapping(value = IMPORT_ENDPOINT, consumes = "text/xml+markr", produces = "application/json")
public ResponseEntity<Void> handleXmlRequest(@RequestBody MCQTestResultsDTO testResults) {
testResultsService.importTestResults(testResults);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
diff --git a/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java b/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java
new file mode 100644
index 0000000..04207a1
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java
@@ -0,0 +1,43 @@
+package com.stileeducation.markr.converter;
+
+import com.stileeducation.markr.dto.MCQTestResultsDTO;
+import jakarta.xml.bind.JAXBContext;
+import jakarta.xml.bind.Marshaller;
+import jakarta.xml.bind.Unmarshaller;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import java.util.Collections;
+import java.util.List;
+
+public class XmlMarkrMessageConverter extends AbstractXmlHttpMessageConverter<MCQTestResultsDTO> {
+
+ public static final MediaType MEDIA_TYPE = new MediaType("text", "xml+markr");
+
+ @Override
+ protected MCQTestResultsDTO readFromSource(Class<? extends MCQTestResultsDTO> clazz, HttpHeaders headers, Source source) throws Exception {
+ JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class);
+ Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+ return (MCQTestResultsDTO) unmarshaller.unmarshal(source);
+ }
+
+ @Override
+ protected void writeToResult(MCQTestResultsDTO testResultsDTO, HttpHeaders headers, Result result) throws Exception {
+ JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class);
+ Marshaller marshaller = jaxbContext.createMarshaller();
+ marshaller.marshal(testResultsDTO, result);
+ }
+
+ @Override
+ protected boolean supports(Class<?> clazz) {
+ return MCQTestResultsDTO.class.isAssignableFrom(clazz);
+ }
+
+ @Override
+ public List<MediaType> getSupportedMediaTypes() {
+ return Collections.singletonList(MEDIA_TYPE);
+ }
+}