예제 #1
0
  /**
   * Throws a {@link AccessDeniedException} if currently no user is logged in (aka: current user is
   * anonymous).
   *
   * @param ref the referer to redirect to
   * @param logout set to true to log out (does currently nothing)
   * @return a redirect to the referer url
   * @throws AccessDeniedException if currently no user is logged in. @HTTP 303 if the user is
   *     already logged in (or <code>logout == true</code>)
   */
  @GET
  @Path("/login")
  public Response login(
      @HeaderParam("Referer") String ref,
      @QueryParam("logout") @DefaultValue("false") boolean logout,
      @QueryParam("user") String login) {
    // Check whether we want to logout
    if (logout) {
      userService.setCurrentUser(userService.getAnonymousUser());
      throw new AccessDeniedException();
    }

    // Anonymous cannot login
    if (userService.isAnonymous(userService.getCurrentUser())) throw new AccessDeniedException();

    // Check whether this is the right (desired) user
    if (login != null && !userService.getCurrentUser().equals(userService.getUser(login)))
      throw new AccessDeniedException();

    if (ref == null || "".equals(ref)) {
      ref =
          configurationService.getServerUri()
              + configurationService.getStringConfiguration("kiwi.pages.startup");
    }
    return Response.seeOther(java.net.URI.create(ref)).build();
  }
예제 #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();
    }
  }