Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save marcosblandim/f1b0af29dc5a89451dd6ca8393f0ba34 to your computer and use it in GitHub Desktop.

Select an option

Save marcosblandim/f1b0af29dc5a89451dd6ca8393f0ba34 to your computer and use it in GitHub Desktop.
Get the Display Page Asset and Journal inside a Portlet
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
import com.liferay.info.constants.InfoDisplayWebKeys;
import com.liferay.info.item.InfoItemDetails;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.service.JournalArticleLocalService;
import com.liferay.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.PortalUtil;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
/*
* Notes:
* - The portlet must be instantiated inside a Display Page for this to work.
* - PortletRequest is a parent class of ActionRequest, RenderRequest and ResourceRequest, so portletRequest can be any instance of those 3 classes.
*/
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(portletRequest);
HttpServletRequest originalServletRequest = PortalUtil.getOriginalServletRequest(httpServletRequest);
InfoItemDetails infoItemDetails = ((InfoItemDetails) originalServletRequest.getAttribute(InfoDisplayWebKeys.INFO_ITEM_DETAILS));
long classPK = infoItemDetails.getInfoItemReference().getClassPK();
String className = infoItemDetails.getClassName();
// Util classes were used here to simplify the code, but you should use OSGi injected Services instead.
AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(className, classPK);
JournalArticle journalArticle = JournalArticleLocalServiceUtil.getLatestArticle(classPK);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment