Instantly share code, notes, and snippets.
Created Jul 14, 2016
Controller to handle Navigation in DD4T 2 Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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