@Override
  public String getActualURL(
      long companyId,
      long groupId,
      boolean privateLayout,
      String mainPath,
      String friendlyURL,
      Map<String, String[]> params,
      Map<String, Object> requestContext)
      throws PortalException {

    String urlTitle =
        friendlyURL.substring(JournalArticleConstants.CANONICAL_URL_SEPARATOR.length());

    JournalArticle journalArticle =
        _journalArticleLocalService.getArticleByUrlTitle(groupId, urlTitle);

    Layout layout = getJournalArticleLayout(groupId, privateLayout, friendlyURL);

    String layoutActualURL = PortalUtil.getLayoutActualURL(layout, mainPath);

    InheritableMap<String, String[]> actualParams = new InheritableMap<>();

    if (params != null) {
      actualParams.setParentMap(params);
    }

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

    String defaultAssetPublisherPortletId =
        typeSettingsProperties.get(LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID);

    String currentDefaultAssetPublisherPortletId = defaultAssetPublisherPortletId;

    if (Validator.isNull(defaultAssetPublisherPortletId)) {
      PortletInstance portletInstance =
          new PortletInstance(AssetPublisherPortletKeys.ASSET_PUBLISHER);

      defaultAssetPublisherPortletId = portletInstance.getPortletInstanceKey();
    }

    HttpServletRequest request = (HttpServletRequest) requestContext.get("request");

    if (Validator.isNull(currentDefaultAssetPublisherPortletId)) {
      String actualPortletAuthenticationToken =
          AuthTokenUtil.getToken(request, layout.getPlid(), defaultAssetPublisherPortletId);

      actualParams.put("p_p_auth", new String[] {actualPortletAuthenticationToken});
    }

    actualParams.put("p_p_id", new String[] {defaultAssetPublisherPortletId});
    actualParams.put("p_p_lifecycle", new String[] {"0"});

    if (Validator.isNull(currentDefaultAssetPublisherPortletId)) {
      actualParams.put("p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
    }

    actualParams.put("p_p_mode", new String[] {"view"});
    actualParams.put("p_j_a_id", new String[] {String.valueOf(journalArticle.getId())});

    String namespace = PortalUtil.getPortletNamespace(defaultAssetPublisherPortletId);

    actualParams.put(namespace + "mvcPath", new String[] {"/view_content.jsp"});

    AssetRendererFactory<?> assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
            JournalArticle.class.getName());

    actualParams.put(namespace + "type", new String[] {assetRendererFactory.getType()});

    actualParams.put(namespace + "urlTitle", new String[] {journalArticle.getUrlTitle()});

    String queryString = HttpUtil.parameterMapToString(actualParams, false);

    if (layoutActualURL.contains(StringPool.QUESTION)) {
      layoutActualURL = layoutActualURL + StringPool.AMPERSAND + queryString;
    } else {
      layoutActualURL = layoutActualURL + StringPool.QUESTION + queryString;
    }

    Locale locale = PortalUtil.getLocale(request);

    PortalUtil.addPageSubtitle(journalArticle.getTitle(locale), request);
    PortalUtil.addPageDescription(journalArticle.getDescription(locale), request);

    List<AssetTag> assetTags =
        _assetTagLocalService.getTags(
            JournalArticle.class.getName(), journalArticle.getPrimaryKey());

    if (!assetTags.isEmpty()) {
      PortalUtil.addPageKeywords(ListUtil.toString(assetTags, AssetTag.NAME_ACCESSOR), request);
    }

    return layoutActualURL;
  }
Esempio n. 2
0
  public static void doTag(
      String portletName,
      String instanceId,
      String queryString,
      String settingsScope,
      String defaultPreferences,
      boolean persistSettings,
      PageContext pageContext,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    if (pageContext != null) {
      response = new PipingServletResponse(response, pageContext.getOut());
    }

    PortletInstance portletInstance = PortletInstance.fromPortletInstanceKey(portletName);

    if (Validator.isNotNull(instanceId)) {
      portletInstance =
          new PortletInstance(
              portletInstance.getPortletName(), portletInstance.getUserId(), instanceId);
    }

    RestrictPortletServletRequest restrictPortletServletRequest =
        new RestrictPortletServletRequest(PortalUtil.getOriginalServletRequest(request));

    queryString =
        PortletParameterUtil.addNamespace(portletInstance.getPortletInstanceKey(), queryString);

    Map<String, String[]> parameterMap = request.getParameterMap();

    if (!Objects.equals(portletInstance.getPortletInstanceKey(), request.getParameter("p_p_id"))) {

      parameterMap = MapUtil.filterByKeys(parameterMap, (key) -> !key.startsWith("p_p_"));
    }

    request =
        DynamicServletRequest.addQueryString(
            restrictPortletServletRequest, parameterMap, queryString, false);

    try {
      request.setAttribute(WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);

      ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

      Portlet portlet =
          getPortlet(themeDisplay.getCompanyId(), portletInstance.getPortletInstanceKey());

      Stack<String> embeddedPortletIds = _embeddedPortletIds.get();

      if (embeddedPortletIds == null) {
        embeddedPortletIds = new Stack<>();

        _embeddedPortletIds.set(embeddedPortletIds);
      }

      if (embeddedPortletIds.search(portlet.getPortletId()) > -1) {
        String errorMessage = LanguageUtil.get(request, "the-application-cannot-include-itself");

        request.setAttribute("liferay-portlet:runtime:errorMessage", errorMessage);

        PortalIncludeUtil.include(pageContext, _ERROR_PAGE);

        return;
      }

      if (themeDisplay.isStateMaximized()) {
        LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

        if (layoutTypePortlet.hasStateMaxPortletId(portletInstance.getPortletInstanceKey())) {

          // A portlet in the maximized state has already been
          // processed

          return;
        }
      }

      Layout layout = themeDisplay.getLayout();

      request.setAttribute(WebKeys.SETTINGS_SCOPE, settingsScope);

      JSONObject jsonObject = null;

      boolean writeJSONObject = false;

      LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

      if (persistSettings && !layoutTypePortlet.isPortletEmbedded(portlet.getPortletId())) {

        PortletPreferencesFactoryUtil.getLayoutPortletSetup(
            themeDisplay.getCompanyId(),
            themeDisplay.getScopeGroupId(),
            PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
            PortletKeys.PREFS_PLID_SHARED,
            portletInstance.getPortletInstanceKey(),
            defaultPreferences);

        writeJSONObject = true;
      }

      if (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
              themeDisplay.getPlid(),
              portletInstance.getPortletInstanceKey())
          < 1) {

        PortletPreferencesFactoryUtil.getLayoutPortletSetup(
            layout, portletInstance.getPortletInstanceKey(), defaultPreferences);
        PortletPreferencesFactoryUtil.getPortletSetup(
            request, portletInstance.getPortletInstanceKey(), defaultPreferences);

        PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

        if (portletLayoutListener != null) {
          portletLayoutListener.onAddToLayout(
              portletInstance.getPortletInstanceKey(), themeDisplay.getPlid());
        }

        writeJSONObject = true;
      }

      if (writeJSONObject) {
        jsonObject = JSONFactoryUtil.createJSONObject();

        PortletJSONUtil.populatePortletJSONObject(request, StringPool.BLANK, portlet, jsonObject);
      }

      if (jsonObject != null) {
        PortletJSONUtil.writeHeaderPaths(response, jsonObject);
      }

      embeddedPortletIds.push(portletInstance.getPortletInstanceKey());

      PortletContainerUtil.render(request, response, portlet);

      embeddedPortletIds.pop();

      if (jsonObject != null) {
        PortletJSONUtil.writeFooterPaths(response, jsonObject);
      }
    } finally {
      restrictPortletServletRequest.mergeSharedAttributes();
    }
  }