Example #1
0
  @Override
  public List<String> saveStatements(List<Statement> statements) throws Exception {
    List<String> statementIds = new ArrayList<String>();
    if (statements.size() == 0) {
      return statementIds;
    }

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

    HTTPRequest request = new HTTPRequest();
    request.setRequestContent(
        new ByteArrayBuffer(
            Mapper.getWriter(this.usePrettyJSON()).writeValueAsString(rootNode), "UTF-8"));
    request.setMethod(HttpMethods.POST);
    request.setURL(this.getEndpoint() + "statements");

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

    if (status == 200) {
      String content = request.getResponseContent();
      Iterator it = Mapper.getInstance().readValue(content, ArrayNode.class).elements();
      while (it.hasNext()) {
        statementIds.add(((JsonNode) it.next()).textValue());
      }
      return statementIds;
    }

    throw new UnexpectedHTTPResponse(response);
  }
Example #2
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;
  }