1 package org.jahia.services.htmleditors.fckeditor; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 6 import javax.servlet.ServletException ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 10 import org.apache.commons.lang.StringEscapeUtils; 11 import org.jahia.bin.Jahia; 12 import org.jahia.data.viewhelper.sitemap.SiteMapViewHelper; 13 import org.jahia.data.viewhelper.sitemap.TreeSiteMapViewHelper; 14 import org.jahia.exceptions.JahiaException; 15 import org.jahia.params.ParamBean; 16 import org.jahia.registries.ServicesRegistry; 17 import org.jahia.services.pages.ContentPage; 18 import org.jahia.services.pages.JahiaPage; 19 import org.jahia.services.sitemap.JahiaSiteMapService; 20 21 26 27 public class FCKeditorSiteMapConnector extends FCKeditorBaseConnector 28 { 29 30 protected void appendPage(int id, int parentId, String title, String url, 31 StringBuffer out) 32 { 33 out.append("<Page id=\"").append(id).append("\" pid=\"").append(parentId) 34 .append("\" title=\"") 35 .append(StringEscapeUtils.escapeXml(StringEscapeUtils.escapeHtml(title))) 38 .append("\" url=\"").append(StringEscapeUtils.escapeXml(url)).append( 39 "\"/>"); 40 } 41 42 protected void appendSiteMapPages(String jahiaPath, int rootPageId, 43 ParamBean jParams, StringBuffer out) throws JahiaException 44 { 45 appendPage(0, -1, "root", "/", out); 46 String languageCode = jParams.getLocale().toString(); 47 JahiaSiteMapService siteMapService = ServicesRegistry.getInstance() 48 .getJahiaSiteMapService(); 49 50 ContentPage theContentPage = ServicesRegistry.getInstance() 51 .getJahiaPageService().lookupContentPage(rootPageId, false); 52 53 SiteMapViewHelper siteMapViewHelper = siteMapService 54 .getTreeSiteMapViewHelper(jParams.getUser(), theContentPage, jParams 55 .getSessionID(), getPageInfoFlags(), languageCode, 56 getLevelsToDisplay(), false, jParams); 57 58 if (siteMapViewHelper == null) 59 return; 60 61 TreeSiteMapViewHelper treeJahiaSiteMap = (TreeSiteMapViewHelper)siteMapViewHelper; 62 63 boolean reachablePage = false; 64 65 for (int i = 0; i < treeJahiaSiteMap.size(); i++) 66 { 67 ContentPage contentPage = treeJahiaSiteMap.getContentPage(i); 68 if (contentPage == null) 69 continue; 70 71 reachablePage = contentPage.isReachable(jParams.getOperationMode(), 72 jParams.getEntryLoadRequest().getFirstLocale(true).toString(), jParams 73 .getUser()); 74 if (reachablePage) 75 { 76 String pageTitle = treeJahiaSiteMap.getPageTitle(i, languageCode); 77 JahiaPage thePage = contentPage.getPage(jParams); 78 if (thePage != null) 79 { 80 int pageId = thePage.getID(); 81 String pageUrl = thePage.getRemoteURL(); 82 if (pageUrl == null || pageUrl.length() == 0 83 || pageUrl.equals("http://")) 84 { 85 if (disableCacheForPage(thePage, jParams)) 86 pageUrl = new StringBuffer (64).append(jahiaPath).append("/site/") 87 .append(jParams.getSiteKey()).append("/cache/bypass/pid/") 88 .append(pageId).toString(); 89 else 90 pageUrl = new StringBuffer (64).append(jahiaPath).append("/site/") 91 .append(jParams.getSiteKey()).append("/pid/").append(pageId) 92 .toString(); 93 } 94 95 appendPage(treeJahiaSiteMap.getPageID(i), treeJahiaSiteMap 96 .getParentPageID(i), pageTitle, pageUrl, out); 97 } 98 } 99 } 100 } 101 102 111 protected boolean disableCacheForPage(JahiaPage thePage, ParamBean jParams) 112 { 113 return false; 114 } 115 116 122 public void doGet(HttpServletRequest request, HttpServletResponse response) 123 throws ServletException , IOException 124 { 125 String rootPageIdParam = request.getParameter("rootPageId"); 126 int rootPageId = 0; 127 if (rootPageIdParam != null) 128 { 129 try 130 { 131 rootPageId = Integer.parseInt(rootPageIdParam); 132 } 133 catch (NumberFormatException ex) 134 { 135 logger.warn("Unable to parse root page ID for the Site Map." 136 + " Using Site's home page as root", ex); 137 } 138 } 139 140 if (logger.isDebugEnabled()) 141 logger.debug("Start bulding Site Map using root page ID " + rootPageId); 142 143 StringBuffer outBuffer = new StringBuffer (10240); 144 outBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); 145 146 ParamBean jParams = getJahiaParamBean(request, response, false); 147 148 if (rootPageId == 0) 150 rootPageId = jParams.getSite().getHomePageID(); 151 152 outBuffer.append("<Connector rootPageId=\"").append(rootPageId).append( 153 "\">"); 154 155 outBuffer.append("<SiteMap>"); 156 try 157 { 158 appendSiteMapPages(Jahia.getContextPath() + Jahia.getServletPath(), 160 rootPageId, jParams, outBuffer); 161 } 162 catch (Exception ex) 163 { 164 logger.error("Unable to load the list of portal pages from server", ex); 165 } 166 finally 167 { 168 releaseResources(); 169 } 170 171 outBuffer.append("</SiteMap></Connector>"); 172 173 if (logger.isDebugEnabled()) 174 logger.debug("Site Map built: " + outBuffer.toString()); 175 176 response.setContentType("text/xml; charset=UTF-8"); 177 response.setHeader("Cache-Control", "no-cache"); 178 PrintWriter out = response.getWriter(); 179 out.print(outBuffer.toString()); 180 out.flush(); 181 out.close(); 182 } 183 184 190 public void doPost(HttpServletRequest request, HttpServletResponse response) 191 throws IOException 192 { 193 response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); 194 } 195 196 201 protected int getLevelsToDisplay() 202 { 203 return SiteMapViewHelper.DISPLAY_ALL_LEVEL; 204 } 205 206 212 protected int getPageInfoFlags() 213 { 214 return ContentPage.ACTIVE_PAGE_INFOS; 215 } 216 } 217 | Popular Tags |