Skip to content

Instantly share code, notes, and snippets.

@chrismrgn
Created July 14, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismrgn/219443179e0ea4f5b1deb6717e94b148 to your computer and use it in GitHub Desktop.
Save chrismrgn/219443179e0ea4f5b1deb6717e94b148 to your computer and use it in GitHub Desktop.
Controller to handle Navigation in DD4T 2 Java
@Controller
public class NavigationController {
private static final Logger LOG = LoggerFactory.getLogger(NavigationController.class);
private SitemapFactory sitemapFactory;
private PublicationResolver publicationResolver;
private Sitemap sitemap;
private String navigationViewPath = "";
@RequestMapping(value = "/mainNavigation", method = { GET })
public String mainNavigation(Model model, final HttpServletRequest request, final HttpServletResponse response)
throws IOException, FactoryException {
//Get current page
final Page currentPage = (Page) request.getAttribute(Constants.PAGE_MODEL_KEY);
//TODO: Extract to configuration
String url = "/ww/en/sitemap.json";
try {
if (StringUtils.isEmpty(url)) {
// url is not valid, throw an ItemNotFoundException
throw new ItemNotFoundException("Navigation Url was empty or could not be resolved.");
}
Sitemap navigationModel = sitemapFactory.findSitemapByUrl(url, publicationResolver.getPublicationId(), sitemap.getClass());
model.addAttribute("navigationModel", navigationModel);
return navigationViewPath+"main-navigation";
} catch (ItemNotFoundException e) {
LOG.trace(e.getLocalizedMessage(), e);
LOG.warn("Navigation with url '{}' could not be found.", url);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (FactoryException e) {
if (e.getCause() instanceof ItemNotFoundException) {
LOG.warn("Navigation with url '{}' could not be found.", url);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} else {
LOG.error("Factory Error.", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
return null;
}
public String getNavigationViewPath () {
return navigationViewPath;
}
public void setNavigationViewPath (final String navigationViewPath) {
this.navigationViewPath = navigationViewPath;
}
public PublicationResolver getPublicationResolver () {
return publicationResolver;
}
public void setPublicationResolver (final PublicationResolver publicationResolver) {
this.publicationResolver = publicationResolver;
}
public SitemapFactory getSitemapFactory() {
return sitemapFactory;
}
public void setSitemapFactory(final SitemapFactory sitemapFactory) {
this.sitemapFactory = sitemapFactory;
}
public Sitemap getSitemap() {
return sitemap;
}
public void setSitemap(Sitemap sitemap) {
this.sitemap = sitemap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment