Skip to content

Commit 5f45902

Browse files
Store and retrieve content type with state documents
Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com>
1 parent fc96e44 commit 5f45902

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateController.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.UUID;
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
13+
import org.springframework.http.HttpHeaders;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.MediaType;
1516
import org.springframework.http.ResponseEntity;
@@ -19,6 +20,7 @@
1920
import org.springframework.web.bind.annotation.PostMapping;
2021
import org.springframework.web.bind.annotation.PutMapping;
2122
import org.springframework.web.bind.annotation.RequestBody;
23+
import org.springframework.web.bind.annotation.RequestHeader;
2224
import org.springframework.web.bind.annotation.RequestMapping;
2325
import org.springframework.web.bind.annotation.RequestParam;
2426
import org.springframework.web.bind.annotation.RestController;
@@ -66,9 +68,14 @@ public ResponseEntity<String> getState(@RequestParam(required = true) String act
6668

6769
log.debug("GET state");
6870

69-
final var state = stateService.getState(activityId, agent, stateId, registration);
71+
final var stateEntity = stateService.getState(activityId, agent, stateId, registration);
7072

71-
return state.map(s -> ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(s))
73+
return stateEntity
74+
.map(entity -> ResponseEntity.ok()
75+
.contentType(MediaType.parseMediaType(
76+
entity.getContentType() != null ? entity.getContentType()
77+
: MediaType.APPLICATION_JSON_VALUE))
78+
.body(entity.getStateDocument()))
7279
.orElseGet(() -> ResponseEntity.notFound().build());
7380
}
7481

@@ -115,11 +122,14 @@ public ResponseEntity<List<String>> getStateIds(
115122
public ResponseEntity<Void> putState(@RequestParam(required = true) String activityId,
116123
@Valid @RequestParam(required = true) Agent agent,
117124
@RequestParam(required = true) String stateId,
118-
@RequestParam(required = false) UUID registration, @RequestBody String stateDocument) {
125+
@RequestParam(required = false) UUID registration,
126+
@RequestHeader(value = HttpHeaders.CONTENT_TYPE,
127+
defaultValue = MediaType.APPLICATION_JSON_VALUE) String contentType,
128+
@RequestBody String stateDocument) {
119129

120130
log.debug("PUT state");
121131

122-
stateService.putState(activityId, agent, stateId, registration, stateDocument);
132+
stateService.putState(activityId, agent, stateId, registration, stateDocument, contentType);
123133

124134
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
125135
}
@@ -142,11 +152,14 @@ public ResponseEntity<Void> putState(@RequestParam(required = true) String activ
142152
public ResponseEntity<Void> postState(@RequestParam(required = true) String activityId,
143153
@Valid @RequestParam(required = true) Agent agent,
144154
@RequestParam(required = true) String stateId,
145-
@RequestParam(required = false) UUID registration, @RequestBody String stateDocument) {
155+
@RequestParam(required = false) UUID registration,
156+
@RequestHeader(value = HttpHeaders.CONTENT_TYPE,
157+
defaultValue = MediaType.APPLICATION_JSON_VALUE) String contentType,
158+
@RequestBody String stateDocument) {
146159

147160
log.debug("POST state");
148161

149-
stateService.postState(activityId, agent, stateId, registration, stateDocument);
162+
stateService.postState(activityId, agent, stateId, registration, stateDocument, contentType);
150163

151164
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
152165
}

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateEntity.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class StateEntity {
3838
@Column(columnDefinition = "CLOB")
3939
private String stateDocument;
4040

41+
private String contentType;
42+
4143
/**
4244
* StateEntity Constructor.
4345
*/
@@ -47,12 +49,13 @@ public StateEntity() {}
4749
* StateEntity Constructor.
4850
*/
4951
public StateEntity(String activityId, String agentJson, String stateId, UUID registration,
50-
String stateDocument) {
52+
String stateDocument, String contentType) {
5153
this.activityId = activityId;
5254
this.agentJson = agentJson;
5355
this.stateId = stateId;
5456
this.registration = registration;
5557
this.stateDocument = stateDocument;
58+
this.contentType = contentType;
5659
}
5760

5861
/**
@@ -145,6 +148,24 @@ public void setStateDocument(String stateDocument) {
145148
this.stateDocument = stateDocument;
146149
}
147150

151+
/**
152+
* Gets the contentType.
153+
*
154+
* @return the contentType
155+
*/
156+
public String getContentType() {
157+
return contentType;
158+
}
159+
160+
/**
161+
* Sets the contentType.
162+
*
163+
* @param contentType the contentType to set
164+
*/
165+
public void setContentType(String contentType) {
166+
this.contentType = contentType;
167+
}
168+
148169
/**
149170
* Composite primary key for StateEntity.
150171
*/

samples/xapi-server/src/main/java/dev/learning/xapi/samples/xapiserver/StateService.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public StateService(StateRepository repository, ObjectMapper mapper) {
4545
* @param agent the agent
4646
* @param stateId the stateId
4747
* @param registration the registration (nullable)
48-
* @return the state document or Optional.empty() if not found
48+
* @return the state entity or Optional.empty() if not found
4949
*/
50-
public Optional<String> getState(String activityId, Agent agent, String stateId,
50+
public Optional<StateEntity> getState(String activityId, Agent agent, String stateId,
5151
UUID registration) {
5252

5353
log.info("get state: activityId={}, agent={}, stateId={}, registration={}", activityId, agent,
@@ -56,7 +56,7 @@ public Optional<String> getState(String activityId, Agent agent, String stateId,
5656
final String agentJson = serializeAgent(agent);
5757
final StateEntity.StateId id = new StateEntity.StateId(activityId, agentJson, stateId);
5858

59-
return repository.findById(id).map(StateEntity::getStateDocument);
59+
return repository.findById(id);
6060
}
6161

6262
/**
@@ -85,16 +85,17 @@ public List<String> getStateIds(String activityId, Agent agent, UUID registratio
8585
* @param stateId the stateId
8686
* @param registration the registration (nullable)
8787
* @param stateDocument the state document
88+
* @param contentType the content type
8889
*/
8990
public void putState(String activityId, Agent agent, String stateId, UUID registration,
90-
String stateDocument) {
91+
String stateDocument, String contentType) {
9192

9293
log.info("put state: activityId={}, agent={}, stateId={}, registration={}", activityId, agent,
9394
stateId, registration);
9495

9596
final String agentJson = serializeAgent(agent);
9697
final StateEntity entity =
97-
new StateEntity(activityId, agentJson, stateId, registration, stateDocument);
98+
new StateEntity(activityId, agentJson, stateId, registration, stateDocument, contentType);
9899

99100
repository.save(entity);
100101
}
@@ -107,9 +108,10 @@ public void putState(String activityId, Agent agent, String stateId, UUID regist
107108
* @param stateId the stateId
108109
* @param registration the registration (nullable)
109110
* @param stateDocument the state document to merge
111+
* @param contentType the content type
110112
*/
111113
public void postState(String activityId, Agent agent, String stateId, UUID registration,
112-
String stateDocument) {
114+
String stateDocument, String contentType) {
113115

114116
log.info("post state: activityId={}, agent={}, stateId={}, registration={}", activityId, agent,
115117
stateId, registration);
@@ -120,14 +122,18 @@ public void postState(String activityId, Agent agent, String stateId, UUID regis
120122
final Optional<StateEntity> existingEntity = repository.findById(id);
121123

122124
String mergedDocument = stateDocument;
125+
String finalContentType = contentType;
123126
if (existingEntity.isPresent()) {
124127
// Merge the documents (for JSON objects, newer properties override)
125128
final String existing = existingEntity.get().getStateDocument();
126129
mergedDocument = mergeJsonDocuments(existing, stateDocument);
130+
// Keep the original content type if merging
131+
finalContentType = existingEntity.get().getContentType();
127132
}
128133

129134
final StateEntity entity =
130-
new StateEntity(activityId, agentJson, stateId, registration, mergedDocument);
135+
new StateEntity(activityId, agentJson, stateId, registration, mergedDocument,
136+
finalContentType);
131137

132138
repository.save(entity);
133139
}

0 commit comments

Comments
 (0)