For a "deterministic finite energy" continuous time signal $x\left(t\right)$, as you have exemplified, the energy density spectrum, or equivalently energy spectral density, which is a real nonnegative signal, meant to be used as a function showing the energy distribution of the signal with respect to the frequency is defined to be: $$S_x\left(\omega\right) = \left|X\left(j\omega\right)\right|^2 $$ where $X\left(j\omega\right)$ is the Continuous-Time Fourier Transform $\left(CTFT\right)$ of the signal $x\left(t\right)$ given as: $$X\left(j\omega\right) = \int_{-\infty}^{\infty}{x\left(t\right)e^{-j\omega t} dt}$$
Now coming to the specific sample of your question, you should first find the CTFT $G\left(j\omega\right)$ of $g\left(t\right)$: That your signal is a multiplication of two other signals you should use a fundamental property of CTFT namely the ${multiplication}$ in time equals ${convolution}$ in frequency. Hence
$$x\left(t\right)y\left(t\right) \leftrightarrow \frac{1}{2\pi} X\left(j\omega\right) \star Y\left(j\omega\right) $$
From a table of FTs (or by carrying out the integral if you can) find that for
$x\left(t\right)= \textrm {rect} \left( \frac {t}{T} \right)$ which is defined from $t=-T/2$ to $t=T/2$, we have: $$X\left(j\omega\right) = \frac{2 \sin \left({\omega \frac{T}{2} }\right)} {\omega}$$ and for the $y\left(t\right) = \cos\left( \pi \frac {t}{T}\right)$ we have $$Y\left(j\omega\right) = \pi \delta \left(\omega - \frac {\pi}{T}\right) + \pi \delta \left(\omega + \frac{\pi}{T}\right)$$
What remains now is to compute their convolution, which is easy considering the convolution with impulse given as:$X\left(j\omega\right) \star \delta\left(\omega - \omega_0\right) = X\left(j\left(\omega - \omega_0\right)\right) $
Hence we get: $$G\left(j\omega\right) = \frac{1}{2\pi} X\left(j\omega\right) \star Y\left(j\omega\right)$$
as $$\frac{\sin \left({\left(\omega- \frac {\pi}{T}\right) \frac{T}{2} }\right)} {\left(\omega- \frac {\pi}{T}\right)} + \frac{\sin \left({\left(\omega + \frac {\pi}{T}\right) \frac{T}{2} }\right)} {\left(\omega + \frac {\pi}{T}\right)}$$
which, after simplyfying, becomes:
$$G\left(j\omega\right) = \cos\left({\omega\frac{T}{2} }\right) \left( \frac{-1} {\left(\omega- \frac {\pi}{T}\right)} + \frac{1} {\left(\omega + \frac {\pi}{T}\right)} \right)$$
whose magnitude is $$\left|G\left(j\omega\right)\right| = \left|\cos\left(\omega \frac{T}{2}\right)\right| \cdot \left|\left( \frac{ \frac{-2\pi}{T}}{\omega^2 - \frac{\pi^2}{T^2}} \right)\right|$$
And from which we conclude for the Energy Spectrum Density to be:
$$S_x\left(\omega\right) = \left|G\left(j\omega\right)\right|^2 = A^2 \cos^2\left(\omega \frac{T}{2}\right) \left( \frac{ \frac{2\pi}{T}}{\omega^2 - \frac{\pi^2}{T^2}} \right)^2 $$
where I have added the last linear scaler A in $g\left(t\right)$.