/** * Gets the book for the provided context-relative servlet path or <code>null</code> if no book configured at that path. * The book with the longest prefix match is used. * The servlet path must begin with a slash (/). */ public Book getBook(String servletPath) { if(servletPath.charAt(0) != '/') throw new IllegalArgumentException("Invalid servletPath: " + servletPath); Book longestPrefixBook = null; int longestPrefixLen = -1; for(Book book : getBooks().values()) { String prefix = book.getPathPrefix(); int prefixLen = prefix.length(); if( prefixLen > longestPrefixLen && servletPath.startsWith(prefix) ) { longestPrefixBook = book; longestPrefixLen = prefixLen; } } return longestPrefixBook; }
/** * Finds the path to the current page. * The current page must be in a Book. */ public static PageRef getCurrentPageRef(ServletContext servletContext, HttpServletRequest request) throws ServletException { String pagePath = Dispatcher.getCurrentPagePath(request); Book book = SemanticCMS.getInstance(servletContext).getBook(pagePath); if(book == null) throw new ServletException("Book not found for pagePath: " + pagePath); String bookPrefix = book.getPathPrefix(); if(!pagePath.startsWith(bookPrefix)) throw new AssertionError(); return new PageRef(book, pagePath.substring(bookPrefix.length())); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.reset(); resp.setContentType(CONTENT_TYPE); resp.setCharacterEncoding(ENCODING); PrintWriter out = resp.getWriter(); out.println("<?xml version=\"1.0\" encoding=\"" + ENCODING + "\"?>"); out.println("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); for(Book book : SemanticCMS.getInstance(getServletContext()).getBooks().values()) { out.println(" <sitemap>"); out.print(" <loc>"); ServletUtil.getAbsoluteURL( req, resp.encodeURL(book.getPathPrefix() + SiteMapServlet.SERVLET_PATH), textInXhtmlEncoder, out ); out.println("</loc>"); out.println(" </sitemap>"); } out.println("</sitemapindex>"); } }
@Override public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException { ServletRegistration.Dynamic registration = servletContext.addServlet( SiteMapServlet.class.getName(), SiteMapServlet.class ); for(Book book : SemanticCMS.getInstance(servletContext).getBooks().values()) { registration.addMapping(book.getPathPrefix() + SiteMapServlet.SERVLET_PATH); } } }
/** * Gets the canonical base URL, not including any trailing slash, such as * <code>https://example.com</code> * This is configured in the book via the "canonicalBase" setting. * <p> * TODO: Create central per-request warnings list that could be reported during development mode, include this warning on requests. * TODO: Also could use that for broken link detection instead of throwing exceptions. * </p> */ public static String getCanonicalBase(ServletContext servletContext, HttpServletRequest request, Book book) throws MalformedURLException { String canonicalBase = book.getCanonicalBase(); if(canonicalBase == null) { String autoCanonical = ServletUtil.getAbsoluteURL(request, book.getPathPrefix()); if( // Logger checked first, so if warnings enabled mid-run, will get first warning still logger.isLoggable(Level.WARNING) ) { String bookName = book.getName(); String warningAttribute = CANONICAL_BASE_WARNED_ATTRIBUTE + bookName; if(servletContext.getAttribute(warningAttribute) == null) { servletContext.setAttribute(warningAttribute, true); logger.warning("Using generated canonical base URL, please configure the \"canonicalBase\" setting in the \"" + bookName + "\" book: " + autoCanonical); } } return autoCanonical; } else { return canonicalBase; } }
pageRef = new PageRef( book, combinedPath.substring(book.getPathPrefix().length()) );
String bookPrefix = book.getPathPrefix(); if(bookPrefix.isEmpty()) { encodedBookPath = encodedServletPath;
pagePath.substring(book.getPathPrefix().length()) );
currentBook, ServletUtil.getAbsolutePath( currentPagePath.substring(currentBook.getPathPrefix().length()), path
ServletUtil.getAbsoluteURL( req, resp.encodeURL(book.getPathPrefix() + imageUrl), textInXhtmlEncoder, out