/** {@inheritDoc} */
  @Nonnull
  @Override
  protected Event doExecute(
      final @Nonnull RequestContext springRequestContext,
      final @Nonnull ProfileRequestContext<ServiceTicketRequest, Object> profileRequestContext) {

    final ServiceTicketRequest request =
        FlowStateSupport.getServiceTicketRequest(springRequestContext);

    // Per http://www.jasig.org/cas/protocol section 2.1.1
    // It is RECOMMENDED that renew supersede gateway
    if (request.isRenew()) {
      return new Event(this, Events.RenewRequested.id());
    }

    if (request.isGateway()) {
      return new Event(this, Events.GatewayRequested.id());
    }

    final SessionContext sessionCtx =
        profileRequestContext.getSubcontext(SessionContext.class, false);
    Events result;
    if (sessionCtx != null) {
      final IdPSession session = sessionCtx.getIdPSession();
      if (session != null) {
        log.debug("Found session ID {}", session.getId());
        try {
          // Timeout check updates session lastActivityInstant field
          if (session.checkTimeout()) {
            result = Events.SessionFound;
          } else {
            result = Events.SessionNotFound;
          }
        } catch (SessionException e) {
          log.debug("Error performing session timeout check. Assuming session has expired.", e);
          result = Events.SessionNotFound;
        }
      } else {
        log.debug("Session not found.");
        result = Events.SessionNotFound;
      }
    } else {
      log.debug("Session context not found.");
      result = Events.SessionNotFound;
    }
    return result.event(this);
  }