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();
  }
  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());
  }
  @Override
  public synchronized void fireUndeployEvent(HotDeployEvent hotDeployEvent) {
    for (int i = _hotDeployListeners.size() - 1; i >= 0; i--) {
      HotDeployListener hotDeployListener = _hotDeployListeners.get(i);

      PortletClassLoaderUtil.setServletContextName(hotDeployEvent.getServletContextName());

      try {
        hotDeployListener.invokeUndeploy(hotDeployEvent);
      } catch (HotDeployException hde) {
        _log.error(hde, hde);
      } finally {
        PortletClassLoaderUtil.setServletContextName(null);
      }
    }

    _deployedServletContextNames.remove(hotDeployEvent.getServletContextName());

    ClassLoader classLoader = hotDeployEvent.getContextClassLoader();

    TemplateManagerUtil.destroy(classLoader);

    _pacl.unregister(classLoader);

    RequiredPluginsUtil.startCheckingRequiredPlugins();
  }
Exemple #4
0
  protected Template getTemplate(
      String templateId,
      Map<String, String> tokens,
      String languageId,
      Document document,
      String script,
      String langType)
      throws Exception {

    TemplateResource templateResource = null;

    if (langType.equals(TemplateConstants.LANG_TYPE_XSL)) {
      XSLURIResolver xslURIResolver = new JournalXSLURIResolver(tokens, languageId);

      templateResource =
          new XSLTemplateResource(templateId, script, xslURIResolver, document.asXML());
    } else {
      templateResource = new StringTemplateResource(templateId, script);
    }

    TemplateResource errorTemplateResource = getErrorTemplateResource(langType);

    return TemplateManagerUtil.getTemplate(
        langType, templateResource, errorTemplateResource, _restricted);
  }
  @BeforeClass
  public static void setupClass() throws TemplateException {
    try {
      DBUpgrader.upgrade();
    } catch (Exception e) {
      _log.error(e, e);
    }

    TemplateManagerUtil.init();
  }
  protected Template getTemplate(String templateId, String script, String langType)
      throws Exception {

    TemplateResource templateResource = new StringTemplateResource(templateId, script);

    TemplateResource errorTemplateResource = getErrorTemplateResource(langType);

    return TemplateManagerUtil.getTemplate(
        langType, templateResource, errorTemplateResource, _restricted);
  }
  public Transformer(String errorTemplatePropertyKey, boolean restricted) {
    Set<String> langTypes = TemplateManagerUtil.getSupportedLanguageTypes(errorTemplatePropertyKey);

    for (String langType : langTypes) {
      String errorTemplateId = PropsUtil.get(errorTemplatePropertyKey, new Filter(langType));

      if (Validator.isNotNull(errorTemplateId)) {
        _errorTemplateIds.put(langType, errorTemplateId);
      }
    }

    _restricted = restricted;
  }
  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);
    }
  }
  protected void doRun(String[] ids) throws Exception {

    // Print release information

    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());

    // Portal resiliency

    DistributedRegistry.registerDistributed(
        ComponentConstants.COMPONENT_CONTEXT, Direction.DUPLEX, MatchType.POSTFIX);
    DistributedRegistry.registerDistributed(
        MimeResponse.MARKUP_HEAD_ELEMENT, Direction.DUPLEX, MatchType.EXACT);
    DistributedRegistry.registerDistributed(
        PortletRequest.LIFECYCLE_PHASE, Direction.DUPLEX, MatchType.EXACT);
    DistributedRegistry.registerDistributed(WebKeys.class);

    Intraband intraband = MPIHelperUtil.getIntraband();

    intraband.registerDatagramReceiveHandler(
        SystemDataType.MAILBOX.getValue(), new MailboxDatagramReceiveHandler());

    MessageBus messageBus = (MessageBus) PortalBeanLocatorUtil.locate(MessageBus.class.getName());

    intraband.registerDatagramReceiveHandler(
        SystemDataType.MESSAGE.getValue(), new MessageDatagramReceiveHandler(messageBus));

    intraband.registerDatagramReceiveHandler(
        SystemDataType.PROXY.getValue(), new IntrabandProxyDatagramReceiveHandler());

    intraband.registerDatagramReceiveHandler(
        SystemDataType.RPC.getValue(), new RPCDatagramReceiveHandler());

    // Shutdown hook

    if (_log.isDebugEnabled()) {
      _log.debug("Add shutdown hook");
    }

    Runtime runtime = Runtime.getRuntime();

    runtime.addShutdownHook(new Thread(new ShutdownHook()));

    // Template manager

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize template manager");
    }

    TemplateManagerUtil.init();

    // Indexers

    IndexerRegistryUtil.register(new MBMessageIndexer());
    IndexerRegistryUtil.register(new PluginPackageIndexer());

    // Upgrade

    if (_log.isDebugEnabled()) {
      _log.debug("Upgrade database");
    }

    DBUpgrader.upgrade();

    // Clear locks

    if (_log.isDebugEnabled()) {
      _log.debug("Clear locks");
    }

    try {
      LockLocalServiceUtil.clear();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Unable to clear locks because Lock table does not exist");
      }
    }

    // Messaging

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize message bus");
    }

    MessageSender messageSender =
        (MessageSender) PortalBeanLocatorUtil.locate(MessageSender.class.getName());
    SynchronousMessageSender synchronousMessageSender =
        (SynchronousMessageSender)
            PortalBeanLocatorUtil.locate(SynchronousMessageSender.class.getName());

    MessageBusUtil.init(
        DoPrivilegedUtil.wrap(messageBus),
        DoPrivilegedUtil.wrap(messageSender),
        DoPrivilegedUtil.wrap(synchronousMessageSender));

    // Cluster executor

    ClusterExecutorUtil.initialize();

    if (!SPIUtil.isSPI()) {
      ClusterMasterExecutorUtil.initialize();
    }

    // Ehache bootstrap

    EhcacheStreamBootstrapCacheLoader.start();

    // Scheduler

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize scheduler engine lifecycle");
    }

    SchedulerEngineHelperUtil.initialize();

    // Verify

    if (_log.isDebugEnabled()) {
      _log.debug("Verify database");
    }

    DBUpgrader.verify();

    // Background tasks

    if (!ClusterMasterExecutorUtil.isEnabled()) {
      BackgroundTaskLocalServiceUtil.cleanUpBackgroundTasks();
    }

    // Liferay JspFactory

    JspFactorySwapper.swap();

    // Jericho

    CachedLoggerProvider.install();
  }
Exemple #10
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;
  }
  @Override
  public String renderDDMTemplate(
      HttpServletRequest request,
      HttpServletResponse response,
      DDMTemplate ddmTemplate,
      List<?> entries,
      Map<String, Object> contextObjects)
      throws Exception {

    Transformer transformer = TransformerHolder.getTransformer();

    contextObjects.put(PortletDisplayTemplateConstants.TEMPLATE_ID, ddmTemplate.getTemplateId());
    contextObjects.put(PortletDisplayTemplateConstants.ENTRIES, entries);

    if (!entries.isEmpty()) {
      contextObjects.put(PortletDisplayTemplateConstants.ENTRY, entries.get(0));
    }

    contextObjects.put(PortletDisplayTemplateConstants.LOCALE, request.getLocale());

    contextObjects.put(PortletDisplayTemplateConstants.REQUEST, request);

    RenderRequest renderRequest =
        (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

    contextObjects.put(PortletDisplayTemplateConstants.RENDER_REQUEST, renderRequest);

    RenderResponse renderResponse =
        (RenderResponse) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);

    contextObjects.put(PortletDisplayTemplateConstants.RENDER_RESPONSE, renderResponse);

    if ((renderRequest != null) && (renderResponse != null)) {
      PortletURL currentURL = PortletURLUtil.getCurrent(renderRequest, renderResponse);

      contextObjects.put(PortletDisplayTemplateConstants.CURRENT_URL, currentURL.toString());
    }

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

    contextObjects.put(PortletDisplayTemplateConstants.THEME_DISPLAY, themeDisplay);

    // Custom context objects

    contextObjects.put(TemplateConstants.CLASS_NAME_ID, ddmTemplate.getClassNameId());

    String language = ddmTemplate.getLanguage();

    TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(language);

    TemplateHandler templateHandler =
        TemplateHandlerRegistryUtil.getTemplateHandler(ddmTemplate.getClassNameId());

    templateManager.addContextObjects(contextObjects, templateHandler.getCustomContextObjects());

    // Taglibs

    templateManager.addTaglibApplication(
        contextObjects,
        PortletDisplayTemplateConstants.FREEMARKER_SERVLET_APPLICATION,
        request.getServletContext());

    templateManager.addTaglibRequest(
        contextObjects,
        PortletDisplayTemplateConstants.FREEMARKER_SERVLET_REQUEST,
        request,
        response);

    templateManager.addTaglibFactory(
        contextObjects,
        PortletDisplayTemplateConstants.TAGLIB_LIFERAY_HASH,
        request.getServletContext());

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    templateManager.addTaglibTheme(
        contextObjects,
        "taglibLiferay",
        request,
        new PipingServletResponse(response, unsyncStringWriter));

    contextObjects.put(TemplateConstants.WRITER, unsyncStringWriter);

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

    return transformer.transform(
        themeDisplay, contextObjects, ddmTemplate.getScript(), language, unsyncStringWriter);
  }