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

Commit

Permalink
Merge pull request #5 from ljwolford/master
Browse files Browse the repository at this point in the history
Activity State Update
  • Loading branch information
wegrata committed Dec 18, 2014
2 parents 5b8fc01 + e80bc12 commit 31aeebe
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 205 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ target/
.classpath
.project
.settings/

# IntelliJ additions
*.iml
.idea/
2 changes: 1 addition & 1 deletion src/main/java/gov/adlnet/xapi/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class App {
private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/";
private static final String USERNAME = "Walt Grata";
private static final String USERNAME = "jXAPI";
private static final String PASSWORD = "password";
public static void main(String[] args) throws IOException {
StatementClient client = new StatementClient(LRS_URI, USERNAME,
Expand Down
112 changes: 99 additions & 13 deletions src/main/java/gov/adlnet/xapi/client/ActivityClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gov.adlnet.xapi.client;

import gov.adlnet.xapi.model.Activity;
import gov.adlnet.xapi.model.Agent;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class ActivityClient extends BaseClient {

Expand All @@ -27,27 +29,111 @@ public Activity getActivity(String activityId)
String result = issueGet(path);
return getDecoder().fromJson(result, Activity.class);
}
public JsonArray getActivities(String activityId, String since) throws MalformedURLException, IOException {

private String createProfilePath(String activityId, String profileId){
StringBuilder sb = new StringBuilder();
sb.append("/xAPI/activities/profile?activityId=");
sb.append(activityId);
sb.append("&profileId=");
sb.append(profileId);
return sb.toString();
}

public JsonObject getActivityProfile(String activityId, String profileId)
throws MalformedURLException, IOException{
String result = issueGet(createProfilePath(activityId, profileId));
return getDecoder().fromJson(result, JsonObject.class);
}

public JsonArray getActivityProfiles(String activityId, String since) throws MalformedURLException, IOException {
String path = "/xapi/activities/profile?activityId=" + activityId;
if (since != null && since.length() > 0){
path += ("&since=" + since);
}
String result = issueGet(path);
return getDecoder().fromJson(result, JsonArray.class);
}
private String createProfilePath(String activityId, String profileId){
StringBuilder sb = new StringBuilder();
sb.append("/xAPI/activities/profile?activityId=");
sb.append(activityId);
sb.append("&profileId=");
sb.append(profileId);
return sb.toString();
}
public boolean publishActivityProfile(String activityId, String profileId,
JsonElement obj) throws IOException {
String json = getDecoder().toJson(obj);

public boolean postActivityProfile(String activityId, String profileId,
JsonElement profile) throws IOException {
String json = getDecoder().toJson(profile);
String response = issuePost(createProfilePath(activityId, profileId),
json);
return response != null;
return response.isEmpty();
}

public boolean putActivityProfile(String activityId, String profileId,
JsonElement profile) throws IOException {
String json = getDecoder().toJson(profile);
String response = issuePut(createProfilePath(activityId, profileId),
json);
return response.isEmpty();
}

public boolean deleteActivityProfile(String activityId, String profileId) throws IOException {
String response = issueDelete(createProfilePath(activityId, profileId));
return response.isEmpty();
}

private String createStatePath(String activityId, Agent agent, String registration, String stateId){
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s&stateId=%s", activityId,
getDecoder().toJson(agent.serialize()), stateId);
if (registration != null && registration.length() > 0){
path += ("&registration=" + registration);
}
return path;
}

public JsonObject getActivityState(String activityId, Agent agent, String registration, String stateId)
throws MalformedURLException, IOException{
String result = issueGet(createStatePath(activityId, agent, registration, stateId));
return getDecoder().fromJson(result, JsonObject.class);
}

public boolean postActivityState(String activityId, Agent agent, String registration, String stateId,
JsonElement state)
throws MalformedURLException, IOException{
String json = getDecoder().toJson((state));
String result = issuePost(createStatePath(activityId, agent, registration, stateId), json);
return result.isEmpty();
}

public boolean putActivityState(String activityId, Agent agent, String registration, String stateId,
JsonElement state)
throws MalformedURLException, IOException{
String json = getDecoder().toJson((state));
String result = issuePut(createStatePath(activityId, agent, registration, stateId), json);
return result.isEmpty();
}

public boolean deleteActivityState(String activityId, Agent agent, String registration, String stateId)
throws MalformedURLException, IOException{
String result = issueDelete(createStatePath(activityId, agent, registration, stateId));
return result.isEmpty();
}

public JsonArray getActivityStates(String activityId, Agent agent, String registration, String since)
throws MalformedURLException, IOException{
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId,
getDecoder().toJson(agent.serialize()));
if (registration != null && registration.length() > 0){
path += ("&registration=" + registration);
}
if (since != null && since.length() > 0){
path += ("&since=" + since);
}
String result = issueGet(path);
return getDecoder().fromJson(result, JsonArray.class);
}

public boolean deleteActivityStates(String activityId, Agent agent, String registration)
throws MalformedURLException, IOException{
String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId,
getDecoder().toJson(agent.serialize()));
if (registration != null && registration.length() > 0){
path += ("&registration=" + registration);
}
String result = issueDelete(path);
return result.isEmpty();
}
}
54 changes: 45 additions & 9 deletions src/main/java/gov/adlnet/xapi/client/AgentClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package gov.adlnet.xapi.client;

import gov.adlnet.xapi.model.Agent;
import gov.adlnet.xapi.model.Person;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

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

private String formatProfilePath(Agent a, String profileId) {
public Person getPerson(Agent a)
throws MalformedURLException, IOException {
String path = "/xapi/agents?agent=" + getDecoder().toJson(a.serialize());
String result = issueGet(path);
return getDecoder().fromJson(result, Person.class);
}

private String formatProfilePath(Agent a, String profileId) {
String agentJson = getDecoder().toJson(a.serialize());
String path = "/xAPI/agents/profile?agent=" + agentJson + "&profileId="
+ profileId;
return path;
}

public boolean publishAgentProfile(Agent a, String profileId,
JsonElement profile) throws IOException {
String requestBody = getDecoder().toJson(profile);
String path = formatProfilePath(a, profileId);
String result = issuePost(path, requestBody);
return result != null;
}

public JsonElement getAgentProfile(Agent a, String profileId)
throws MalformedURLException, IOException {
String path = formatProfilePath(a, profileId);
String result = issueGet(path);
return getDecoder().fromJson(result, JsonElement.class);
}

public boolean putAgentProfile(Agent a, String profileId, JsonElement profile)
throws MalformedURLException, IOException {
String path = formatProfilePath(a, profileId);
String json = getDecoder().toJson((profile));
String result = issuePut(path, json);
return result.isEmpty();
}

public boolean postAgentProfile(Agent a, String profileId, JsonElement profile)
throws MalformedURLException, IOException {
String path = formatProfilePath(a, profileId);
String json = getDecoder().toJson((profile));
String result = issuePost(path, json);
return result.isEmpty();
}

public boolean deleteAgentProfile(Agent a, String profileId)
throws MalformedURLException, IOException {
String path = formatProfilePath(a, profileId);
String result = issueDelete(path);
return result.isEmpty();
}

public JsonArray getAgentProfiles(Agent a, String since)
throws MalformedURLException, IOException {
String agentJson = getDecoder().toJson(a.serialize());
String path = "/xAPI/agents/profile?agent=" + agentJson;
if (since != null && since.length() > 0){
path += ("&since=" + since);
}
String result = issueGet(path);
return getDecoder().fromJson(result, JsonArray.class);
}

}
67 changes: 63 additions & 4 deletions src/main/java/gov/adlnet/xapi/client/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

import javax.xml.bind.DatatypeConverter;
Expand Down Expand Up @@ -52,8 +50,8 @@ protected void init(URL uri, String user, String password) {
this._host = uri;
this.username = user;
this.password = password;
this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes());
}
this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes());
}

protected String readFromConnection(HttpURLConnection conn)
throws java.io.IOException {
Expand Down Expand Up @@ -123,6 +121,67 @@ protected String issuePost(String path, String data)
}
}

protected String issuePut(String path, String data)
throws java.io.IOException {
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);
HttpURLConnection conn = initializePOSTConnection(url);
conn.setRequestMethod("PUT");
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());
try {
writer.write(data);
} catch (IOException ex) {
InputStream s = conn.getErrorStream();
InputStreamReader isr = new InputStreamReader(s);
BufferedReader br = new BufferedReader(isr);
try {
String line = "";
while((line = br.readLine()) != null){
System.out.print(line);
}
System.out.println();
} finally {
s.close();
}
throw ex;
} finally {
writer.close();
}
try {
return readFromConnection(conn);
} finally {
conn.disconnect();
}
}

protected String issueDelete(String path)
throws java.io.IOException {
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);
HttpURLConnection conn = initializeConnection(url);
conn.setRequestMethod("DELETE");
try{
return readFromConnection(conn);
}
catch (IOException ex){
InputStream s = conn.getErrorStream();
InputStreamReader isr = new InputStreamReader(s);
BufferedReader br = new BufferedReader(isr);
try {
String line = "";
while((line = br.readLine()) != null){
System.out.print(line);
}
System.out.println();
} finally {
s.close();
}
throw ex;
}
finally{
conn.disconnect();
}
}

protected String issueGet(String path) throws java.io.IOException,
java.net.MalformedURLException {
URL url = new URL(this._host.getProtocol(), this._host.getHost(), path);
Expand Down
Loading

0 comments on commit 31aeebe

Please sign in to comment.