1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.jcr;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.Property;
25 import javax.jcr.Session;
26 import javax.servlet.RequestDispatcher;
27 import javax.servlet.ServletConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.exoplatform.services.log.Log;
35 import org.exoplatform.container.PortalContainer;
36 import org.exoplatform.container.RootContainer;
37 import org.exoplatform.services.cms.templates.TemplateService;
38 import org.exoplatform.services.jcr.RepositoryService;
39 import org.exoplatform.services.log.ExoLogger;
40
41 @SuppressWarnings({"serial","unused"})
42 public class RssServlet extends HttpServlet {
43 private static final Log LOG = ExoLogger.getLogger(RssServlet.class.getName());
44 public void init(ServletConfig config) throws ServletException {}
45 public void service(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 response.setHeader("Cache-Control", "private max-age=600, s-maxage=120");
48 String wsName = null ;
49 String path = null ;
50 String portalName = null ;
51 String pathInfo = request.getPathInfo() ;
52 portalName = pathInfo.substring(1, pathInfo.indexOf("/", 1)) ;
53 wsName = pathInfo.substring(portalName.length() + 2, pathInfo.indexOf("/", portalName.length() + 2)) ;
54 path = pathInfo.substring(pathInfo.indexOf(wsName)+ wsName.length() + 1);
55 PortalContainer pcontainer = RootContainer.getInstance().getPortalContainer(portalName);
56 PortalContainer.setInstance(pcontainer) ;
57 RepositoryService repositoryService =
58 (RepositoryService)pcontainer.getComponentInstanceOfType(RepositoryService.class);
59 TemplateService tservice =
60 (TemplateService)pcontainer.getComponentInstanceOfType(TemplateService.class);
61 Session session = null ;
62 try{
63 session = repositoryService.getCurrentRepository().getSystemSession(wsName) ;
64 String repositoryName = repositoryService.getCurrentRepository().getConfiguration().getName();
65 Node rootNode = session.getRootNode() ;
66 Node file = null ;
67 if(rootNode.hasNode(path))
68 file = rootNode.getNode(path) ;
69 else if(rootNode.hasNode(path.substring(0, path.lastIndexOf("/")))){
70 Node parentNode = rootNode.getNode(path.substring(0, path.lastIndexOf("/"))) ;
71 String name = path.substring(path.lastIndexOf("/") + 1) ;
72 if(name.indexOf(".") > -1 && parentNode.hasNode(name.substring(0, name.indexOf("."))))
73 file = parentNode.getNode(name.substring(0, name.indexOf("."))) ;
74 }
75 if (file == null) throw new Exception("Node " + path + " not found. ");
76 Node content;
77 if(file.isNodeType("nt:file")) {
78 content = file.getNode("jcr:content");
79 Property data = content.getProperty("jcr:data");
80 String mimeType = content.getProperty("jcr:mimeType").getString();
81 response.setContentType(mimeType) ;
82 InputStream is = data.getStream();
83 byte[] buf = new byte[is.available()];
84 is.read(buf);
85 ServletOutputStream os = response.getOutputStream();
86 os.write(buf);
87 } else if (file.isNodeType("exo:rss-enable")){
88 List documentNodeType = tservice.getDocumentTemplates() ;
89 String nodeType = file.getPrimaryNodeType().getName() ;
90 if(documentNodeType.contains(nodeType)){
91 String templateName = tservice.getTemplatePath(false, nodeType, "view1") ;
92 request.setAttribute("portalName", portalName) ;
93 request.setAttribute("wsName", wsName) ;
94 request.setAttribute("templateName", "jcr:"+templateName) ;
95 request.setAttribute("curNode", file) ;
96 RequestDispatcher rd = request.getRequestDispatcher("/viewcontent");
97 rd.forward(request, response);
98 }else {
99 throw new Exception("This node type is not document node");
100 }
101 } else throw new Exception("Invalid node type, expected nt:file or exo:rss-enable type");
102 }catch(Exception e) {
103 if (LOG.isErrorEnabled()) {
104 LOG.error("Unexpected error", e);
105 }
106 throw new ServletException(e) ;
107 }finally{
108 if(session != null) {
109 session.logout() ;
110 }
111 }
112 }
113 }