Skip to content

Commit

Permalink
merged with master, added javadocs and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Aug 23, 2014
1 parent 88c9d0b commit 58dc12b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
Expand Up @@ -33,29 +33,47 @@
import org.springframework.webflow.execution.RequestContextHolder;

/**
* ThemeBasedViewResolver is an alternate Spring View Resolver that utilizes a service's
* {@link RegisteredServiceThemeBasedViewResolver} is an alternate Spring View Resolver that utilizes a service's
* associated theme to selectively choose which set of UI views will be used to generate
* the standard views (casLoginView.jsp, casLogoutView.jsp etc).
*
* <p>Views associated with a particular theme by default are expected to be found at:
* {@link #DEFAULT_PATH_PREFIX}/<code>themeId/ui</code>. A starting point may be to
* clone the default set of view pages into a new directory based on the theme id.</p>
*
* <p>Note: There also exists a {@link org.jasig.cas.services.web.ServiceThemeResolver}
* that attempts to resolve the view name based on the service theme id. The difference
* however is that {@link org.jasig.cas.services.web.ServiceThemeResolver} only decorates
* the default view pages with additional tags and coloring, such as CSS and JS. The
* component presented here on the other hand has the ability to load an entirely new
* set of pages that are may be totally different from that of the default's. This
* is specially useful in cases where the set of pages for a theme that are targetted
* for a different type of audience are entirely different structurally that simply
* using the {@link org.jasig.cas.services.web.ServiceThemeResolver} is not practical
* to augment the default views. In such cases, new view pages may be required.</p>
*
* @author John Gasper
* @author Misagh Moayyed
* @since 4.1
*/
public class ThemeBasedViewResolver extends InternalResourceViewResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(ThemeBasedViewResolver.class);
public final class RegisteredServiceThemeBasedViewResolver extends InternalResourceViewResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisteredServiceThemeBasedViewResolver.class);
private static final String DEFAULT_PATH_PREFIX = "/WEB-INF/view/jsp";

/** The ServiceRegistry to look up the service. */
private ServicesManager servicesManager;
private final ServicesManager servicesManager;

private final String pathPrefix = "/WEB-INF/view/jsp";
private final String defaultThemeId;

private String pathPrefix = DEFAULT_PATH_PREFIX;

/**
* The ThemeBasedViewResolver constructor.
* The {@link RegisteredServiceThemeBasedViewResolver} constructor.
* @param defaultThemeId the theme to apply if the service doesn't specific one or a service is not provided
* @param servicesManager the serviceManager implementation
* @see #setCache(boolean)
*/
public ThemeBasedViewResolver(final String defaultThemeId, final ServicesManager servicesManager) {
public RegisteredServiceThemeBasedViewResolver(final String defaultThemeId, final ServicesManager servicesManager) {
super();
super.setCache(false);

Expand All @@ -64,7 +82,7 @@ public ThemeBasedViewResolver(final String defaultThemeId, final ServicesManager
}

/**
* buildView uses the viewName and the theme associated with the service.
* Uses the viewName and the theme associated with the service.
* being requested and returns the appropriate view.
* @param viewName the name of the view to be resolved
* @return a theme-based UrlBasedView
Expand Down Expand Up @@ -104,7 +122,7 @@ protected AbstractUrlBasedView buildView(final String viewName) throws Exception
}

/**
* setCache is not supported in the ThemeBasedViewResourceViewResolver because each
* setCache is not supported in the {@link RegisteredServiceThemeBasedViewResolver} because each
* request must be independently evaluated.
* @param cache a value indicating whether the view should cache results.
*/
Expand All @@ -113,4 +131,15 @@ public void setCache(final boolean cache) {
LOGGER.warn("The {} does not support caching. Turned off caching forcefully.", this.getClass().getSimpleName());
super.setCache(false);
}

/**
* Sets path prefix. This is the location where
* views are expected to be found. The default
* prefix is {@link #DEFAULT_PATH_PREFIX}.
*
* @param pathPrefix the path prefix
*/
public void setPathPrefix(final String pathPrefix) {
this.pathPrefix = pathPrefix;
}
}
Expand Up @@ -39,7 +39,7 @@
*/
public class ThemeBasedViewResolverTests {

private ThemeBasedViewResolver themeBasedViewResolver;
private RegisteredServiceThemeBasedViewResolver registeredServiceThemeBasedViewResolver;

private ServicesManager servicesManager;

Expand All @@ -60,7 +60,7 @@ public void setUp() throws Exception {
r2.setServiceId("myDefaultId");
this.servicesManager.save(r2);

this.themeBasedViewResolver = new ThemeBasedViewResolver("defaultTheme", this.servicesManager);
this.registeredServiceThemeBasedViewResolver = new RegisteredServiceThemeBasedViewResolver("defaultTheme", this.servicesManager);
}

@Test
Expand All @@ -71,7 +71,7 @@ public void testGetServiceWithTheme() throws Exception {
WebApplicationService webApplicationService = new SimpleWebApplicationServiceImpl("myServiceId");
requestContext.getFlowScope().put("service", webApplicationService);

assertEquals("/WEB-INF/view/jsp/myTheme/ui/casLoginView", this.themeBasedViewResolver.buildView("casLoginView").getUrl());
assertEquals("/WEB-INF/view/jsp/myTheme/ui/casLoginView", this.registeredServiceThemeBasedViewResolver.buildView("casLoginView").getUrl());
}

@Test
Expand All @@ -82,14 +82,14 @@ public void testGetServiceWithDefault() throws Exception {
WebApplicationService webApplicationService = new SimpleWebApplicationServiceImpl("myDefaultId");
requestContext.getFlowScope().put("service", webApplicationService);

assertEquals("/WEB-INF/view/jsp/defaultTheme/ui/casLoginView", this.themeBasedViewResolver.buildView("casLoginView").getUrl());
assertEquals("/WEB-INF/view/jsp/defaultTheme/ui/casLoginView", this.registeredServiceThemeBasedViewResolver.buildView("casLoginView").getUrl());
}

@Test
public void testNoService() throws Exception {
MockRequestContext requestContext = new MockRequestContext();
RequestContextHolder.setRequestContext(requestContext);

assertEquals("/WEB-INF/view/jsp/defaultTheme/ui/casLoginView", this.themeBasedViewResolver.buildView("casLoginView").getUrl());
assertEquals("/WEB-INF/view/jsp/defaultTheme/ui/casLoginView", this.registeredServiceThemeBasedViewResolver.buildView("casLoginView").getUrl());
}
}
1 change: 0 additions & 1 deletion cas-server-webapp/src/main/webapp/WEB-INF/cas-servlet.xml
Expand Up @@ -66,7 +66,6 @@
p:prefix="/WEB-INF/view/jsp/"
p:suffix=".jsp"
p:order="1"/>


<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
Expand Down
4 changes: 3 additions & 1 deletion cas-server-webapp/src/main/webapp/WEB-INF/cas.properties
Expand Up @@ -27,7 +27,9 @@ cas.securityContext.status.access=hasIpAddress('127.0.0.1')
cas.securityContext.statistics.access=hasIpAddress('127.0.0.1')

cas.themeResolver.defaultThemeName=cas-theme-default
cas.viewResolver.defaultViewsPathPrefix=/WEB-INF/view/jsp/default/ui/

# Path prefix for where views are to be found
# cas.viewResolver.defaultViewsPathPrefix=/WEB-INF/view/jsp/default/ui/

# Location of the Spring xml config file where views may be collected
# cas.viewResolver.xmlFile=/META-INF/spring/views.xml
Expand Down

0 comments on commit 58dc12b

Please sign in to comment.