Skip to content

Compatibility with org.springframework.web.servlet.view.ContentNegotiatingViewResolver #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package org.springframework.web.servlet.view.mustache;

import com.samskivert.mustache.Template;
import org.springframework.web.servlet.view.AbstractTemplateView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.Writer;
import java.util.Locale;
import java.util.Map;

/**
Expand All @@ -47,4 +48,16 @@ public void setTemplate(MustacheTemplate template) {
public MustacheTemplate getTemplate() {
return template;
}

/**
* Check whether the underlying resource that the configured URL points to
* actually exists.
* @param locale the desired Locale that we're looking for
* @return {@code true} if the resource exists (or is assumed to exist);
* {@code false} if we know that it does not exist
* @throws Exception if the resource exists but is invalid (e.g. could not be parsed)
*/
public boolean checkResource(Locale locale) throws Exception {
return template != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.springframework.web.servlet.view.mustache;

import java.io.FileNotFoundException;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
Expand All @@ -24,7 +29,7 @@
* @author Sean Scanlon <[email protected]>
*/
public class MustacheViewResolver extends AbstractTemplateViewResolver implements ViewResolver {

private static Log LOG = LogFactory.getLog(MustacheViewResolver.class);
private MustacheTemplateFactory templateFactory;

public MustacheViewResolver() {
Expand All @@ -39,14 +44,27 @@ protected Class<?> requiredViewClass() {
@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {

final MustacheView view = (MustacheView) super.buildView(viewName);

final MustacheTemplate template = templateFactory.getTemplate(view.getUrl());
view.setTemplate(template);
MustacheView view = (MustacheView) super.buildView(viewName);

try {
final MustacheTemplate template = templateFactory.getTemplate(view.getUrl());
view.setTemplate(template);
}
// catch( FileNotFoundException ex ){
// LOG.debug("Template [" + viewName + "] not found.");
// }
catch( MustacheTemplateException ex ){
if( ex.getCause() != null && ex.getCause() instanceof FileNotFoundException ){
LOG.debug("Template [" + viewName + "] not found.");
// view = null;
}
else throw ex; // simply rethrow!
}
return view;
}



@Required
public void setTemplateFactory(MustacheTemplateFactory templateFactory) {
this.templateFactory = templateFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
*/
package org.springframework.web.servlet.view.mustache;

import java.io.FileNotFoundException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.web.servlet.View;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;

/**
Expand Down Expand Up @@ -57,4 +64,30 @@ public void testBuildView() throws Exception {
verify(templateFactory).getTemplate(viewName);
}

@Test
public void testBuildViewReturnsNullWhenFileNotFound() throws Exception {
/*
* Test the exception is well trapped in the checkResource()
* used by Spring.
*
*/
doThrow(new MustacheTemplateException(new FileNotFoundException()))
.when(templateFactory).getTemplate(viewName);
View view = viewResolver.buildView(viewName);

/*
* Check the view is a Mustache View!
*/
assertTrue( view instanceof MustacheView);

/*
* If the view does not exists, then the template
* is "null" and the "checkResource" will return
* false.
*
*/
MustacheView mustacheView = (MustacheView)view;
assertNull( mustacheView.getTemplate() );
assertFalse( mustacheView.checkResource(null) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletResponse;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;

/**
Expand Down Expand Up @@ -68,4 +73,7 @@ public void testRenderMergedTemplateModel() throws Exception {

assertEquals(template, view.getTemplate());
}



}