示例#1
0
  @Override
  public State retrieveState(String id, String activityId, Agent agent, UUID registration)
      throws Exception {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("stateId", id);
    params.put("activityId", activityId);
    params.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
    if (registration != null) {
      params.put("registration", registration.toString());
    }

    String queryString = "?";
    Boolean first = true;
    for (Map.Entry<String, String> parameter : params.entrySet()) {
      queryString +=
          (first ? "" : "&")
              + URLEncoder.encode(parameter.getKey(), "UTF-8")
              + "="
              + URLEncoder.encode(parameter.getValue(), "UTF-8").replace("+", "%20");
      first = false;
    }

    HTTPRequest request = new HTTPRequest();
    request.setURL(this.getEndpoint() + "activities/state" + queryString);

    HTTPResponse response = this.sendRequest(request);
    int status = response.getStatus();

    if (status == 200) {
      return new State(id, response.getContentBytes(), activityId, agent, registration);
    } else if (status == 404) {
      return null;
    }
    throw new UnexpectedHTTPResponse(response);
  }
示例#2
0
  @Override
  public UUID saveStatement(Statement statement) throws Exception {
    HTTPRequest request = new HTTPRequest();
    request.setRequestContent(
        new ByteArrayBuffer(statement.toJSON(this.getVersion(), this.usePrettyJSON()), "UTF-8"));

    String url = this.getEndpoint() + "statements";
    if (statement.getId() == null) {
      request.setMethod(HttpMethods.POST);
    } else {
      request.setMethod(HttpMethods.PUT);
      url += "?statementId=" + statement.getId().toString();
    }
    request.setURL(url);

    HTTPResponse response = this.sendRequest(request);
    int status = response.getStatus();

    // TODO: handle 409 conflict, etc.
    if (status == 204) {
      return statement.getId();
    } else if (status == 200) {
      String content = request.getResponseContent();
      return UUID.fromString(
          Mapper.getInstance().readValue(content, ArrayNode.class).get(0).textValue());
    }

    throw new UnexpectedHTTPResponse(response);
  }
示例#3
0
  @Override
  public StatementsResultLRSResponse saveStatements(List<Statement> statements) {
    StatementsResultLRSResponse lrsResponse = new StatementsResultLRSResponse();
    if (statements.isEmpty()) {
      lrsResponse.setSuccess(true);
      return lrsResponse;
    }

    ArrayNode rootNode = Mapper.getInstance().createArrayNode();
    for (Statement statement : statements) {
      rootNode.add(statement.toJSONNode(getVersion()));
    }

    lrsResponse.setRequest(new HTTPRequest());
    lrsResponse.getRequest().setResource("statements");
    lrsResponse.getRequest().setMethod(HttpMethods.POST);
    lrsResponse.getRequest().setContentType("application/json");
    try {
      lrsResponse
          .getRequest()
          .setContent(Mapper.getWriter(this.usePrettyJSON()).writeValueAsBytes(rootNode));
    } catch (JsonProcessingException ex) {
      lrsResponse.setErrMsg("Exception: " + ex.toString());
      return lrsResponse;
    }

    HTTPResponse response = makeSyncRequest(lrsResponse.getRequest());
    int status = response.getStatus();

    lrsResponse.setResponse(response);

    if (status == 200) {
      lrsResponse.setSuccess(true);
      lrsResponse.setContent(new StatementsResult());
      try {
        Iterator it =
            Mapper.getInstance().readValue(response.getContent(), ArrayNode.class).elements();
        for (int i = 0; it.hasNext(); ++i) {
          lrsResponse.getContent().getStatements().add(statements.get(i));
          lrsResponse
              .getContent()
              .getStatements()
              .get(i)
              .setId(UUID.fromString(((JsonNode) it.next()).textValue()));
        }
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
示例#4
0
  @Override
  public LRSResponse clearState(Activity activity, Agent agent, UUID registration) {
    HashMap<String, String> queryParams = new HashMap<String, String>();

    queryParams.put("activityId", activity.getId().toString());
    queryParams.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
    if (registration != null) {
      queryParams.put("registration", registration.toString());
    }
    return deleteDocument("activities/state", queryParams);
  }
示例#5
0
  @Override
  public ProfileKeysLRSResponse retrieveStateIds(
      Activity activity, Agent agent, UUID registration) {
    HashMap<String, String> queryParams = new HashMap<String, String>();
    queryParams.put("activityId", activity.getId().toString());
    queryParams.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
    if (registration != null) {
      queryParams.put("registration", registration.toString());
    }

    return getProfileKeys("activities/state", queryParams);
  }
示例#6
0
  @Override
  public StatementLRSResponse saveStatement(Statement statement) {
    StatementLRSResponse lrsResponse = new StatementLRSResponse();
    lrsResponse.setRequest(new HTTPRequest());

    lrsResponse.getRequest().setResource("statements");
    lrsResponse.getRequest().setContentType("application/json");

    try {
      lrsResponse
          .getRequest()
          .setContent(statement.toJSON(this.getVersion(), this.usePrettyJSON()).getBytes("UTF-8"));
    } catch (IOException ex) {
      lrsResponse.setErrMsg("Exception: " + ex.toString());
      return lrsResponse;
    }

    if (statement.getId() == null) {
      lrsResponse.getRequest().setMethod(HttpMethods.POST);
    } else {
      lrsResponse.getRequest().setMethod(HttpMethods.PUT);
      lrsResponse.getRequest().setQueryParams(new HashMap<String, String>());
      lrsResponse.getRequest().getQueryParams().put("statementId", statement.getId().toString());
    }

    lrsResponse.setResponse(makeSyncRequest(lrsResponse.getRequest()));
    int status = lrsResponse.getResponse().getStatus();

    lrsResponse.setContent(statement);

    // TODO: handle 409 conflict, etc.
    if (status == 200) {
      lrsResponse.setSuccess(true);
      String content = lrsResponse.getResponse().getContent();
      try {
        lrsResponse
            .getContent()
            .setId(
                UUID.fromString(
                    Mapper.getInstance().readValue(content, ArrayNode.class).get(0).textValue()));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else if (status == 204) {
      lrsResponse.setSuccess(true);
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
示例#7
0
  @Override
  public void saveState(State state, String activityId, Agent agent, UUID registration)
      throws Exception {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("stateId", state.getId());
    params.put("activityId", activityId);
    params.put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));
    if (registration != null) {
      params.put("registration", registration.toString());
    }

    String queryString = "?";
    Boolean first = true;
    for (Map.Entry<String, String> parameter : params.entrySet()) {
      queryString +=
          (first ? "" : "&")
              + URLEncoder.encode(parameter.getKey(), "UTF-8")
              + "="
              + URLEncoder.encode(parameter.getValue(), "UTF-8").replace("+", "%20");
      first = false;
    }

    HTTPRequest request = new HTTPRequest();
    request.setMethod(HttpMethods.PUT);
    request.setURL(this.getEndpoint() + "activities/state" + queryString);
    request.setRequestContent(new ByteArrayBuffer(state.getContents()));

    // TODO: need to set the 'updated' property based on header
    HTTPResponse response = this.sendRequest(request);
    int status = response.getStatus();

    if (status == 204) {
      return;
    }
    throw new UnexpectedHTTPResponse(response);
  }