Пример #1
0
  protected Optional<String> getUserId(String sessionId) {
    EntityManager entityManager = CoreEntityManager.INSTANCE.createEntityManager();

    try {
      SessionEntity sessionEntity = entityManager.find(SessionEntity.class, sessionId);

      if (sessionEntity == null) {
        return Optional.empty();
      }

      return Optional.of(sessionEntity.getUserId());
    } finally {
      entityManager.close();
    }
  }
Пример #2
0
  public void write(HttpServletResponse httpServletResponse, User user) {

    EntityManager entityManager = CoreEntityManager.INSTANCE.createEntityManager();
    try {
      EntityTransaction entityTransaction = entityManager.getTransaction();
      entityTransaction.begin();
      SessionEntity sessionEntity = new SessionEntity();
      sessionEntity.setSessionId(UUID.randomUUID().toString());
      sessionEntity.setUserId(user.getUserId());
      entityManager.persist(sessionEntity);
      entityTransaction.commit();

      Cookie cookie = new Cookie(COOKIE_NAME_SESSION_ID, sessionEntity.getSessionId());
      cookie.setPath("/");
      cookie.setMaxAge(60 * 60 * 24 * 365);
      httpServletResponse.addCookie(cookie);
    } finally {
      entityManager.close();
    }
  }