private String _getJavaScript() throws PortalException {
    String javaScript =
        "/com/liferay/frontend/image/editor/integration/document/library"
            + "/internal/display/context/dependencies"
            + "/edit_with_image_editor_js.ftl";

    Class<?> clazz = getClass();

    URLTemplateResource urlTemplateResource =
        new URLTemplateResource(javaScript, clazz.getResource(javaScript));

    Template template =
        TemplateManagerUtil.getTemplate(
            TemplateConstants.LANG_TYPE_FTL, urlTemplateResource, false);

    template.put("editLanguageKey", LanguageUtil.get(_request, "edit"));

    LiferayPortletResponse liferayPortletResponse = _getLiferayPortletResponse();

    template.put("namespace", liferayPortletResponse.getNamespace());

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    return unsyncStringWriter.toString();
  }
  @Test
  public void testProcessTemplate6() throws Exception {
    Template template =
        new VelocityTemplate(
            new MockTemplateResource(_WRONG_TEMPLATE_ID),
            new MockTemplateResource(_WRONG_ERROR_TEMPLATE_ID),
            null,
            _velocityEngine,
            _templateContextHelper,
            60,
            false);

    template.put(_TEST_KEY, _TEST_VALUE);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    try {
      template.processTemplate(unsyncStringWriter);

      Assert.fail();
    } catch (Exception e) {
      if (e instanceof TemplateException) {
        String message = e.getMessage();

        Assert.assertTrue(message.contains(_WRONG_ERROR_TEMPLATE_ID));

        return;
      }

      Assert.fail();
    }
  }
  @Test
  public void testPrepare() throws Exception {
    Template template =
        new VelocityTemplate(
            new MockTemplateResource(_TEMPLATE_FILE_NAME),
            null,
            null,
            _velocityEngine,
            _templateContextHelper,
            60,
            false);

    template.put(_TEST_KEY, _TEST_VALUE);

    template.prepare(null);

    Object result = template.get(_TEST_VALUE);

    Assert.assertNotNull(result);

    Assert.assertTrue(result instanceof String);

    String stringResult = (String) result;

    Assert.assertEquals(_TEST_VALUE, stringResult);
  }
  @Test(expected = IllegalArgumentException.class)
  public void testProcessMultiTemplateEmptyList() throws Exception {
    List<String> list = Collections.emptyList();

    Template template = _soyManagerTestHelper.getTemplate(list);

    template.processTemplate(new UnsyncStringWriter());
  }
  @Override
  public void prepare(Template template, HttpServletRequest request) {
    super.prepare(template, request);

    // Theme display

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

    if (themeDisplay != null) {

      // Init

      template.put(
          "init",
          themeDisplay.getPathContext()
              + TemplateConstants.SERVLET_SEPARATOR
              + "/html/themes/_unstyled/templates/init.vm");
    }

    // Theme

    Theme theme = (Theme) request.getAttribute(WebKeys.THEME);

    if ((theme == null) && (themeDisplay != null)) {
      theme = themeDisplay.getTheme();
    }

    if (theme != null) {

      // Full css and templates path

      String servletContextName = GetterUtil.getString(theme.getServletContextName());

      template.put(
          "fullCssPath",
          servletContextName + theme.getVelocityResourceListener() + theme.getCssPath());

      template.put(
          "fullTemplatesPath",
          servletContextName + theme.getVelocityResourceListener() + theme.getTemplatesPath());
    }

    // Insert custom vm variables

    Map<String, Object> vmVariables =
        (Map<String, Object>) request.getAttribute(WebKeys.VM_VARIABLES);

    if (vmVariables != null) {
      for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();

        if (Validator.isNotNull(key)) {
          template.put(key, value);
        }
      }
    }
  }
示例#6
0
  protected void mergeTemplate(
      Template template, UnsyncStringWriter unsyncStringWriter, boolean propagateException)
      throws Exception {

    if (propagateException) {
      template.doProcessTemplate(unsyncStringWriter);
    } else {
      template.processTemplate(unsyncStringWriter);
    }
  }
  @Test
  public void testProcessTemplateSimple() throws Exception {
    Template template = _soyManagerTestHelper.getTemplate("simple.soy");

    template.put("namespace", "soy.test.simple");

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    Assert.assertEquals("Hello.", unsyncStringWriter.toString());
  }
示例#8
0
  protected void mergeTemplate(Template template, UnsyncStringWriter unsyncStringWriter)
      throws Exception {

    VelocityTaglib velocityTaglib =
        (VelocityTaglib) template.get(PortletDisplayTemplateConstants.TAGLIB_LIFERAY);

    if (velocityTaglib != null) {
      velocityTaglib.setTemplate(template);
    }

    template.processTemplate(unsyncStringWriter);
  }
  @Test
  public void testProcessTemplateWithContext() throws Exception {
    Template template = _soyManagerTestHelper.getTemplate("context.soy");

    template.put("name", "Bruno Basto");
    template.put("namespace", "soy.test.withContext");

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    Assert.assertEquals("Hello. My name is Bruno Basto.", unsyncStringWriter.toString());
  }
  @Test
  public void testProcessMultiTemplateAllResources() throws Exception {
    Template template =
        _soyManagerTestHelper.getTemplate(
            Arrays.asList("multi.soy", "simple.soy", "context.soy", "multi-context.soy"));

    template.put("namespace", "soy.multiTest.simple");

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    Assert.assertEquals("Hello.", unsyncStringWriter.toString());
  }
  protected void sendHTML(
      HttpServletResponse response, String path, List<WebServerEntry> webServerEntries)
      throws Exception {

    Template template =
        TemplateManagerUtil.getTemplate(
            TemplateManager.FREEMARKER, _TEMPLATE_FTL, TemplateContextType.RESTRICTED);

    template.put("dateFormat", _dateFormat);
    template.put("entries", webServerEntries);
    template.put("path", HttpUtil.encodePath(path));

    if (_WEB_SERVER_SERVLET_VERSION_VERBOSITY_DEFAULT) {
    } else if (_WEB_SERVER_SERVLET_VERSION_VERBOSITY_PARTIAL) {
      template.put("releaseInfo", ReleaseInfo.getName());
    } else {
      template.put("releaseInfo", ReleaseInfo.getReleaseInfo());
    }

    template.put("validator", Validator_IW.getInstance());

    response.setContentType(ContentTypes.TEXT_HTML_UTF8);

    template.processTemplate(response.getWriter());
  }
示例#12
0
  protected void prepareTemplate(ThemeDisplay themeDisplay, Template template) throws Exception {

    if (themeDisplay == null) {
      return;
    }

    template.prepare(themeDisplay.getRequest());
  }
  @Test
  public void testProcessTemplate7() throws Exception {
    Template template =
        new VelocityTemplate(
            new MockTemplateResource(_WRONG_TEMPLATE_ID),
            new StringTemplateResource(_WRONG_ERROR_TEMPLATE_ID, _TEST_TEMPLATE_CONTENT),
            null,
            _velocityEngine,
            _templateContextHelper,
            60,
            false);

    template.put(_TEST_KEY, _TEST_VALUE);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    String result = unsyncStringWriter.toString();

    Assert.assertEquals(_TEST_VALUE, result);
  }
  @Test
  public void testProcessTemplate8() throws Exception {
    Map<String, Object> context = new HashMap<>();

    context.put(_TEST_KEY, _TEST_VALUE);

    Template template =
        new VelocityTemplate(
            new MockTemplateResource(_TEMPLATE_FILE_NAME),
            null,
            context,
            _velocityEngine,
            _templateContextHelper,
            60,
            false);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    template.processTemplate(unsyncStringWriter);

    String result = unsyncStringWriter.toString();

    Assert.assertEquals(_TEST_VALUE, result);
  }
  protected String getPageDiff(long companyId, WikiPage latestPage, WikiPage page, Locale locale)
      throws SystemException {

    try {
      String templateId = "com/liferay/portlet/wiki/dependencies/rss.vm";

      String templateContent = ContentUtil.get(templateId);

      Template template =
          TemplateManagerUtil.getTemplate(
              TemplateManager.VELOCITY,
              new StringTemplateResource(templateId, templateContent),
              TemplateContextType.STANDARD);

      template.put("companyId", companyId);
      template.put("contextLine", Diff.CONTEXT_LINE);
      template.put("diffUtil", new DiffUtil());
      template.put("languageUtil", LanguageUtil.getLanguage());
      template.put("locale", locale);

      String sourceContent = WikiUtil.processContent(latestPage.getContent());
      String targetContent = WikiUtil.processContent(page.getContent());

      sourceContent = HtmlUtil.escape(sourceContent);
      targetContent = HtmlUtil.escape(targetContent);

      List<DiffResult>[] diffResults =
          DiffUtil.diff(
              new UnsyncStringReader(sourceContent), new UnsyncStringReader(targetContent));

      template.put("sourceResults", diffResults[0]);
      template.put("targetResults", diffResults[1]);

      UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

      template.processTemplate(unsyncStringWriter);

      return unsyncStringWriter.toString();
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
示例#16
0
  protected String doTransform(
      ThemeDisplay themeDisplay,
      Map<String, Object> contextObjects,
      Map<String, String> tokens,
      String viewMode,
      String languageId,
      Document document,
      PortletRequestModel portletRequestModel,
      String script,
      String langType,
      boolean propagateException)
      throws Exception {

    // Setup listeners

    if (_log.isDebugEnabled()) {
      _log.debug("Language " + languageId);
    }

    if (Validator.isNull(viewMode)) {
      viewMode = Constants.VIEW;
    }

    if (_logTokens.isDebugEnabled()) {
      String tokensString = PropertiesUtil.list(tokens);

      _logTokens.debug(tokensString);
    }

    if (_logTransformBefore.isDebugEnabled()) {
      _logTransformBefore.debug(document);
    }

    List<TransformerListener> transformerListeners =
        JournalTransformerListenerRegistryUtil.getTransformerListeners();

    for (TransformerListener transformerListener : transformerListeners) {

      // Modify XML

      if (_logXmlBeforeListener.isDebugEnabled()) {
        _logXmlBeforeListener.debug(document);
      }

      if (transformerListener != null) {
        document = transformerListener.onXml(document, languageId, tokens);

        if (_logXmlAfterListener.isDebugEnabled()) {
          _logXmlAfterListener.debug(document);
        }
      }

      // Modify script

      if (_logScriptBeforeListener.isDebugEnabled()) {
        _logScriptBeforeListener.debug(script);
      }

      if (transformerListener != null) {
        script = transformerListener.onScript(script, document, languageId, tokens);

        if (_logScriptAfterListener.isDebugEnabled()) {
          _logScriptAfterListener.debug(script);
        }
      }
    }

    // Transform

    String output = null;

    if (Validator.isNull(langType)) {
      output = LocalizationUtil.getLocalization(document.asXML(), languageId);
    } else {
      long companyId = 0;
      long companyGroupId = 0;
      long articleGroupId = 0;
      long classNameId = 0;

      if (tokens != null) {
        companyId = GetterUtil.getLong(tokens.get("company_id"));
        companyGroupId = GetterUtil.getLong(tokens.get("company_group_id"));
        articleGroupId = GetterUtil.getLong(tokens.get("article_group_id"));
        classNameId = GetterUtil.getLong(tokens.get(TemplateConstants.CLASS_NAME_ID));
      }

      long scopeGroupId = 0;
      long siteGroupId = 0;

      if (themeDisplay != null) {
        companyId = themeDisplay.getCompanyId();
        companyGroupId = themeDisplay.getCompanyGroupId();
        scopeGroupId = themeDisplay.getScopeGroupId();
        siteGroupId = themeDisplay.getSiteGroupId();
      }

      String templateId = tokens.get("template_id");

      templateId = getTemplateId(templateId, companyId, companyGroupId, articleGroupId);

      Template template = getTemplate(templateId, tokens, languageId, document, script, langType);

      if (contextObjects != null) {
        template.putAll(contextObjects);
      }

      UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

      try {
        if (document != null) {
          Element rootElement = document.getRootElement();

          List<TemplateNode> templateNodes =
              getTemplateNodes(
                  themeDisplay, rootElement, Long.valueOf(tokens.get("ddm_structure_id")));

          if (templateNodes != null) {
            for (TemplateNode templateNode : templateNodes) {
              template.put(templateNode.getName(), templateNode);
            }
          }

          if (portletRequestModel != null) {
            template.put("request", portletRequestModel.toMap());

            if (langType.equals(TemplateConstants.LANG_TYPE_XSL)) {
              Document requestDocument = SAXReaderUtil.read(portletRequestModel.toXML());

              Element requestElement = requestDocument.getRootElement();

              template.put("xmlRequest", requestElement.asXML());
            }
          } else {
            Element requestElement = rootElement.element("request");

            template.put("request", insertRequestVariables(requestElement));

            if (langType.equals(TemplateConstants.LANG_TYPE_XSL)) {
              template.put("xmlRequest", requestElement.asXML());
            }
          }
        }

        template.put("articleGroupId", articleGroupId);
        template.put("company", getCompany(themeDisplay, companyId));
        template.put("companyId", companyId);
        template.put("device", getDevice(themeDisplay));

        String templatesPath = getTemplatesPath(companyId, articleGroupId, classNameId);

        Locale locale = LocaleUtil.fromLanguageId(languageId);

        template.put("locale", locale);

        template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
        template.put("randomNamespace", StringUtil.randomId() + StringPool.UNDERLINE);
        template.put("scopeGroupId", scopeGroupId);
        template.put("siteGroupId", siteGroupId);
        template.put("templatesPath", templatesPath);
        template.put("viewMode", viewMode);

        if (themeDisplay != null) {
          TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(langType);

          HttpServletRequest request = themeDisplay.getRequest();

          templateManager.addTaglibSupport(template, request, themeDisplay.getResponse());
          templateManager.addTaglibTheme(
              template,
              "taglibLiferay",
              request,
              new PipingServletResponse(themeDisplay.getResponse(), unsyncStringWriter));
        }

        // Deprecated variables

        template.put("groupId", articleGroupId);
        template.put("journalTemplatesPath", templatesPath);

        mergeTemplate(template, unsyncStringWriter, propagateException);
      } catch (Exception e) {
        if (e instanceof DocumentException) {
          throw new TransformException("Unable to read XML document", e);
        } else if (e instanceof IOException) {
          throw new TransformException("Error reading template", e);
        } else if (e instanceof TransformException) {
          throw (TransformException) e;
        } else {
          throw new TransformException("Unhandled exception", e);
        }
      }

      output = unsyncStringWriter.toString();
    }

    // Postprocess output

    for (TransformerListener transformerListener : transformerListeners) {

      // Modify output

      if (_logOutputBeforeListener.isDebugEnabled()) {
        _logOutputBeforeListener.debug(output);
      }

      output = transformerListener.onOutput(output, languageId, tokens);

      if (_logOutputAfterListener.isDebugEnabled()) {
        _logOutputAfterListener.debug(output);
      }
    }

    if (_logTransfromAfter.isDebugEnabled()) {
      _logTransfromAfter.debug(output);
    }

    return output;
  }
示例#17
0
  public String transform(
      ThemeDisplay themeDisplay, Map<String, Object> contextObjects, String script, String langType)
      throws Exception {

    if (Validator.isNull(langType)) {
      return null;
    }

    long companyId = 0;
    long companyGroupId = 0;
    long scopeGroupId = 0;
    long siteGroupId = 0;

    if (themeDisplay != null) {
      companyId = themeDisplay.getCompanyId();
      companyGroupId = themeDisplay.getCompanyGroupId();
      scopeGroupId = themeDisplay.getScopeGroupId();
      siteGroupId = themeDisplay.getSiteGroupId();
    }

    String templateId = String.valueOf(contextObjects.get("template_id"));

    templateId = getTemplateId(templateId, companyId, companyGroupId, scopeGroupId);

    Template template = getTemplate(templateId, script, langType);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    try {
      if (contextObjects != null) {
        for (String key : contextObjects.keySet()) {
          template.put(key, contextObjects.get(key));
        }
      }

      template.put("company", getCompany(themeDisplay, companyId));
      template.put("companyId", companyId);
      template.put("device", getDevice(themeDisplay));

      String templatesPath = getTemplatesPath(companyId, scopeGroupId);

      template.put("journalTemplatesPath", templatesPath);
      template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
      template.put(
          "randomNamespace", PwdGenerator.getPassword(PwdGenerator.KEY3, 4) + StringPool.UNDERLINE);
      template.put("scopeGroupId", scopeGroupId);
      template.put("siteGroupId", siteGroupId);
      template.put("templatesPath", templatesPath);

      // Deprecated variables

      template.put("groupId", scopeGroupId);

      mergeTemplate(template, unsyncStringWriter);
    } catch (Exception e) {
      throw new TransformException("Unhandled exception", e);
    }

    return unsyncStringWriter.toString();
  }
  @Test(expected = TemplateException.class)
  public void testProcessMultiTemplateWithoutNamespace() throws Exception {
    Template template = _soyManagerTestHelper.getTemplate(Collections.singletonList("simple.soy"));

    template.processTemplate(new UnsyncStringWriter());
  }
示例#19
0
  public String transform(
      ThemeDisplay themeDisplay,
      Map<String, String> tokens,
      String viewMode,
      String languageId,
      String xml,
      String script,
      String langType)
      throws Exception {

    // Setup listeners

    if (_log.isDebugEnabled()) {
      _log.debug("Language " + languageId);
    }

    if (Validator.isNull(viewMode)) {
      viewMode = Constants.VIEW;
    }

    if (_logTokens.isDebugEnabled()) {
      String tokensString = PropertiesUtil.list(tokens);

      _logTokens.debug(tokensString);
    }

    if (_logTransformBefore.isDebugEnabled()) {
      _logTransformBefore.debug(xml);
    }

    List<TransformerListener> transformerListeners = new ArrayList<TransformerListener>();

    for (String transformerListenersClassName : _transformerListenerClassNames) {

      TransformerListener transformerListener = null;

      try {
        if (_log.isDebugEnabled()) {
          _log.debug("Instantiate listener " + transformerListenersClassName);
        }

        ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();

        transformerListener =
            (TransformerListener)
                InstanceFactory.newInstance(classLoader, transformerListenersClassName);

        transformerListeners.add(transformerListener);
      } catch (Exception e) {
        _log.error(e, e);
      }

      // Modify XML

      if (_logXmlBeforeListener.isDebugEnabled()) {
        _logXmlBeforeListener.debug(xml);
      }

      if (transformerListener != null) {
        xml = transformerListener.onXml(xml, languageId, tokens);

        if (_logXmlAfterListener.isDebugEnabled()) {
          _logXmlAfterListener.debug(xml);
        }
      }

      // Modify script

      if (_logScriptBeforeListener.isDebugEnabled()) {
        _logScriptBeforeListener.debug(script);
      }

      if (transformerListener != null) {
        script = transformerListener.onScript(script, xml, languageId, tokens);

        if (_logScriptAfterListener.isDebugEnabled()) {
          _logScriptAfterListener.debug(script);
        }
      }
    }

    // Transform

    String output = null;

    if (Validator.isNull(langType)) {
      output = LocalizationUtil.getLocalization(xml, languageId);
    } else {
      long companyId = 0;
      long companyGroupId = 0;
      long articleGroupId = 0;

      if (tokens != null) {
        companyId = GetterUtil.getLong(tokens.get("company_id"));
        companyGroupId = GetterUtil.getLong(tokens.get("company_group_id"));
        articleGroupId = GetterUtil.getLong(tokens.get("article_group_id"));
      }

      long scopeGroupId = 0;
      long siteGroupId = 0;

      if (themeDisplay != null) {
        companyId = themeDisplay.getCompanyId();
        companyGroupId = themeDisplay.getCompanyGroupId();
        scopeGroupId = themeDisplay.getScopeGroupId();
        siteGroupId = themeDisplay.getSiteGroupId();
      }

      String templateId = tokens.get("template_id");

      templateId = getTemplateId(templateId, companyId, companyGroupId, articleGroupId);

      Template template = getTemplate(templateId, tokens, languageId, xml, script, langType);

      UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

      try {
        if (Validator.isNotNull(xml)) {
          Document document = SAXReaderUtil.read(xml);

          Element rootElement = document.getRootElement();

          List<TemplateNode> templateNodes = getTemplateNodes(themeDisplay, rootElement);

          if (templateNodes != null) {
            for (TemplateNode templateNode : templateNodes) {
              template.put(templateNode.getName(), templateNode);
            }
          }

          Element requestElement = rootElement.element("request");

          template.put("request", insertRequestVariables(requestElement));

          template.put("xmlRequest", requestElement.asXML());
        }

        template.put("articleGroupId", articleGroupId);
        template.put("company", getCompany(themeDisplay, companyId));
        template.put("companyId", companyId);
        template.put("device", getDevice(themeDisplay));

        String templatesPath = getTemplatesPath(companyId, articleGroupId);

        template.put("journalTemplatesPath", templatesPath);

        Locale locale = LocaleUtil.fromLanguageId(languageId);

        template.put("locale", locale);

        template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
        template.put(
            "randomNamespace",
            PwdGenerator.getPassword(PwdGenerator.KEY3, 4) + StringPool.UNDERLINE);
        template.put("scopeGroupId", scopeGroupId);
        template.put("siteGroupId", siteGroupId);
        template.put("templatesPath", templatesPath);
        template.put("viewMode", viewMode);

        // Deprecated variables

        template.put("groupId", articleGroupId);

        mergeTemplate(template, unsyncStringWriter);
      } catch (Exception e) {
        if (e instanceof DocumentException) {
          throw new TransformException("Unable to read XML document", e);
        } else if (e instanceof IOException) {
          throw new TransformException("Error reading template", e);
        } else if (e instanceof TransformException) {
          throw (TransformException) e;
        } else {
          throw new TransformException("Unhandled exception", e);
        }
      }

      output = unsyncStringWriter.toString();
    }

    // Postprocess output

    for (TransformerListener transformerListener : transformerListeners) {

      // Modify output

      if (_logOutputBeforeListener.isDebugEnabled()) {
        _logOutputBeforeListener.debug(output);
      }

      output = transformerListener.onOutput(output, languageId, tokens);

      if (_logOutputAfterListener.isDebugEnabled()) {
        _logOutputAfterListener.debug(output);
      }
    }

    if (_logTransfromAfter.isDebugEnabled()) {
      _logTransfromAfter.debug(output);
    }

    return output;
  }