Skip to content

Commit 674a196

Browse files
committed
Merge pull request #51 from brianjmiller/pr37
Add methods populateWithAvailableLanguages, retrieveActivity, retrievePerson
2 parents 80316ee + fc43f15 commit 674a196

File tree

10 files changed

+350
-5
lines changed

10 files changed

+350
-5
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.rusticisoftware.tincan;
1717

18+
import java.io.IOException;
1819
import java.net.URI;
1920
import java.net.URISyntaxException;
2021

@@ -26,6 +27,7 @@
2627
import com.fasterxml.jackson.databind.node.ObjectNode;
2728
import com.rusticisoftware.tincan.json.JSONBase;
2829
import com.rusticisoftware.tincan.json.Mapper;
30+
import com.rusticisoftware.tincan.json.StringOfJSON;
2931

3032
/**
3133
* Activity model class
@@ -67,6 +69,10 @@ public Activity(String id, String name, String description) throws URISyntaxExce
6769
this.setDefinition(new ActivityDefinition(name, description));
6870
}
6971

72+
public Activity(StringOfJSON jsonStr) throws URISyntaxException, IOException {
73+
this(jsonStr.toJSONNode());
74+
}
75+
7076
@Override
7177
public ObjectNode toJSONNode(TCAPIVersion version) {
7278
ObjectNode node = Mapper.getInstance().createObjectNode();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ public interface LRS {
4141
LRSResponse deleteState(StateDocument state);
4242
LRSResponse clearState(Activity activity, Agent agent, UUID registration);
4343

44+
ActivityLRSResponse retrieveActivity(Activity activity);
4445
ProfileKeysLRSResponse retrieveActivityProfileIds(Activity activity);
4546
ActivityProfileLRSResponse retrieveActivityProfile(String id, Activity activity);
4647
LRSResponse saveActivityProfile(ActivityProfileDocument profile);
4748
LRSResponse updateActivityProfile(ActivityProfileDocument profile);
4849
LRSResponse deleteActivityProfile(ActivityProfileDocument profile);
4950

51+
PersonLRSResponse retrievePerson(Agent agent);
5052
ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent);
5153
AgentProfileLRSResponse retrieveAgentProfile(String id, Agent agent);
5254
LRSResponse saveAgentProfile(AgentProfileDocument profile);

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Iterator;
2323
import java.util.Map;
2424
import java.util.Map.Entry;
25+
import java.util.List;
26+
import java.util.ArrayList;
2527

2628
import com.rusticisoftware.tincan.json.JSONBase;
2729
import com.rusticisoftware.tincan.json.Mapper;
@@ -39,6 +41,7 @@ private class LanguageMapIterator implements Iterator<Map.Entry<String, String>>
3941
public LanguageMapIterator() {
4042
iterator = _map.entrySet().iterator();
4143
}
44+
4245
@Override
4346
public boolean hasNext() {
4447
return iterator.hasNext();
@@ -52,7 +55,8 @@ public Entry<String, String> next() {
5255
@Override
5356
public void remove() throws UnsupportedOperationException {
5457
throw new UnsupportedOperationException(
55-
"LanguageMap iterator does not implement the remove method");
58+
"LanguageMap iterator does not implement the remove method"
59+
);
5660
}
5761
}
5862
public LanguageMap(JsonNode jsonNode) {
@@ -79,7 +83,7 @@ public ObjectNode toJSONNode(TCAPIVersion version) {
7983
public String put(String key, String val) {
8084
return this._map.put(key, val);
8185
}
82-
86+
8387
public String put(Map.Entry<String, String> entry) {
8488
return this.put(entry.getKey(), entry.getValue());
8589
}
@@ -109,6 +113,14 @@ public Map.Entry<String, String> findFirstValue(String value) {
109113
return retVal;
110114
}
111115

116+
public void populateWithAvailableLanguages(List<String> list) {
117+
Iterator<Map.Entry<String, String>> it = this.iterator();
118+
while (it.hasNext()) {
119+
Map.Entry<String, String> n = it.next();
120+
list.add(n.getKey());
121+
}
122+
}
123+
112124
@Override
113125
public Iterator<Entry<String, String>> iterator() {
114126
return new LanguageMapIterator();
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright 2015 Rustici Software
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.rusticisoftware.tincan;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.node.ArrayNode;
21+
import com.fasterxml.jackson.databind.node.ObjectNode;
22+
import com.rusticisoftware.tincan.json.JSONBase;
23+
import com.rusticisoftware.tincan.json.Mapper;
24+
import com.rusticisoftware.tincan.json.StringOfJSON;
25+
import lombok.Data;
26+
import lombok.EqualsAndHashCode;
27+
import lombok.NoArgsConstructor;
28+
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
/**
34+
* Person Model class
35+
*/
36+
@Data
37+
@EqualsAndHashCode(callSuper = false)
38+
@NoArgsConstructor
39+
public class Person extends JSONBase {
40+
protected final String objectType = "Person";
41+
private List<String> name;
42+
private List<String> mbox;
43+
private List<String> mbox_sha1sum;
44+
private List<String> openid;
45+
private List<AgentAccount> account;
46+
47+
public Person(StringOfJSON jsonStr) throws IOException {
48+
this(jsonStr.toJSONNode());
49+
}
50+
51+
public Person(JsonNode jsonNode) {
52+
this();
53+
54+
JsonNode nameNode = jsonNode.path("name");
55+
if (! nameNode.isMissingNode()) {
56+
this.name = new ArrayList<String>();
57+
58+
for (JsonNode element : nameNode) {
59+
this.name.add(element.textValue());
60+
}
61+
}
62+
63+
JsonNode mboxNode = jsonNode.path("mbox");
64+
if (! mboxNode.isMissingNode()) {
65+
this.mbox = new ArrayList<String>();
66+
67+
for (JsonNode element : mboxNode) {
68+
this.mbox.add(element.textValue());
69+
}
70+
}
71+
72+
JsonNode mbox_sha1sumNode = jsonNode.path("mbox_sha1sum");
73+
if (! mbox_sha1sumNode.isMissingNode()) {
74+
this.mbox_sha1sum = new ArrayList<String>();
75+
76+
for (JsonNode element : mbox_sha1sumNode) {
77+
this.mbox_sha1sum.add(element.textValue());
78+
}
79+
}
80+
81+
JsonNode openidNode = jsonNode.path("openid");
82+
if (! openidNode.isMissingNode()) {
83+
this.openid = new ArrayList<String>();
84+
85+
for (JsonNode element : openidNode) {
86+
this.openid.add(element.textValue());
87+
}
88+
}
89+
90+
JsonNode accountNode = jsonNode.path("account");
91+
if (! accountNode.isMissingNode()) {
92+
this.account = new ArrayList<AgentAccount>();
93+
94+
for (JsonNode element : accountNode) {
95+
this.account.add(new AgentAccount(element));
96+
}
97+
}
98+
}
99+
100+
@Override
101+
public ObjectNode toJSONNode(TCAPIVersion version) {
102+
ObjectMapper mapper = Mapper.getInstance();
103+
ObjectNode node = mapper.createObjectNode();
104+
node.put("objectType", this.getObjectType());
105+
106+
if (this.name != null && this.name.size() > 0) {
107+
ArrayNode name = mapper.createArrayNode();
108+
node.put("name", name);
109+
110+
for (String element : this.getName()) {
111+
name.add(element);
112+
}
113+
}
114+
115+
if (this.mbox != null && this.mbox.size() > 0) {
116+
ArrayNode mbox = mapper.createArrayNode();
117+
node.put("mbox", mbox);
118+
119+
for (String element : this.getMbox()) {
120+
mbox.add(element);
121+
}
122+
}
123+
124+
if (this.mbox_sha1sum != null && this.mbox_sha1sum.size() > 0) {
125+
ArrayNode mbox_sha1sum = mapper.createArrayNode();
126+
node.put("mbox_sha1sum", mbox_sha1sum);
127+
128+
for (String element : this.getMbox_sha1sum()) {
129+
mbox_sha1sum.add(element);
130+
}
131+
}
132+
133+
if (this.openid != null && this.openid.size() > 0) {
134+
ArrayNode openid = mapper.createArrayNode();
135+
node.put("openid", openid);
136+
137+
for (String element : this.getOpenid()) {
138+
openid.add(element);
139+
}
140+
}
141+
142+
if (this.account != null && this.account.size() > 0) {
143+
ArrayNode account = mapper.createArrayNode();
144+
node.put("account", account);
145+
146+
for (AgentAccount element : this.getAccount()) {
147+
account.add(element.toJSONNode(version));
148+
}
149+
}
150+
151+
return node;
152+
}
153+
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.rusticisoftware.tincan.http.HTTPRequest;
2626
import com.rusticisoftware.tincan.http.HTTPResponse;
2727
import com.rusticisoftware.tincan.lrsresponses.*;
28+
2829
import lombok.Data;
2930
import lombok.NoArgsConstructor;
3031

@@ -42,6 +43,7 @@
4243
import org.eclipse.jetty.http.HttpMethods;
4344
import org.eclipse.jetty.io.Buffer;
4445
import org.eclipse.jetty.io.ByteArrayBuffer;
46+
4547
import com.rusticisoftware.tincan.exceptions.*;
4648
import com.rusticisoftware.tincan.json.Mapper;
4749
import com.rusticisoftware.tincan.json.StringOfJSON;
@@ -694,6 +696,35 @@ public LRSResponse clearState(Activity activity, Agent agent, UUID registration)
694696
return deleteDocument("activities/state", queryParams);
695697
}
696698

699+
@Override
700+
public ActivityLRSResponse retrieveActivity(Activity activity) {
701+
HTTPRequest request = new HTTPRequest();
702+
request.setMethod(HttpMethods.GET);
703+
request.setResource("activities");
704+
request.setQueryParams(new HashMap<String, String>());
705+
request.getQueryParams().put("activityId", activity.getId().toString());
706+
707+
HTTPResponse response = makeSyncRequest(request);
708+
int status = response.getStatus();
709+
710+
ActivityLRSResponse lrsResponse = new ActivityLRSResponse(request, response);
711+
712+
if (status == 200) {
713+
lrsResponse.setSuccess(true);
714+
try {
715+
lrsResponse.setContent(new Activity(new StringOfJSON(response.getContent())));
716+
} catch (Exception ex) {
717+
lrsResponse.setErrMsg("Exception: " + ex.toString());
718+
lrsResponse.setSuccess(false);
719+
}
720+
}
721+
else {
722+
lrsResponse.setSuccess(false);
723+
}
724+
725+
return lrsResponse;
726+
}
727+
697728
@Override
698729
public ProfileKeysLRSResponse retrieveActivityProfileIds(Activity activity) {
699730
HashMap<String, String> queryParams = new HashMap<String, String>();
@@ -753,6 +784,35 @@ public LRSResponse deleteActivityProfile(ActivityProfileDocument profile) {
753784
return deleteDocument("activities/profile", queryParams);
754785
}
755786

787+
@Override
788+
public PersonLRSResponse retrievePerson(Agent agent) {
789+
HTTPRequest request = new HTTPRequest();
790+
request.setMethod(HttpMethods.GET);
791+
request.setResource("agents");
792+
request.setQueryParams(new HashMap<String, String>());
793+
request.getQueryParams().put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
794+
795+
HTTPResponse response = makeSyncRequest(request);
796+
int status = response.getStatus();
797+
798+
PersonLRSResponse lrsResponse = new PersonLRSResponse(request, response);
799+
800+
if (status == 200) {
801+
lrsResponse.setSuccess(true);
802+
try {
803+
lrsResponse.setContent(new Person(new StringOfJSON(response.getContent())));
804+
} catch (Exception ex) {
805+
lrsResponse.setErrMsg("Exception: " + ex.toString());
806+
lrsResponse.setSuccess(false);
807+
}
808+
}
809+
else {
810+
lrsResponse.setSuccess(false);
811+
}
812+
813+
return lrsResponse;
814+
}
815+
756816
@Override
757817
public ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent) {
758818
HashMap<String, String> queryParams = new HashMap<String, String>();

src/main/java/com/rusticisoftware/tincan/http/HTTPResponse.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public String getContent() {
4646
}
4747

4848
public String getContentType() { return this.getHeader("Content-Type"); }
49-
public String getEtag() { return this.getHeader("ETag").toLowerCase(); }
49+
public String getEtag() {
50+
String etag = this.getHeader("ETag");
51+
if (etag == null) {
52+
return etag;
53+
}
54+
55+
return etag.toLowerCase();
56+
}
5057
public DateTime getLastModified() {
5158
DateTimeFormatter RFC1123_DATE_TIME_FORMATTER =
5259
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withZoneUTC();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2015 Rustici Software
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.rusticisoftware.tincan.lrsresponses;
17+
18+
import com.rusticisoftware.tincan.Activity;
19+
import com.rusticisoftware.tincan.http.HTTPRequest;
20+
import com.rusticisoftware.tincan.http.HTTPResponse;
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
import lombok.NoArgsConstructor;
24+
25+
@Data
26+
@EqualsAndHashCode(callSuper = false)
27+
@NoArgsConstructor
28+
public class ActivityLRSResponse extends LRSResponse{
29+
private Activity content;
30+
31+
public ActivityLRSResponse (HTTPRequest initRequest, HTTPResponse initResponse) {
32+
super(initRequest, initResponse);
33+
}
34+
}

0 commit comments

Comments
 (0)