Liferay on Private Site to get Public Site Page URL

来源:互联网 发布:学校网络舆情监控制度 编辑:程序博客网 时间:2024/04/19 10:21

This is a very interesting design in our website. We have a private site only for registered member. There's a youbar portlet which list bunches of articles. Members can share those articles in social networks like twitter, facebook, plusone and pinit. When the user in those social networks see the link our member shares, they should see the article detail via the link. But if we use the friendly url of private page, Liferay will link to login page first because it's private page.

Our design is we create a special YouBar page on Public Site which only has youbar portlet on it. And when member shares the articles on private site, we use the public site page friendly url instead of private one.

The thing is how to get YouBar page url. Liferay allows multiple pages with the same name. We can make an unique name for that.

1.  our website is an organization website, All member is in the organization or sub-organization. If any member is in sub-organization, we have to get the top parent organization.

2. Every page in Liferay is a Layout. We need to identify the layout name equals "YouBar"

This method is used to get member's top organization in order to get group and get Layout.

public static String getPublicPageURLByName(ThemeDisplay themeDisplay, String pageName) throws Exception{Group group = null;if(themeDisplay.isSignedIn()){User user = themeDisplay.getUser();if(user != null){List<Organization> organizations = user.getOrganizations();if(organizations.size() > 0){for(Organization organization : organizations){while(organization.getParentOrganization() != null){organization = organization.getParentOrganization();}logger.info(organization.getName());group = organization.getGroup();break;}}}}else{group = themeDisplay.getScopeGroup();logger.info(group.getName());}return getPageURLByGroup(themeDisplay, pageName, group);}
This method is to get the "YouBar" Page url.
public static String getPageURLByGroup(ThemeDisplay themeDisplay, String pageName, Group group)throws Exception{String pageURL = "";List<Layout> allLayouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);for(Layout layout : allLayouts){if(layout.getNameCurrentValue().equalsIgnoreCase(pageName)){pageURL = themeDisplay.getPortalURL() + themeDisplay.getPathFriendlyURLPublic() + group.getFriendlyURL() + layout.getFriendlyURL();break;}}return pageURL;}
It works only when member logs in. For not log in user, it doesn't work. Because it doesn't belong the special organization. One thing we can do is to search the organization based on organization name.

The other way is to configure partial url in properties file. Because we can get the portal url and public friendly url.

youbar.properties 

dev.youbar.url=/web/guest/youbarqa.youbar.url=prod.youbar.url=

Controller.java

@Value("${${spring.profiles.active}.youbar.url}")private String youbarPartialFriendlyURL;
String friendlyURLPrefix = themeDisplay.getPortalURL() + themeDisplay.getPathFriendlyURLPublic() + youbarPartialFriendlyURL;

Then we can use the prefix now.

<c:set var="youbarFullFriendlyURL" value="${youbar_friendlyURL_prefix}/-/youbar/${channelArticle.friendlyUrl}.html" />
According to your friendly url setting, you can configure it.