Ejemplo n.º 1
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;
  }
Ejemplo n.º 2
0
  @Override
  public StatementsResult queryStatements(StatementsQueryInterface query) throws Exception {
    // 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.");
    }

    // Build query string
    HashMap<String, String> params = query.toParameterMap();
    StringBuilder queryString = new StringBuilder();
    Boolean first = true;
    for (Map.Entry<String, String> parameter : params.entrySet()) {
      queryString.append(
          (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() + "statements" + queryString.toString());

    HTTPResponse response = this.sendRequest(request);
    if (response.getStatus() == 200) {
      return new StatementsResult(new StringOfJSON(response.getContent()));
    }

    throw new UnexpectedHTTPResponse(response);
  }