Skip to content

Commit d76dcf8

Browse files
committed
Merge pull request #34 from brianjmiller/pr33
RemoteLRS Improvements
2 parents 2a282cd + ddb3bf8 commit d76dcf8

File tree

4 files changed

+334
-62
lines changed

4 files changed

+334
-62
lines changed

src/main/java/com/rusticisoftware/tincan/LRS.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ public interface LRS {
3737
ProfileKeysLRSResponse retrieveStateIds(Activity activity, Agent agent, UUID registration);
3838
StateLRSResponse retrieveState(String id, Activity activity, Agent agent, UUID registration);
3939
LRSResponse saveState(StateDocument state);
40+
LRSResponse updateState(StateDocument state);
4041
LRSResponse deleteState(StateDocument state);
4142
LRSResponse clearState(Activity activity, Agent agent, UUID registration);
4243

4344
ProfileKeysLRSResponse retrieveActivityProfileIds(Activity activity);
4445
ActivityProfileLRSResponse retrieveActivityProfile(String id, Activity activity);
4546
LRSResponse saveActivityProfile(ActivityProfileDocument profile);
47+
LRSResponse updateActivityProfile(ActivityProfileDocument profile);
4648
LRSResponse deleteActivityProfile(ActivityProfileDocument profile);
4749

4850
ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent);
4951
AgentProfileLRSResponse retrieveAgentProfile(String id, Agent agent);
5052
LRSResponse saveAgentProfile(AgentProfileDocument profile);
53+
LRSResponse updateAgentProfile(AgentProfileDocument profile);
5154
LRSResponse deleteAgentProfile(AgentProfileDocument profile);
5255
}

src/main/java/com/rusticisoftware/tincan/RemoteLRS.java

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public String calculateBasicAuth() {
134134
private HTTPResponse makeSyncRequest(HTTPRequest req) {
135135
String url;
136136

137-
if (req.getResource().toLowerCase().startsWith("http")){
137+
if (req.getResource().toLowerCase().startsWith("http")) {
138138
url = req.getResource();
139139
}
140140
else {
@@ -191,8 +191,7 @@ protected void onResponseComplete() throws IOException {
191191
webReq.setMethod(req.getMethod());
192192

193193
webReq.addRequestHeader("X-Experience-API-Version", this.version.toString());
194-
if (this.auth != null)
195-
{
194+
if (this.auth != null) {
196195
webReq.addRequestHeader("Authorization", this.auth);
197196
}
198197
if (req.getHeaders() != null) {
@@ -269,7 +268,7 @@ private LRSResponse getDocument(String resource, Map<String, String> queryParams
269268

270269
LRSResponse lrsResponse = new LRSResponse(request, response);
271270

272-
if(response.getStatus() == 200) {
271+
if (response.getStatus() == 200) {
273272
document.setContent(response.getContentBytes());
274273
document.setContentType(response.getContentType());
275274
document.setTimestamp(response.getLastModified());
@@ -287,7 +286,6 @@ else if (response.getStatus() == 404) {
287286
}
288287

289288
private LRSResponse deleteDocument(String resource, Map<String, String> queryParams) {
290-
291289
HTTPRequest request = new HTTPRequest();
292290

293291
request.setMethod(HttpMethods.DELETE);
@@ -298,7 +296,7 @@ private LRSResponse deleteDocument(String resource, Map<String, String> queryPar
298296

299297
LRSResponse lrsResponse = new LRSResponse(request, response);
300298

301-
if(response.getStatus() == 204) {
299+
if (response.getStatus() == 204) {
302300
lrsResponse.setSuccess(true);
303301
}
304302
else {
@@ -315,7 +313,7 @@ private LRSResponse saveDocument(String resource, Map<String, String> queryParam
315313
request.setQueryParams(queryParams);
316314
request.setContentType(document.getContentType());
317315
request.setContent(document.getContent());
318-
if(document.getEtag() != null) {
316+
if (document.getEtag() != null) {
319317
request.setHeaders(new HashMap<String, String>());
320318
request.getHeaders().put("If-Match", document.getEtag());
321319
}
@@ -324,7 +322,33 @@ private LRSResponse saveDocument(String resource, Map<String, String> queryParam
324322

325323
LRSResponse lrsResponse = new LRSResponse(request, response);
326324

327-
if(response.getStatus() == 204) {
325+
if (response.getStatus() == 204) {
326+
lrsResponse.setSuccess(true);
327+
}
328+
else {
329+
lrsResponse.setSuccess(false);
330+
}
331+
332+
return lrsResponse;
333+
}
334+
335+
private LRSResponse updateDocument(String resource, Map<String, String> queryParams, Document document) {
336+
HTTPRequest request = new HTTPRequest();
337+
request.setMethod(HttpMethods.POST);
338+
request.setResource(resource);
339+
request.setQueryParams(queryParams);
340+
request.setContentType(document.getContentType());
341+
request.setContent(document.getContent());
342+
if (document.getEtag() != null) {
343+
request.setHeaders(new HashMap<String, String>());
344+
request.getHeaders().put("If-Match", document.getEtag());
345+
}
346+
347+
HTTPResponse response = makeSyncRequest(request);
348+
349+
LRSResponse lrsResponse = new LRSResponse(request, response);
350+
351+
if (response.getStatus() == 204) {
328352
lrsResponse.setSuccess(true);
329353
}
330354
else {
@@ -344,7 +368,7 @@ private ProfileKeysLRSResponse getProfileKeys(String resource, HashMap<String, S
344368

345369
ProfileKeysLRSResponse lrsResponse = new ProfileKeysLRSResponse(request, response);
346370

347-
if(response.getStatus() == 200){
371+
if (response.getStatus() == 200) {
348372
lrsResponse.setSuccess(true);
349373
try {
350374
Iterator it = Mapper.getInstance().readValue(response.getContent(), ArrayNode.class).elements();
@@ -505,18 +529,18 @@ public StatementLRSResponse retrieveVoidedStatement(String id) {
505529

506530
@Override
507531
public StatementsResultLRSResponse queryStatements(StatementsQueryInterface query) {
508-
//Setup empty query object if null was passed in
509-
if (query == null) {
510-
query = (this.getVersion() == TCAPIVersion.V095) ?
511-
new com.rusticisoftware.tincan.v095.StatementsQuery() :
512-
new StatementsQuery();
513-
}
514-
515-
//Choke if the query parameters don't match the LRS version
532+
// Setup empty query object if null was passed in
533+
if (query == null) {
534+
query = (this.getVersion() == TCAPIVersion.V095) ?
535+
new com.rusticisoftware.tincan.v095.StatementsQuery() :
536+
new StatementsQuery();
537+
}
538+
539+
// Choke if the query parameters don't match the LRS version
516540
if (this.getVersion() != query.getVersion()) {
517541
throw new IncompatibleTCAPIVersion(
518-
"Attempted to issue " + this.getVersion() + " query using a " +
519-
query.getVersion() + " set of query parameters.");
542+
"Attempted to issue " + this.getVersion() + " query using a " +
543+
query.getVersion() + " set of query parameters.");
520544
}
521545

522546
StatementsResultLRSResponse lrsResponse = new StatementsResultLRSResponse();
@@ -560,7 +584,7 @@ public StatementsResultLRSResponse moreStatements(String moreURL) {
560584

561585
// moreURL is relative to the endpoint's server root
562586
URL endpoint = this.getEndpoint();
563-
String url = endpoint.getProtocol() + "://" + endpoint.getHost() + (endpoint.getPort() == -1 ? "" : endpoint.getPort()) + moreURL;
587+
String url = endpoint.getProtocol() + "://" + endpoint.getHost() + (endpoint.getPort() == -1 ? "" : ":" + endpoint.getPort()) + moreURL;
564588

565589
HTTPRequest request = new HTTPRequest();
566590
request.setResource(url);
@@ -590,7 +614,7 @@ public ProfileKeysLRSResponse retrieveStateIds(Activity activity, Agent agent, U
590614
HashMap<String, String> queryParams = new HashMap<String, String>();
591615
queryParams.put("activityId", activity.getId().toString());
592616
queryParams.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
593-
if(registration != null) {
617+
if (registration != null) {
594618
queryParams.put("registration", registration.toString());
595619
}
596620

@@ -632,6 +656,17 @@ public LRSResponse saveState(StateDocument state) {
632656
return saveDocument("activities/state", queryParams, state);
633657
}
634658

659+
@Override
660+
public LRSResponse updateState(StateDocument state) {
661+
HashMap<String,String> queryParams = new HashMap<String,String>();
662+
663+
queryParams.put("stateId", state.getId());
664+
queryParams.put("activityId", state.getActivity().getId().toString());
665+
queryParams.put("agent", state.getAgent().toJSON(this.getVersion(), this.usePrettyJSON()));
666+
667+
return updateDocument("activities/state", queryParams, state);
668+
}
669+
635670
@Override
636671
public LRSResponse deleteState(StateDocument state) {
637672
Map queryParams = new HashMap<String, String>();
@@ -640,7 +675,7 @@ public LRSResponse deleteState(StateDocument state) {
640675
queryParams.put("activityId", state.getActivity().getId().toString());
641676
queryParams.put("agent", state.getAgent().toJSON());
642677

643-
if(state.getRegistration() != null ) {
678+
if (state.getRegistration() != null ) {
644679
queryParams.put("registration", state.getRegistration().toString());
645680
}
646681

@@ -653,7 +688,7 @@ public LRSResponse clearState(Activity activity, Agent agent, UUID registration)
653688

654689
queryParams.put("activityId", activity.getId().toString());
655690
queryParams.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
656-
if(registration != null) {
691+
if (registration != null) {
657692
queryParams.put("registration", registration.toString());
658693
}
659694
return deleteDocument("activities/state", queryParams);
@@ -683,7 +718,7 @@ public ActivityProfileLRSResponse retrieveActivityProfile(String id, Activity ac
683718
ActivityProfileLRSResponse lrsResponse = new ActivityProfileLRSResponse(lrsResp.getRequest(), lrsResp.getResponse());
684719
lrsResponse.setSuccess(lrsResp.getSuccess());
685720

686-
if(lrsResponse.getResponse().getStatus() == 200) {
721+
if (lrsResponse.getResponse().getStatus() == 200) {
687722
lrsResponse.setContent(profileDocument);
688723
}
689724

@@ -699,6 +734,15 @@ public LRSResponse saveActivityProfile(ActivityProfileDocument profile) {
699734
return saveDocument("activities/profile", queryParams, profile);
700735
}
701736

737+
@Override
738+
public LRSResponse updateActivityProfile(ActivityProfileDocument profile) {
739+
HashMap<String, String> queryParams = new HashMap<String, String>();
740+
queryParams.put("profileId", profile.getId());
741+
queryParams.put("activityId", profile.getActivity().getId().toString());
742+
743+
return updateDocument("activities/profile", queryParams, profile);
744+
}
745+
702746
@Override
703747
public LRSResponse deleteActivityProfile(ActivityProfileDocument profile) {
704748
HashMap<String, String> queryParams = new HashMap<String, String>();
@@ -732,7 +776,7 @@ public AgentProfileLRSResponse retrieveAgentProfile(String id, Agent agent) {
732776
AgentProfileLRSResponse lrsResponse = new AgentProfileLRSResponse(lrsResp.getRequest(), lrsResp.getResponse());
733777
lrsResponse.setSuccess(lrsResp.getSuccess());
734778

735-
if(lrsResponse.getResponse().getStatus() == 200) {
779+
if (lrsResponse.getResponse().getStatus() == 200) {
736780
lrsResponse.setContent(profileDocument);
737781
}
738782

@@ -745,7 +789,17 @@ public LRSResponse saveAgentProfile(AgentProfileDocument profile) {
745789
queryParams.put("profileId", profile.getId());
746790
queryParams.put("agent", profile.getAgent().toJSON(this.getVersion(), this.usePrettyJSON()));
747791

748-
return saveDocument("agents/profile", queryParams, profile); }
792+
return saveDocument("agents/profile", queryParams, profile);
793+
}
794+
795+
@Override
796+
public LRSResponse updateAgentProfile(AgentProfileDocument profile) {
797+
HashMap<String, String> queryParams = new HashMap<String, String>();
798+
queryParams.put("profileId", profile.getId());
799+
queryParams.put("agent", profile.getAgent().toJSON(this.getVersion(), this.usePrettyJSON()));
800+
801+
return updateDocument("agents/profile", queryParams, profile);
802+
}
749803

750804
@Override
751805
public LRSResponse deleteAgentProfile(AgentProfileDocument profile) {
@@ -754,5 +808,6 @@ public LRSResponse deleteAgentProfile(AgentProfileDocument profile) {
754808
queryParams.put("agent", profile.getAgent().toJSON(this.getVersion(), this.usePrettyJSON()));
755809
// TODO: need to pass Etag?
756810

757-
return deleteDocument("agents/profile", queryParams); }
811+
return deleteDocument("agents/profile", queryParams);
812+
}
758813
}

0 commit comments

Comments
 (0)