Establish the optional sampling theorem for a right-continuous submartingale $ \left\{X_t,\mathscr{F_t} ; 0 \leq t < \infty \right\}$ and optional times $S\leq T$ under either of the following two conditions:
i. $T$ is a bounded optional time (there exists a number $a>0$, such that $T \leq a$);
ii. $\exists$ an integrable random variable $Y$, such that $X_t \leq E(Y|\mathscr{F_t})$ a. s. $P$, for every $t \geq 0$.[Problem
3.23, Page20, Chapter1of Brownian Motion and Stochastic Calculus By Ioannis Karatzas and Steven Shreve ]
I am trying to solve part (ii) of the problem.
The Theorem before the Problem was
My first idea was to consider the process $ \hat{X}_t =\begin{cases} X_t,& \text{ if } t < \infty\\ \mathbf{E}[Y \mid { \mathscr{F}_\infty }] & \text{if else.} \end{cases}.$
And use the above Theorem, but then my problem is to recover the original process in the event $[S=\infty]$. Should I consider something else?

I give in the following a sketch of a direct argument.
The idea is stolen (shamelessly) from George Lowther's blog, in which the proof of Optional Sampling Theorem is based on the following reduction of the problem.
First the assertion of the theorem is true (please take a look at argument of GL's blog) if you can prove the following claim for a sub-martingale process $X$ : $$E[X_\tau - X_\sigma]\geq 0~ (Eq.1)$$
To show this introduce (within the scope of hypotheses i or ii) the following sequence of simple (in the sense of GL's blog) stopping times : $$\tau_n = min \{m/n ; m, n\in \mathbb{N}^2 \}$$ (and $\sigma_n$ along the same line), observe (show) that both sequence are well defined and converge almost surely to $\tau, \sigma$.
Now you can conclude if you can show that $X_{\tau_n},X_{\sigma_n}$ are both Uniformly Integrable (UI), because for them it is easy to see that :
$$E[X_{\tau_n} - X_{\sigma_n}]\geq 0~ (Eq.1_n)$$
Using $L^1$ convergence of U. I. sequence, then you are done because this implies (1).
GL's entry of the proof for the UI property of $X_{\tau_n}$ is done through lemma 6, but for it to be meaningful $X_{\tau_n}$ has to exist which is the point to prove especially for the item i- where $\tau$ and/or $\sigma$ can be infinite with positive probability and so $\tau_n$ and/or $\sigma_n$.