Skip to content
This repository was archived by the owner on Oct 23, 2020. It is now read-only.

Commit 31aeebe

Browse files
committed
Merge pull request #5 from ljwolford/master
Activity State Update
2 parents 5b8fc01 + e80bc12 commit 31aeebe

File tree

12 files changed

+440
-205
lines changed

12 files changed

+440
-205
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ target/
1616
.classpath
1717
.project
1818
.settings/
19+
20+
# IntelliJ additions
21+
*.iml
22+
.idea/

src/main/java/gov/adlnet/xapi/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class App {
1010
private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/";
11-
private static final String USERNAME = "Walt Grata";
11+
private static final String USERNAME = "jXAPI";
1212
private static final String PASSWORD = "password";
1313
public static void main(String[] args) throws IOException {
1414
StatementClient client = new StatementClient(LRS_URI, USERNAME,
Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package gov.adlnet.xapi.client;
22

33
import gov.adlnet.xapi.model.Activity;
4+
import gov.adlnet.xapi.model.Agent;
45

56
import java.io.IOException;
67
import java.net.MalformedURLException;
78
import java.net.URL;
89

910
import com.google.gson.JsonArray;
1011
import com.google.gson.JsonElement;
12+
import com.google.gson.JsonObject;
1113

1214
public class ActivityClient extends BaseClient {
1315

@@ -27,27 +29,111 @@ public Activity getActivity(String activityId)
2729
String result = issueGet(path);
2830
return getDecoder().fromJson(result, Activity.class);
2931
}
30-
public JsonArray getActivities(String activityId, String since) throws MalformedURLException, IOException {
32+
33+
private String createProfilePath(String activityId, String profileId){
34+
StringBuilder sb = new StringBuilder();
35+
sb.append("/xAPI/activities/profile?activityId=");
36+
sb.append(activityId);
37+
sb.append("&profileId=");
38+
sb.append(profileId);
39+
return sb.toString();
40+
}
41+
42+
public JsonObject getActivityProfile(String activityId, String profileId)
43+
throws MalformedURLException, IOException{
44+
String result = issueGet(createProfilePath(activityId, profileId));
45+
return getDecoder().fromJson(result, JsonObject.class);
46+
}
47+
48+
public JsonArray getActivityProfiles(String activityId, String since) throws MalformedURLException, IOException {
3149
String path = "/xapi/activities/profile?activityId=" + activityId;
3250
if (since != null && since.length() > 0){
3351
path += ("&since=" + since);
3452
}
3553
String result = issueGet(path);
3654
return getDecoder().fromJson(result, JsonArray.class);
3755
}
38-
private String createProfilePath(String activityId, String profileId){
39-
StringBuilder sb = new StringBuilder();
40-
sb.append("/xAPI/activities/profile?activityId=");
41-
sb.append(activityId);
42-
sb.append("&profileId=");
43-
sb.append(profileId);
44-
return sb.toString();
45-
}
46-
public boolean publishActivityProfile(String activityId, String profileId,
47-
JsonElement obj) throws IOException {
48-
String json = getDecoder().toJson(obj);
56+
57+
public boolean postActivityProfile(String activityId, String profileId,
58+
JsonElement profile) throws IOException {
59+
String json = getDecoder().toJson(profile);
4960
String response = issuePost(createProfilePath(activityId, profileId),
5061
json);
51-
return response != null;
62+
return response.isEmpty();
5263
}
64+
65+
public boolean putActivityProfile(String activityId, String profileId,
66+
JsonElement profile) throws IOException {
67+
String json = getDecoder().toJson(profile);
68+
String response = issuePut(createProfilePath(activityId, profileId),
69+
json);
70+
return response.isEmpty();
71+
}
72+
73+
public boolean deleteActivityProfile(String activityId, String profileId) throws IOException {
74+
String response = issueDelete(createProfilePath(activityId, profileId));
75+
return response.isEmpty();
76+
}
77+
78+
private String createStatePath(String activityId, Agent agent, String registration, String stateId){
79+
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s&stateId=%s", activityId,
80+
getDecoder().toJson(agent.serialize()), stateId);
81+
if (registration != null && registration.length() > 0){
82+
path += ("&registration=" + registration);
83+
}
84+
return path;
85+
}
86+
87+
public JsonObject getActivityState(String activityId, Agent agent, String registration, String stateId)
88+
throws MalformedURLException, IOException{
89+
String result = issueGet(createStatePath(activityId, agent, registration, stateId));
90+
return getDecoder().fromJson(result, JsonObject.class);
91+
}
92+
93+
public boolean postActivityState(String activityId, Agent agent, String registration, String stateId,
94+
JsonElement state)
95+
throws MalformedURLException, IOException{
96+
String json = getDecoder().toJson((state));
97+
String result = issuePost(createStatePath(activityId, agent, registration, stateId), json);
98+
return result.isEmpty();
99+
}
100+
101+
public boolean putActivityState(String activityId, Agent agent, String registration, String stateId,
102+
JsonElement state)
103+
throws MalformedURLException, IOException{
104+
String json = getDecoder().toJson((state));
105+
String result = issuePut(createStatePath(activityId, agent, registration, stateId), json);
106+
return result.isEmpty();
107+
}
108+
109+
public boolean deleteActivityState(String activityId, Agent agent, String registration, String stateId)
110+
throws MalformedURLException, IOException{
111+
String result = issueDelete(createStatePath(activityId, agent, registration, stateId));
112+
return result.isEmpty();
113+
}
114+
115+
public JsonArray getActivityStates(String activityId, Agent agent, String registration, String since)
116+
throws MalformedURLException, IOException{
117+
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId,
118+
getDecoder().toJson(agent.serialize()));
119+
if (registration != null && registration.length() > 0){
120+
path += ("&registration=" + registration);
121+
}
122+
if (since != null && since.length() > 0){
123+
path += ("&since=" + since);
124+
}
125+
String result = issueGet(path);
126+
return getDecoder().fromJson(result, JsonArray.class);
127+
}
128+
129+
public boolean deleteActivityStates(String activityId, Agent agent, String registration)
130+
throws MalformedURLException, IOException{
131+
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId,
132+
getDecoder().toJson(agent.serialize()));
133+
if (registration != null && registration.length() > 0){
134+
path += ("&registration=" + registration);
135+
}
136+
String result = issueDelete(path);
137+
return result.isEmpty();
138+
}
53139
}
Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package gov.adlnet.xapi.client;
22

33
import gov.adlnet.xapi.model.Agent;
4+
import gov.adlnet.xapi.model.Person;
45

56
import java.io.IOException;
67
import java.net.MalformedURLException;
78
import java.net.URL;
89

10+
import com.google.gson.JsonArray;
911
import com.google.gson.JsonElement;
1012

1113
public class AgentClient extends BaseClient {
@@ -20,25 +22,59 @@ public AgentClient(URL uri, String username, String password)
2022
super(uri, username, password);
2123
}
2224

23-
private String formatProfilePath(Agent a, String profileId) {
25+
public Person getPerson(Agent a)
26+
throws MalformedURLException, IOException {
27+
String path = "/xapi/agents?agent=" + getDecoder().toJson(a.serialize());
28+
String result = issueGet(path);
29+
return getDecoder().fromJson(result, Person.class);
30+
}
31+
32+
private String formatProfilePath(Agent a, String profileId) {
2433
String agentJson = getDecoder().toJson(a.serialize());
2534
String path = "/xAPI/agents/profile?agent=" + agentJson + "&profileId="
2635
+ profileId;
2736
return path;
2837
}
2938

30-
public boolean publishAgentProfile(Agent a, String profileId,
31-
JsonElement profile) throws IOException {
32-
String requestBody = getDecoder().toJson(profile);
33-
String path = formatProfilePath(a, profileId);
34-
String result = issuePost(path, requestBody);
35-
return result != null;
36-
}
37-
3839
public JsonElement getAgentProfile(Agent a, String profileId)
3940
throws MalformedURLException, IOException {
4041
String path = formatProfilePath(a, profileId);
4142
String result = issueGet(path);
4243
return getDecoder().fromJson(result, JsonElement.class);
4344
}
45+
46+
public boolean putAgentProfile(Agent a, String profileId, JsonElement profile)
47+
throws MalformedURLException, IOException {
48+
String path = formatProfilePath(a, profileId);
49+
String json = getDecoder().toJson((profile));
50+
String result = issuePut(path, json);
51+
return result.isEmpty();
52+
}
53+
54+
public boolean postAgentProfile(Agent a, String profileId, JsonElement profile)
55+
throws MalformedURLException, IOException {
56+
String path = formatProfilePath(a, profileId);
57+
String json = getDecoder().toJson((profile));
58+
String result = issuePost(path, json);
59+
return result.isEmpty();
60+
}
61+
62+
public boolean deleteAgentProfile(Agent a, String profileId)
63+
throws MalformedURLException, IOException {
64+
String path = formatProfilePath(a, profileId);
65+
String result = issueDelete(path);
66+
return result.isEmpty();
67+
}
68+
69+
public JsonArray getAgentProfiles(Agent a, String since)
70+
throws MalformedURLException, IOException {
71+
String agentJson = getDecoder().toJson(a.serialize());
72+
String path = "/xAPI/agents/profile?agent=" + agentJson;
73+
if (since != null && since.length() > 0){
74+
path += ("&since=" + since);
75+
}
76+
String result = issueGet(path);
77+
return getDecoder().fromJson(result, JsonArray.class);
78+
}
79+
4480
}

src/main/java/gov/adlnet/xapi/client/BaseClient.java

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
import java.io.InputStream;
1212
import java.io.InputStreamReader;
1313
import java.io.OutputStreamWriter;
14-
import java.net.Authenticator;
1514
import java.net.HttpURLConnection;
1615
import java.net.MalformedURLException;
17-
import java.net.PasswordAuthentication;
1816
import java.net.URL;
1917

2018
import javax.xml.bind.DatatypeConverter;
@@ -52,8 +50,8 @@ protected void init(URL uri, String user, String password) {
5250
this._host = uri;
5351
this.username = user;
5452
this.password = password;
55-
this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes());
56-
}
53+
this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes());
54+
}
5755

5856
protected String readFromConnection(HttpURLConnection conn)
5957
throws java.io.IOException {
@@ -123,6 +121,67 @@ protected String issuePost(String path, String data)
123121
}
124122
}
125123

124+
protected String issuePut(String path, String data)
125+
throws java.io.IOException {
126+
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);
127+
HttpURLConnection conn = initializePOSTConnection(url);
128+
conn.setRequestMethod("PUT");
129+
OutputStreamWriter writer = new OutputStreamWriter(
130+
conn.getOutputStream());
131+
try {
132+
writer.write(data);
133+
} catch (IOException ex) {
134+
InputStream s = conn.getErrorStream();
135+
InputStreamReader isr = new InputStreamReader(s);
136+
BufferedReader br = new BufferedReader(isr);
137+
try {
138+
String line = "";
139+
while((line = br.readLine()) != null){
140+
System.out.print(line);
141+
}
142+
System.out.println();
143+
} finally {
144+
s.close();
145+
}
146+
throw ex;
147+
} finally {
148+
writer.close();
149+
}
150+
try {
151+
return readFromConnection(conn);
152+
} finally {
153+
conn.disconnect();
154+
}
155+
}
156+
157+
protected String issueDelete(String path)
158+
throws java.io.IOException {
159+
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);
160+
HttpURLConnection conn = initializeConnection(url);
161+
conn.setRequestMethod("DELETE");
162+
try{
163+
return readFromConnection(conn);
164+
}
165+
catch (IOException ex){
166+
InputStream s = conn.getErrorStream();
167+
InputStreamReader isr = new InputStreamReader(s);
168+
BufferedReader br = new BufferedReader(isr);
169+
try {
170+
String line = "";
171+
while((line = br.readLine()) != null){
172+
System.out.print(line);
173+
}
174+
System.out.println();
175+
} finally {
176+
s.close();
177+
}
178+
throw ex;
179+
}
180+
finally{
181+
conn.disconnect();
182+
}
183+
}
184+
126185
protected String issueGet(String path) throws java.io.IOException,
127186
java.net.MalformedURLException {
128187
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);

0 commit comments

Comments
 (0)