Exemple #1
0
  @Override
  public AboutLRSResponse about() {
    HTTPRequest request = new HTTPRequest();
    request.setMethod(HttpMethods.GET);
    request.setResource("about");

    HTTPResponse response = makeSyncRequest(request);
    int status = response.getStatus();

    AboutLRSResponse lrsResponse = new AboutLRSResponse(request, response);

    if (status == 200) {
      lrsResponse.setSuccess(true);
      try {
        lrsResponse.setContent(new About(response.getContent()));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
Exemple #2
0
  private ProfileKeysLRSResponse getProfileKeys(
      String resource, HashMap<String, String> queryParams) {
    HTTPRequest request = new HTTPRequest();
    request.setMethod(HttpMethods.GET);
    request.setResource(resource);
    request.setQueryParams(queryParams);

    HTTPResponse response = makeSyncRequest(request);

    ProfileKeysLRSResponse lrsResponse = new ProfileKeysLRSResponse(request, response);

    if (response.getStatus() == 200) {
      lrsResponse.setSuccess(true);
      try {
        Iterator it =
            Mapper.getInstance().readValue(response.getContent(), ArrayNode.class).elements();

        lrsResponse.setContent(new ArrayList<String>());
        while (it.hasNext()) {
          lrsResponse.getContent().add(it.next().toString());
        }
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
Exemple #3
0
  private StatementLRSResponse getStatement(String id, String paramName) {
    HTTPRequest request = new HTTPRequest();
    request.setMethod(HttpMethods.GET);
    request.setResource("statements");
    request.setQueryParams(new HashMap<String, String>());
    request.getQueryParams().put(paramName, id);

    HTTPResponse response = makeSyncRequest(request);
    int status = response.getStatus();

    StatementLRSResponse lrsResponse = new StatementLRSResponse(request, response);

    if (status == 200) {
      lrsResponse.setSuccess(true);
      try {
        lrsResponse.setContent(new Statement(new StringOfJSON(response.getContent())));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
Exemple #4
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;
  }
Exemple #5
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;
  }
Exemple #6
0
  @Override
  public StatementsResultLRSResponse queryStatements(StatementsQueryInterface query) {
    // Setup empty query object if null was passed in
    if (query == null) {
      query =
          (this.getVersion() == TCAPIVersion.V095)
              ? new com.rusticisoftware.tincan.v095.StatementsQuery()
              : new StatementsQuery();
    }

    // Choke if the query parameters don't match the LRS version
    if (this.getVersion() != query.getVersion()) {
      throw new IncompatibleTCAPIVersion(
          "Attempted to issue "
              + this.getVersion()
              + " query using a "
              + query.getVersion()
              + " set of query parameters.");
    }

    StatementsResultLRSResponse lrsResponse = new StatementsResultLRSResponse();

    lrsResponse.setRequest(new HTTPRequest());
    lrsResponse.getRequest().setMethod(HttpMethods.GET);
    lrsResponse.getRequest().setResource("statements");

    try {
      lrsResponse.getRequest().setQueryParams(query.toParameterMap());
    } catch (IOException ex) {
      lrsResponse.setErrMsg("Exception: " + ex.toString());
      return lrsResponse;
    }

    HTTPResponse response = makeSyncRequest(lrsResponse.getRequest());

    lrsResponse.setResponse(response);

    if (response.getStatus() == 200) {
      lrsResponse.setSuccess(true);
      try {
        lrsResponse.setContent(new StatementsResult(new StringOfJSON(response.getContent())));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
Exemple #7
0
  @Override
  public StatementsResultLRSResponse moreStatements(String moreURL) {
    if (moreURL == null) {
      return null;
    }

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

    HTTPRequest request = new HTTPRequest();
    request.setResource(url);
    request.setMethod(HttpMethods.GET);
    HTTPResponse response = makeSyncRequest(request);

    StatementsResultLRSResponse lrsResponse = new StatementsResultLRSResponse(request, response);

    if (response.getStatus() == 200) {
      lrsResponse.setSuccess(true);
      try {
        lrsResponse.setContent(new StatementsResult(new StringOfJSON(response.getContent())));
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }