LPS-31061

Equivalent to https://github.com/shinnlok/liferay-portal/pull/96 (feel free to close), rebased against master, one bugfix, and some code consolidation