KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > portlet > cms > CMSPortlet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.portlet.cms;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.portal.cms.Node;
13 import org.jboss.portal.cms.NoSuchURIException;
14 import org.jboss.portal.cms.CMS;
15 import org.jboss.portal.cms.NodeFactory;
16 import org.jboss.portal.cms.Content;
17 import org.jboss.portal.core.CoreConstants;
18 import org.jboss.portal.server.PortalConstants;
19 import org.jboss.portal.common.transaction.Transactions;
20 import org.jboss.portlet.JBossPortlet;
21 import org.jboss.portlet.JBossActionRequest;
22 import org.jboss.portlet.JBossActionResponse;
23 import org.jboss.portlet.JBossRenderResponse;
24 import org.jboss.portlet.JBossRenderRequest;
25
26 import javax.portlet.PortletSecurityException;
27 import javax.portlet.PortletException;
28 import javax.portlet.UnavailableException;
29 import javax.portlet.PortletRequestDispatcher;
30 import javax.portlet.WindowState;
31 import javax.transaction.Transaction JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.regex.Matcher JavaDoc;
37
38 /**
39  * This portlet handles content serving to the client in a dynamic way.
40  *
41  * @author Roy Russo : roy at jboss dot org
42  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
43  * @version $Revision: 1.18 $
44  */

45 public class CMSPortlet extends JBossPortlet
46 {
47
48    private static Logger log = Logger.getLogger(CMSPortlet.class);
49
50    //
51
protected String JavaDoc rootDir;
52
53    public void init() throws PortletException
54    {
55       rootDir = getPortletContext().getInitParameter("rootdir");
56    }
57
58
59    public void processAction(final JBossActionRequest req, final JBossActionResponse resp) throws PortletException, PortletSecurityException, IOException JavaDoc
60    {
61       //
62
NodeFactory factory = CMS.getCMS().getNodeFactory();
63
64       try
65       {
66          // Get the path
67
String JavaDoc path = req.getParameter(CoreConstants.REQ_ACTION_PARAM_PATH);
68
69          // If it is not specified use / as starting point
70
if (path == null)
71          {
72             path = "/";
73          }
74
75          // The URI to look into slide
76
String JavaDoc uri = rootDir + "/" + req.getPortalContext().getProperty(PortalConstants.PORTAL_PROP_NAME_KEY) + path;
77
78          // Start slide tx
79
Transactions.begin(factory.getTransactionManager());
80
81          // The content node if any
82
Content content = null;
83          try
84          {
85             Node currentNode = factory.getNode(uri);
86             if (currentNode.isDir())
87             {
88                // If it is a directory look into the index.html file
89
Map JavaDoc children = currentNode.getChildren();
90                Node indexNode = (Node)children.get("index.html");
91                if (indexNode != null && !indexNode.isDir())
92                {
93                   uri = indexNode.getURI();
94                   content = indexNode.getContent();
95                }
96             }
97             else
98             {
99                // Otherwise use the node as is
100
content = currentNode.getContent();
101             }
102          }
103          catch (NoSuchURIException e)
104          {
105          }
106
107          // Finish the job
108
if (content != null)
109          {
110             // According to the content type stream or display
111
final String JavaDoc contentType = content.getMimeType();
112             if (req.getResponseContentType().equalsIgnoreCase(contentType))
113             {
114                // Display in render
115
req.getPortletSession().setAttribute("uri", uri.substring(rootDir.length()));
116             }
117             else
118             {
119                // Stream
120
resp.sendBytes(contentType, content.getBytes());
121             }
122          }
123          else
124          {
125             req.getPortletSession().setAttribute("notfound", "true");
126          }
127       }
128       catch (Exception JavaDoc e)
129       {
130          log.error("An exception occured in the action cms portlet", e);
131          req.setAttribute("notfound", "true");
132       }
133       finally
134       {
135          Transactions.safeEnd(factory.getTransactionManager());
136       }
137
138       //
139
req.setIdempotent(Boolean.TRUE);
140    }
141
142    /**
143     * Handles render requests when in VIEW mode. Will instantiate a connection to the webdav server, unless one exists.
144     *
145     * @param req
146     * @param resp
147     * @throws javax.portlet.PortletException
148     * @throws IOException
149     */

150    protected void doView(final JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException JavaDoc, UnavailableException
151    {
152       //
153
NodeFactory factory = CMS.getCMS().getNodeFactory();
154       TransactionManager JavaDoc tm = factory.getTransactionManager();
155
156       //
157
String JavaDoc uri = null;
158       if (req.getPortletSession().getAttribute("notfound") != null)
159       {
160          req.getPortletSession().removeAttribute("notfound");
161          uri = "/errorpages/404.html";
162       }
163       else
164       {
165          uri = (String JavaDoc)req.getPortletSession().getAttribute("uri");
166          if (uri == null) // when session times out, we need to reset the uri.
167
{
168             uri = "/default/index.html";
169          }
170       }
171
172       // The html retrieved from the cms store
173
String JavaDoc html = null;
174
175       // Get content
176
if (uri != null)
177       {
178          try
179          {
180             Transactions.begin(tm);
181             Node node = factory.getNode(rootDir + uri);
182             html = new String JavaDoc(node.getContent().getBytes());
183          }
184          catch (NoSuchURIException e)
185          {
186             // Not found
187
}
188          catch (Exception JavaDoc e)
189          {
190             log.error("An exception occured in the render cms portlet", e);
191          }
192          finally
193          {
194             Transactions.safeEnd(tm);
195          }
196
197          if (html != null)
198          {
199             int lastSlash = uri.lastIndexOf('/');
200             if (lastSlash > 0 && lastSlash < uri.length() - 1)
201             {
202                String JavaDoc contextPath = (String JavaDoc)req.getAttribute(CoreConstants.REQ_ATT_CONTEXT_PATH);
203                String JavaDoc servletPath = (String JavaDoc)req.getAttribute(CoreConstants.REQ_ATT_SERVLET_PATH);
204                String JavaDoc baseURI = contextPath + servletPath + uri.substring(0, lastSlash) + "/";
205                StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
206                Matcher JavaDoc m = WebDAVUtil.RELATIVE_URI_PATTERN.matcher(html);
207                while(m.find())
208                {
209                   String JavaDoc relURI = m.group(3) != null ? m.group(3) : m.group(4);
210                   if (!relURI.startsWith("mailto"))
211                   {
212                      String JavaDoc absoluteURI = baseURI + relURI;
213                      m.appendReplacement(buffer, "$1\"" + absoluteURI + "\"");
214                   }
215                }
216                m.appendTail(buffer);
217                html = buffer.toString();
218             }
219             else
220             {
221                // Does not make sense
222
}
223          }
224       }
225
226       // In case we have very nothing to display
227
if (html == null)
228       {
229          html = "<h2>404 - Page Not Found</h2>Oops! We can't really find the resource you're looking for.";
230       }
231
232       resp.setContentType("text/html");
233       PrintWriter JavaDoc writer = resp.getWriter();
234       writer.print(html);
235       writer.close();
236    }
237
238    /**
239     * Handles render requests when in HELP mode
240     *
241     * @param req
242     * @param resp
243     * @throws javax.portlet.PortletException
244     * @throws IOException
245     */

246    protected void doHelp(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException JavaDoc
247    {
248       if(!WindowState.MINIMIZED.equals(req.getWindowState()))
249       {
250          resp.setContentType("text/html");
251          PortletRequestDispatcher dispatcher = this.getPortletContext().getRequestDispatcher(CMSConstants.CMS_JSP_PATH + "/help.jsp");
252          dispatcher.include(req, resp);
253       }
254    }
255
256    /**
257     * Needed to override to handle default page when portlet is placed in windowstate.normal.
258     *
259     * @param req
260     * @param resp
261     * @throws PortletException
262     * @throws IOException
263     */

264    public void render(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException JavaDoc
265    {
266       resp.setTitle("CMS portlet");
267       WindowState windowState = req.getWindowState();
268       if(!WindowState.MINIMIZED.equals(windowState))
269       {
270          doDispatch(req, resp);
271       }
272    }
273 }
274
Popular Tags