Пример #1
0
  /**
   * Gets a session's info. This end point requires authentication and can only be accessed by
   * ROLE_ADMIN users and the owner of the session being browsed.
   *
   * @param sessionHeader The authentication header. Feed into UserSession.resume().
   * @param username The username from the URL.
   * @param sessionId The unique session identifier.
   * @return 200 OK.
   */
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("{sessionId}")
  public Response getSession(
      @HeaderParam(UserSession.HEADER) String sessionHeader,
      @PathParam("username") String username,
      @PathParam("sessionId") String sessionId)
      throws ApiException {

    SQLConnection sql = SQLConnection.connectDefault();

    UserSession session = null;
    UserSession desired = null;
    try {
      session = UserSession.resume(sql, sessionHeader);
      desired = UserSession.resume(sql, username, sessionId);

      OMUtil.adminOrOwnerCheck(session.getUser(), desired.getUser());

    } finally {
      sql.close();
    }

    UserSessionResponse response = desired.toResponse(ApiStatus.OK);
    return Response.ok(response.serialize()).build();
  }
Пример #2
0
  /**
   * Creates a new session for the given user with the given password.
   *
   * @throws ApiException If database error occurs, password is incorrect, or some other known error
   *     occurs.
   * @param username The username of the person logging in. This param is part of the URL but is
   *     ignored. Actual username comes in via Json.
   * @param jsonBody The Json request.
   * @return 201 CREATED upon completion.
   */
  @POST
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createSession(@PathParam("username") String username, String jsonBody)
      throws ApiException {
    CreateUserSessionRequest request = new CreateUserSessionRequest();
    request.deserializeFrom(jsonBody);

    SQLConnection sql = SQLConnection.connectDefault();

    UserSession session = null;
    try {
      session = UserSession.start(sql, request.user, request.pass);
    } finally {
      sql.close();
    }

    URI newUri = uriInfo.getRequestUriBuilder().path(session.getSessionId().toString()).build();

    UserSessionResponse response = session.toResponse(ApiStatus.CREATED);

    return Response.created(newUri).entity(response.serialize()).build();
  }