@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);
        }
      }
    }
  }
  protected void exportTheme(LayoutSet layoutSet, ZipWriter zipWriter) throws Exception {

    Theme theme = layoutSet.getTheme();

    String lookAndFeelXML =
        ContentUtil.get("com/liferay/portal/dependencies/liferay-look-and-feel.xml.tmpl");

    lookAndFeelXML =
        StringUtil.replace(
            lookAndFeelXML,
            new String[] {"[$TEMPLATE_EXTENSION$]", "[$VIRTUAL_PATH$]"},
            new String[] {theme.getTemplateExtension(), theme.getVirtualPath()});

    String servletContextName = theme.getServletContextName();

    ServletContext servletContext = ServletContextPool.get(servletContextName);

    if (servletContext == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("Servlet context not found for theme " + theme.getThemeId());
      }

      return;
    }

    File themeZip = new File(zipWriter.getPath() + "/theme.zip");

    ZipWriter themeZipWriter = ZipWriterFactoryUtil.getZipWriter(themeZip);

    themeZipWriter.addEntry("liferay-look-and-feel.xml", lookAndFeelXML);

    File cssPath = null;
    File imagesPath = null;
    File javaScriptPath = null;
    File templatesPath = null;

    if (!theme.isLoadFromServletContext()) {
      ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(servletContextName);

      if (themeLoader == null) {
        _log.error(servletContextName + " does not map to a theme loader");
      } else {
        String realPath =
            themeLoader.getFileStorage().getPath() + StringPool.SLASH + theme.getName();

        cssPath = new File(realPath + "/css");
        imagesPath = new File(realPath + "/images");
        javaScriptPath = new File(realPath + "/javascript");
        templatesPath = new File(realPath + "/templates");
      }
    } else {
      cssPath = new File(servletContext.getRealPath(theme.getCssPath()));
      imagesPath = new File(servletContext.getRealPath(theme.getImagesPath()));
      javaScriptPath = new File(servletContext.getRealPath(theme.getJavaScriptPath()));
      templatesPath = new File(servletContext.getRealPath(theme.getTemplatesPath()));
    }

    exportThemeFiles("css", cssPath, themeZipWriter);
    exportThemeFiles("images", imagesPath, themeZipWriter);
    exportThemeFiles("javascript", javaScriptPath, themeZipWriter);
    exportThemeFiles("templates", templatesPath, themeZipWriter);
  }