1 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 ; 32 import javax.transaction.TransactionManager ; 33 import java.io.IOException ; 34 import java.io.PrintWriter ; 35 import java.util.Map ; 36 import java.util.regex.Matcher ; 37 38 45 public class CMSPortlet extends JBossPortlet 46 { 47 48 private static Logger log = Logger.getLogger(CMSPortlet.class); 49 50 protected String 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 60 { 61 NodeFactory factory = CMS.getCMS().getNodeFactory(); 63 64 try 65 { 66 String path = req.getParameter(CoreConstants.REQ_ACTION_PARAM_PATH); 68 69 if (path == null) 71 { 72 path = "/"; 73 } 74 75 String uri = rootDir + "/" + req.getPortalContext().getProperty(PortalConstants.PORTAL_PROP_NAME_KEY) + path; 77 78 Transactions.begin(factory.getTransactionManager()); 80 81 Content content = null; 83 try 84 { 85 Node currentNode = factory.getNode(uri); 86 if (currentNode.isDir()) 87 { 88 Map 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 content = currentNode.getContent(); 101 } 102 } 103 catch (NoSuchURIException e) 104 { 105 } 106 107 if (content != null) 109 { 110 final String contentType = content.getMimeType(); 112 if (req.getResponseContentType().equalsIgnoreCase(contentType)) 113 { 114 req.getPortletSession().setAttribute("uri", uri.substring(rootDir.length())); 116 } 117 else 118 { 119 resp.sendBytes(contentType, content.getBytes()); 121 } 122 } 123 else 124 { 125 req.getPortletSession().setAttribute("notfound", "true"); 126 } 127 } 128 catch (Exception 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 req.setIdempotent(Boolean.TRUE); 140 } 141 142 150 protected void doView(final JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException , UnavailableException 151 { 152 NodeFactory factory = CMS.getCMS().getNodeFactory(); 154 TransactionManager tm = factory.getTransactionManager(); 155 156 String 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 )req.getPortletSession().getAttribute("uri"); 166 if (uri == null) { 168 uri = "/default/index.html"; 169 } 170 } 171 172 String html = null; 174 175 if (uri != null) 177 { 178 try 179 { 180 Transactions.begin(tm); 181 Node node = factory.getNode(rootDir + uri); 182 html = new String (node.getContent().getBytes()); 183 } 184 catch (NoSuchURIException e) 185 { 186 } 188 catch (Exception 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 contextPath = (String )req.getAttribute(CoreConstants.REQ_ATT_CONTEXT_PATH); 203 String servletPath = (String )req.getAttribute(CoreConstants.REQ_ATT_SERVLET_PATH); 204 String baseURI = contextPath + servletPath + uri.substring(0, lastSlash) + "/"; 205 StringBuffer buffer = new StringBuffer (); 206 Matcher m = WebDAVUtil.RELATIVE_URI_PATTERN.matcher(html); 207 while(m.find()) 208 { 209 String relURI = m.group(3) != null ? m.group(3) : m.group(4); 210 if (!relURI.startsWith("mailto")) 211 { 212 String absoluteURI = baseURI + relURI; 213 m.appendReplacement(buffer, "$1\"" + absoluteURI + "\""); 214 } 215 } 216 m.appendTail(buffer); 217 html = buffer.toString(); 218 } 219 else 220 { 221 } 223 } 224 } 225 226 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 writer = resp.getWriter(); 234 writer.print(html); 235 writer.close(); 236 } 237 238 246 protected void doHelp(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException 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 264 public void render(JBossRenderRequest req, JBossRenderResponse resp) throws PortletException, IOException 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 |