KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmleditors > fckeditor > FCKeditorSiteMapConnector


1 package org.jahia.services.htmleditors.fckeditor;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
22  * FCKeditor link browser integration for Jahia Site Map.
23  *
24  * @author Sergiy Shyrkov
25  */

26
27 public class FCKeditorSiteMapConnector extends FCKeditorBaseConnector
28 {
29
30   protected void appendPage(int id, int parentId, String JavaDoc title, String JavaDoc url,
31     StringBuffer JavaDoc out)
32   {
33     out.append("<Page id=\"").append(id).append("\" pid=\"").append(parentId)
34       .append("\" title=\"")
35       // escape title twice: first - for XHTML page, second - for the XML
36
// response
37
.append(StringEscapeUtils.escapeXml(StringEscapeUtils.escapeHtml(title)))
38       .append("\" url=\"").append(StringEscapeUtils.escapeXml(url)).append(
39         "\"/>");
40   }
41
42   protected void appendSiteMapPages(String JavaDoc jahiaPath, int rootPageId,
43     ParamBean jParams, StringBuffer JavaDoc out) throws JahiaException
44   {
45     appendPage(0, -1, "root", "/", out);
46     String JavaDoc 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 JavaDoc pageTitle = treeJahiaSiteMap.getPageTitle(i, languageCode);
77         JahiaPage thePage = contentPage.getPage(jParams);
78         if (thePage != null)
79         {
80           int pageId = thePage.getID();
81           String JavaDoc pageUrl = thePage.getRemoteURL();
82           if (pageUrl == null || pageUrl.length() == 0
83             || pageUrl.equals("http://"))
84           {
85             if (disableCacheForPage(thePage, jParams))
86               pageUrl = new StringBuffer JavaDoc(64).append(jahiaPath).append("/site/")
87                 .append(jParams.getSiteKey()).append("/cache/bypass/pid/")
88                 .append(pageId).toString();
89             else
90               pageUrl = new StringBuffer JavaDoc(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   /**
103    * Returns <code>true</code> if the cache should be disabled for this page
104    * using <code>/cache/bypass/</code>.
105    *
106    * @param thePage the page to check
107    * @param jParams current Jahia ParamBean
108    * @return <code>true</code> if the cache should be disabled for this page
109    * using <code>/cache/bypass/</code>
110    */

111   protected boolean disableCacheForPage(JahiaPage thePage, ParamBean jParams)
112   {
113     return false;
114   }
115
116   /*
117    * (non-Javadoc)
118    *
119    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
120    * javax.servlet.http.HttpServletResponse)
121    */

122   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
123     throws ServletException JavaDoc, IOException JavaDoc
124   {
125     String JavaDoc 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 JavaDoc 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 JavaDoc outBuffer = new StringBuffer JavaDoc(10240);
144     outBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
145
146     ParamBean jParams = getJahiaParamBean(request, response, false);
147
148     // use Site's home page as root if no page ID is available
149
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       // prepare a list of Portal pages
159
appendSiteMapPages(Jahia.getContextPath() + Jahia.getServletPath(),
160         rootPageId, jParams, outBuffer);
161     }
162     catch (Exception JavaDoc 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 JavaDoc out = response.getWriter();
179     out.print(outBuffer.toString());
180     out.flush();
181     out.close();
182   }
183
184   /*
185    * (non-Javadoc)
186    *
187    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
188    * javax.servlet.http.HttpServletResponse)
189    */

190   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
191     throws IOException JavaDoc
192   {
193     response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
194   }
195
196   /**
197    * Return the number of levels to be displayed in a site mp tree.
198    *
199    * @return the number of levels to be displayed in a site mp tree
200    */

201   protected int getLevelsToDisplay()
202   {
203     return SiteMapViewHelper.DISPLAY_ALL_LEVEL;
204   }
205
206   /**
207    * Return the page info flags to determine, which pages will be displayed.
208    *
209    * @return the page info flags to determine, which pages will be displayed
210    * @see ContentPage
211    */

212   protected int getPageInfoFlags()
213   {
214     return ContentPage.ACTIVE_PAGE_INFOS;
215   }
216 }
217
Popular Tags