Search in sources :

Example 1 with ThemeResource

use of org.pentaho.platform.api.ui.ThemeResource in project pentaho-platform by pentaho.

the class ThemeServlet method handleRequest.

public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        // look for a passed in theme context (content generator, other named area)
        String moduleName = req.getParameter("context");
        OutputStream out = resp.getOutputStream();
        // $NON-NLS-1$
        resp.setContentType("text/javascript");
        // $NON-NLS-1$
        resp.setHeader("Cache-Control", "no-cache");
        IUserSettingService settingsService = PentahoSystem.get(IUserSettingService.class, getPentahoSession(req));
        // NOTE: this code should be kept in sync with that of PentahoWebContextFilter.java
        String activeTheme = (String) getPentahoSession(req).getAttribute("pentaho-user-theme");
        String ua = req.getHeader("User-Agent");
        // check if we're coming from a mobile device, if so, lock to system default (ruby)
        if (!StringUtils.isEmpty(ua) && ua.matches(".*(?i)(iPad|iPod|iPhone|Android).*")) {
            activeTheme = PentahoSystem.getSystemSetting("default-theme", "ruby");
        }
        if (activeTheme == null) {
            try {
                activeTheme = settingsService.getUserSetting("pentaho-user-theme", null).getSettingValue();
            } catch (Exception ignored) {
            // the user settings service is not valid in the agile-bi deployment of the
            // server
            }
            if (activeTheme == null) {
                activeTheme = PentahoSystem.getSystemSetting("default-theme", "ruby");
            }
        }
        out.write(("\n\n// Theming scripts. This file is generated by (" + getClass().getName() + ") and cannot be found on disk\n").getBytes());
        out.write(("var active_theme = \"" + activeTheme + "\";\n\n").getBytes());
        // Build-up JSON graph for system theme.
        JSONObject root = new JSONObject();
        JSONObject themeObject;
        for (String systemThemeName : themeManager.getSystemThemeIds()) {
            Theme theme = themeManager.getSystemTheme(systemThemeName);
            themeObject = new JSONObject();
            root.put(theme.getId(), themeObject);
            themeObject.put("rootDir", theme.getThemeRootDir());
            for (ThemeResource res : theme.getResources()) {
                themeObject.append("resources", res.getLocation());
            }
        }
        out.write(("var core_theme_tree = " + root.toString() + ";\n\n").getBytes());
        out.write("// Inject the theme script to handle the insertion of requested theme resources\n\n".getBytes());
        ModuleThemeInfo moduleThemeinfo = themeManager.getModuleThemeInfo(moduleName);
        if (moduleThemeinfo != null) {
            // Build-up JSON graph for module theme.
            root = new JSONObject();
            for (Theme theme : moduleThemeinfo.getModuleThemes()) {
                themeObject = new JSONObject();
                root.put(theme.getName(), themeObject);
                themeObject.put("rootDir", theme.getThemeRootDir());
                for (ThemeResource res : theme.getResources()) {
                    themeObject.append("resources", res.getLocation());
                }
            }
            out.write(("var module_theme_tree = " + root.toString() + ";\n\n").getBytes());
        }
        // createElement & insertBefore
        out.write(("(function() {\n" + "var script = document.createElement('script');\n" + "script.type = 'text/javascript';\n" + // "script.async = false;\n" +
        "script.src = CONTEXT_PATH + 'js/themeResources.js';\n" + "var existing = document.getElementsByTagName('script')[0];\n" + "existing.parentNode.insertBefore(script, existing);\n" + "}());").getBytes());
    } catch (IOException e) {
        logger.debug("IO exception creating Theme info", e);
        throw new ServletException(e);
    } catch (JSONException e) {
        logger.debug("JSON exception creating Theme info", e);
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) JSONObject(org.json.JSONObject) IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) OutputStream(java.io.OutputStream) ModuleThemeInfo(org.pentaho.platform.api.ui.ModuleThemeInfo) Theme(org.pentaho.platform.api.ui.Theme) ThemeResource(org.pentaho.platform.api.ui.ThemeResource) JSONException(org.json.JSONException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JSONException(org.json.JSONException)

Example 2 with ThemeResource

use of org.pentaho.platform.api.ui.ThemeResource in project pentaho-platform by pentaho.

the class PluginThemeResolver method findPluginThemes.

private void findPluginThemes(String pluginId) {
    try {
        InputStream pluginManifestStream = resLoader.getResourceAsStream(pluginManager.getClassLoader(pluginId), "themes.xml");
        if (pluginManifestStream == null) {
            return;
        }
        ResourceBundle resourceBundle = null;
        try {
            resourceBundle = ResourceBundle.getBundle("themes", LocaleHelper.getDefaultLocale(), pluginManager.getClassLoader(pluginId));
        } catch (Exception ignored) {
        /* optional bundle */
        }
        SAXReader rdr = XMLParserFactoryProducer.getSAXReader(null);
        Document pluginManifest = rdr.read(pluginManifestStream);
        String rootThemeFolder = pluginManifest.getRootElement().attributeValue("root-folder");
        // Plugins supplying styles whether local or system must have a root_theme_folder
        if (rootThemeFolder == null) {
            return;
        }
        // Things look good. Build-up theme information for this plugin.
        ModuleThemeInfo moduleThemeInfo = new ModuleThemeInfo(pluginId);
        List<Element> pluginNodes = pluginManifest.getRootElement().elements();
        for (Element themeNode : pluginNodes) {
            String themeId = themeNode.getName();
            String themeName = StringUtils.defaultIfEmpty(themeNode.attributeValue("display-name"), themeId);
            if (themeName.startsWith("${")) {
                themeName = resourceBundle.getString(themeName);
            }
            Theme theme = new Theme(themeId, themeName, "content/" + pluginId + "/" + rootThemeFolder + "/" + themeId + "/");
            theme.setHidden("true".equals(themeNode.attributeValue("hidden")));
            if ("true".equals(themeNode.attributeValue("system"))) {
                moduleThemeInfo.getSystemThemes().add(theme);
            } else {
                moduleThemeInfo.getModuleThemes().add(theme);
            }
            List<Element> resourceList = themeNode.elements();
            for (Element res : resourceList) {
                theme.addResource(new ThemeResource(theme, res.getText()));
            }
        }
        moduleThemes.put(pluginId, moduleThemeInfo);
    } catch (Exception e) {
        logger.debug("Error parsing plugin themes", e);
    }
}
Also used : InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ModuleThemeInfo(org.pentaho.platform.api.ui.ModuleThemeInfo) Theme(org.pentaho.platform.api.ui.Theme) ThemeResource(org.pentaho.platform.api.ui.ThemeResource) ResourceBundle(java.util.ResourceBundle) Document(org.dom4j.Document)

Example 3 with ThemeResource

use of org.pentaho.platform.api.ui.ThemeResource in project pentaho-platform by pentaho.

the class ThemeManagerIT method testThemes.

@Test
public void testThemes() throws Exception {
    // setup mock context
    MockServletContext context = new MockServletContext();
    context.addResourcePaths("/", Arrays.asList("test-module/"));
    context.addResourcePaths("/test-module/", Arrays.asList("themes.xml"));
    File themesDotXML = new File(getSolutionPath() + "/system/themeplugin/themes.xml");
    context.setResource("/test-module/themes.xml", themesDotXML.toURI().toURL());
    context.setResourceAsStream("/test-module/themes.xml", new FileInputStream(themesDotXML));
    PentahoSystem.getApplicationContext().setContext(context);
    StandaloneSession session = new StandaloneSession();
    PentahoSessionHolder.setSession(session);
    PentahoSystem.get(IPluginManager.class).reload();
    IThemeManager themeManager = PentahoSystem.get(IThemeManager.class);
    assertTrue(themeManager.getSystemThemeIds().contains("core"));
    assertNotNull(themeManager.getModuleThemeInfo("themeplugin"));
    assertEquals(1, themeManager.getModuleThemeInfo("themeplugin").getSystemThemes().size());
    Set<ThemeResource> resources = themeManager.getModuleThemeInfo("themeplugin").getSystemThemes().get(0).getResources();
    assertEquals(3, resources.size());
    assertNotNull(themeManager.getModuleThemeInfo("test-module"));
    assertEquals(1, themeManager.getModuleThemeInfo("test-module").getSystemThemes().size());
    resources = themeManager.getModuleThemeInfo("test-module").getSystemThemes().get(0).getResources();
    assertEquals(3, resources.size());
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPluginManager(org.pentaho.platform.api.engine.IPluginManager) IThemeManager(org.pentaho.platform.api.ui.IThemeManager) ThemeResource(org.pentaho.platform.api.ui.ThemeResource) File(java.io.File) MockServletContext(com.mockrunner.mock.web.MockServletContext) FileInputStream(java.io.FileInputStream) BaseTest(org.pentaho.test.platform.engine.core.BaseTest) Test(org.junit.Test)

Example 4 with ThemeResource

use of org.pentaho.platform.api.ui.ThemeResource in project pentaho-platform by pentaho.

the class StyledHtmlAxisServiceLister method createContent.

@Override
public void createContent(AxisConfiguration axisConfiguration, ConfigurationContext context, OutputStream out) throws Exception {
    // write out the style sheet and the HTML document
    // $NON-NLS-1$
    out.write("<html>\n<head>".getBytes());
    final IPentahoSession session = PentahoSessionHolder.getSession();
    IUserSettingService settingsService = PentahoSystem.get(IUserSettingService.class, session);
    String activeThemeName;
    if (session == null || settingsService == null) {
        activeThemeName = PentahoSystem.getSystemSetting("default-activeThemeName", "onyx");
    } else {
        activeThemeName = StringUtils.defaultIfEmpty((String) session.getAttribute("pentaho-user-activeThemeName"), settingsService.getUserSetting("pentaho-user-activeThemeName", PentahoSystem.getSystemSetting("default-activeThemeName", "onyx")).getSettingValue());
    }
    IThemeManager themeManager = PentahoSystem.get(IThemeManager.class, null);
    Theme theme = themeManager.getSystemTheme(activeThemeName);
    final ServletContext servletContext = (ServletContext) PentahoSystem.getApplicationContext().getContext();
    if (servletContext != null) {
        for (ThemeResource res : theme.getResources()) {
            if (res.getLocation().endsWith(".css")) {
                out.write(("<link rel=\"stylesheet\" href=\"" + PentahoSystem.getApplicationContext().getFullyQualifiedServerURL() + theme.getThemeRootDir() + res.getLocation() + "\">").getBytes());
            }
        }
    }
    // $NON-NLS-1$
    out.write("</head>\n<body>\n".getBytes());
    // get the list of services from the core ListServices
    super.createContent(axisConfiguration, context, out);
    // $NON-NLS-1$
    out.write("\n</html>\n".getBytes());
}
Also used : IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IThemeManager(org.pentaho.platform.api.ui.IThemeManager) Theme(org.pentaho.platform.api.ui.Theme) ServletContext(javax.servlet.ServletContext) ThemeResource(org.pentaho.platform.api.ui.ThemeResource)

Example 5 with ThemeResource

use of org.pentaho.platform.api.ui.ThemeResource in project pentaho-platform by pentaho.

the class ServletContextThemeResolver method findPluginThemes.

private void findPluginThemes(String pluginId) {
    // some implementations return folders without a leading slash, fix it now
    if (pluginId.startsWith("/") == false) {
        pluginId = "/" + pluginId;
    }
    try {
        InputStream pluginManifestStream = context.getResourceAsStream(pluginId + "themes.xml");
        if (pluginManifestStream == null) {
            return;
        }
        ResourceBundle resourceBundle = null;
        try {
            resourceBundle = ResourceBundle.getBundle("themes", LocaleHelper.getDefaultLocale());
        } catch (MissingResourceException ignored) {
        // resource bundles are optional
        }
        // getResourcePaths returns directories with slashes, remove those now
        pluginId = pluginId.replace("/", "");
        SAXReader rdr = XMLParserFactoryProducer.getSAXReader(null);
        Document pluginManifest = rdr.read(pluginManifestStream);
        String rootThemeFolder = pluginManifest.getRootElement().attributeValue("root-folder");
        // Plugins supplying styles whether local or system must have a root_theme_folder
        if (rootThemeFolder == null) {
            return;
        }
        // Things look good. Build-up theme information for this plugin.
        ModuleThemeInfo moduleThemeInfo = new ModuleThemeInfo(pluginId);
        List<Element> pluginNodes = pluginManifest.getRootElement().elements();
        for (Element themeNode : pluginNodes) {
            String themeId = themeNode.getName();
            String themeName = StringUtils.defaultIfEmpty(themeNode.attributeValue("display-name"), themeId);
            if (themeName.startsWith("${") && resourceBundle != null) {
                themeName = resourceBundle.getString(themeName);
            }
            Theme theme = new Theme(themeId, themeName, pluginId + "/" + rootThemeFolder + "/" + themeId + "/");
            theme.setHidden("true".equals(themeNode.attributeValue("hidden")));
            if ("true".equals(themeNode.attributeValue("system"))) {
                moduleThemeInfo.getSystemThemes().add(theme);
            } else {
                moduleThemeInfo.getModuleThemes().add(theme);
            }
            List<Element> resourceList = themeNode.elements();
            for (Element res : resourceList) {
                theme.addResource(new ThemeResource(theme, res.getText()));
            }
        }
        moduleThemes.put(pluginId, moduleThemeInfo);
    } catch (Exception e) {
        logger.debug("Error parsing plugin themes", e);
    }
}
Also used : InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) MissingResourceException(java.util.MissingResourceException) Element(org.dom4j.Element) ModuleThemeInfo(org.pentaho.platform.api.ui.ModuleThemeInfo) Theme(org.pentaho.platform.api.ui.Theme) ThemeResource(org.pentaho.platform.api.ui.ThemeResource) ResourceBundle(java.util.ResourceBundle) Document(org.dom4j.Document) MissingResourceException(java.util.MissingResourceException)

Aggregations

ThemeResource (org.pentaho.platform.api.ui.ThemeResource)5 Theme (org.pentaho.platform.api.ui.Theme)4 ModuleThemeInfo (org.pentaho.platform.api.ui.ModuleThemeInfo)3 InputStream (java.io.InputStream)2 ResourceBundle (java.util.ResourceBundle)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 SAXReader (org.dom4j.io.SAXReader)2 IThemeManager (org.pentaho.platform.api.ui.IThemeManager)2 IUserSettingService (org.pentaho.platform.api.usersettings.IUserSettingService)2 MockServletContext (com.mockrunner.mock.web.MockServletContext)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 MissingResourceException (java.util.MissingResourceException)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1