Ejemplo n.º 1
0
  /**
   * Return a list of versions that affect the resource whose uri is passed as argument. For each
   * version, the result will contain the id, the creator, and the date when the version was
   * recorded. Further details for a version can be requested by calling the
   * /versioning/versions/{id} webservice.
   *
   * <p>Note that resource_uri is an optional parameter. In case no resource uri is given, all
   * versions recorded by the LMF are returned, which can take a considerable amount of time. @HTTP
   * 200 in case the versions were retrieved successfully @HTTP 404 in case the resource passed as
   * argument resource_uri could not be found
   *
   * @param resource_uri the URI of the resource for which to return the versions (optional, see
   *     warning above)
   * @return a JSON list of versions, each a map with the properties "id" (long), "creator" (uri),
   *     "date" (ISO 8601 String)
   */
  @GET
  @Produces("application/json")
  @Path("/versions/list")
  public Response getVersions(
      @QueryParam("resource") String resource_uri,
      @QueryParam("from") String dateFrom,
      @QueryParam("to") String dateTo) {
    try {
      RepositoryConnection conn = sesameService.getConnection();
      try {
        if (resource_uri != null) {
          URI resource = ResourceUtils.getUriResource(conn, resource_uri);
          if (resource != null && resource instanceof KiWiUriResource) {

            if (dateFrom == null && dateTo == null) {
              return Response.ok()
                  .entity(formatVersions(versioningService.listVersions(resource)))
                  .build();
            } else {
              Date dateFromD = DateUtils.parseDate(dateFrom);
              Date dateToD = DateUtils.parseDate(dateTo);
              return Response.ok()
                  .entity(
                      formatVersions(versioningService.listVersions(resource, dateFromD, dateToD)))
                  .build();
            }
          } else {
            return Response.status(Response.Status.NOT_FOUND)
                .entity("resource with URI " + resource_uri + " was not found in the system")
                .build();
          }
        } else {
          if (dateFrom == null && dateTo == null) {
            return Response.ok().entity(formatVersions(versioningService.listVersions())).build();
          } else {
            Date dateFromD = DateUtils.parseDate(dateFrom);
            Date dateToD = DateUtils.parseDate(dateTo);
            return Response.ok()
                .entity(formatVersions(versioningService.listVersions(dateFromD, dateToD)))
                .build();
          }
        }
      } finally {
        conn.commit();
        conn.close();
      }
    } catch (RepositoryException ex) {
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    } catch (SailException ex) {
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
  }
Ejemplo n.º 2
0
  private Response get(URI user) {
    if (userService.isAnonymous(user)) {
      AccountPoJo apj = new AccountPoJo(Namespaces.ANONYMOUS_LOGIN, user.stringValue());
      return Response.ok(apj, Namespaces.MIME_TYPE_JSON)
          .location(java.net.URI.create(user.stringValue()))
          .build();
    }
    try {
      RepositoryConnection conn = sesameService.getConnection();
      try {
        final UserAccount a = accountService.getAccount(user);
        if (a != null) {
          AccountPoJo apj = new AccountPoJo(a.getLogin(), a.getWebId());
          apj.setRoles(a.getRoles());

          for (Statement t :
              ResourceUtils.listOutgoing(conn, conn.getValueFactory().createURI(a.getWebId()))) {
            String prop = t.getPredicate().stringValue();
            if (prop.startsWith(Namespaces.NS_FOAF)) {
              Value object = t.getObject();
              if (object instanceof org.openrdf.model.URI) {
                apj.setFoaf(prop, String.format("<%s>", object));
              } else if (object instanceof Literal) {
                apj.setFoaf(prop, object.toString());
              }
            }
          }

          return Response.ok(apj, Namespaces.MIME_TYPE_JSON)
              .location(java.net.URI.create(user.stringValue()))
              .build();
        }
        return Response.status(Status.NOT_FOUND)
            .entity("Could not find account data of " + user)
            .build();
      } finally {
        conn.commit();
        conn.close();
      }
    } catch (RepositoryException e) {
      // This must not happen!
      return Response.serverError().entity(e).build();
    }
  }