Exemplo n.º 1
0
  /**
   * Reinitialise the database configuration. Will close the database connection and reopen it,
   * possibly with new settings.
   *
   * @return ok if successful, 500 if not @HTTP 200 if database was reinitialised successfully @HTTP
   *     500 if there was an error while reinitialising database (see log)
   */
  @POST
  @Path("/database/reinit")
  public Response reinitDatabase() {
    log.info("Reinitialising database after admin user request ...");
    try {
      sesameService.shutdown();
      sesameService.initialise();

      return Response.ok().entity("database reinitialised successfully").build();
    } catch (Exception ex) {
      log.error("Error while reinitalising database ...", ex);
      return ErrorResponse.errorResponse(Response.Status.INTERNAL_SERVER_ERROR, ex);
    }
  }
Exemplo n.º 2
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();
    }
  }
Exemplo n.º 3
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();
    }
  }
Exemplo n.º 4
0
  /**
   * Update/Set the profile information (foaf) for the current user. Post-Body should contain the
   * property=value mapping (propterty without foaf-prefix) for the profile.
   *
   * @param formParams the user profile (foaf, without prefix) in {@value
   *     Namespaces#MIME_TYPE_FORM_URLENC}
   * @return {@link AccountPoJo} after the update in JSON @HTTP 403 When the current user is <code>
   *     anonymous</code>. @HTTP 500 If a {@link RepositoryException} occurs (which should not
   *     happen as no namespaces are used here)
   */
  @POST
  @Path("/me")
  @Consumes(Namespaces.MIME_TYPE_FORM_URLENC)
  public Response post(MultivaluedMap<String, String> formParams) {
    final URI currentUser = userService.getCurrentUser();
    if (userService.isAnonymous(currentUser))
      return Response.status(Status.FORBIDDEN).entity("anonymous is read-only").build();

    try {
      RepositoryConnection conn = sesameService.getConnection();

      try {
        for (String prop : formParams.keySet()) {
          if (!acceptedFoafProperties.contains(prop)) {
            continue;
          }
          URI p = conn.getValueFactory().createURI(Namespaces.NS_FOAF + prop);

          conn.remove(currentUser, p, null);

          String val = formParams.getFirst(prop);
          if (val != null && val.length() > 0) {
            Matcher m = PROFILE_URI_PATTERN.matcher(val);
            if (m.matches()) {
              URI o = conn.getValueFactory().createURI(m.group(1));
              conn.add(currentUser, p, o, currentUser);
            } else {
              Literal o = conn.getValueFactory().createLiteral(val.trim());
              conn.add(currentUser, p, o, currentUser);
            }
          }
        }
        return get(currentUser);
      } finally {
        conn.commit();
        conn.close();
      }
    } catch (RepositoryException e) {
      // This must not happen!
      return Response.serverError().entity(e).build();
    }
  }
Exemplo n.º 5
0
  /**
   * Resolve/Redirect access to /user/* uris.
   *
   * @param login the login of the user to redirect to
   * @param types header param of accepted mime-types
   * @return a redirect to the user-resource in the resource service. @HTTP 404 if no such user
   *     exists. @HTTP 303 on success @HTTP 400 if no valid resource uri could be built with the
   *     login @HTTP 500 on other exceptions
   */
  @GET
  @Path("/{login:[^#?]+}")
  public Response getUser(@PathParam("login") String login, @HeaderParam("Accept") String types) {
    if (login.equals("me")) {
      return get();
    } else {
      try {
        RepositoryConnection conn = sesameService.getConnection();
        try {
          final URI user = userService.getUser(login);
          if (user == null)
            return Response.status(Status.NOT_FOUND)
                .entity(String.format("User %s not found", login))
                .build();

          java.net.URI u =
              new java.net.URI(
                  configurationService.getServerUri()
                      + "resource?uri="
                      + URLEncoder.encode(user.stringValue(), "utf-8"));

          return Response.seeOther(u).header("Accept", types).build();
        } finally {
          conn.commit();
          conn.commit();
        }
      } catch (URISyntaxException e) {
        return Response.status(Status.BAD_REQUEST)
            .entity(String.format("Invalid URI: %s", e.getMessage()))
            .build();
      } catch (UnsupportedEncodingException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
      } catch (RepositoryException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
      }
    }
  }