Example usage for org.springframework.web.context WebApplicationContext getResource

List of usage examples for org.springframework.web.context WebApplicationContext getResource

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getResource.

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:org.kuali.mobility.tags.PageTag.java

/**
 * Adds the theme to the page if there is one available
 *
 * @throws IOException/*w  w  w  .  ja va2 s .c  om*/
 */
void addTheme() throws IOException {
    PageContext pageContext = (PageContext) getJspContext();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext servletContext = pageContext.getServletContext();
    WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Properties kmeProperties = (Properties) ac.getBean("kmeProperties");
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);

    boolean useCampusTheme = kmeProperties != null
            && "true".equals(kmeProperties.getProperty("theme.perCampus", "false"));
    String viewingCampus = user == null ? null : user.getViewCampus();
    String defaultTheme = kmeProperties == null ? null : kmeProperties.getProperty("theme.default");
    String themeToAdd = null;

    // 1 - if this tag requests a theme, we attempt to use it
    if (!StringUtils.isEmpty(this.theme) && ac.getResource("/css/theme-" + this.theme + ".css").exists()) {
        themeToAdd = this.theme;
    }
    // 2 - Try and use campus theme if configured to use it
    if (themeToAdd == null && useCampusTheme && !StringUtils.isEmpty(viewingCampus)
            && ac.getResource("/css/theme-" + viewingCampus + ".css").exists()) {
        themeToAdd = viewingCampus;
    }
    // 3 - Use default theme
    if (themeToAdd == null && !StringUtils.isEmpty(defaultTheme)
            && ac.getResource("/css/theme-" + defaultTheme + ".css").exists()) {
        themeToAdd = defaultTheme;
    }

    if (themeToAdd != null) {
        pageContext.getOut().println("<link href=\"" + request.getContextPath() + "/css/theme-" + themeToAdd
                + ".css\" rel=\"stylesheet\" type=\"text/css\" />");
    }
}