1 package net.sf.flock.webapp;
2
3 import java.io.IOException;
4 import java.io.OutputStreamWriter;
5 import java.io.PrintWriter;
6 import java.util.Collection;
7
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletContext;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import net.sf.flock.FilterI;
16 import net.sf.flock.FlockResourceException;
17 import net.sf.flock.SubscriptionManagerI;
18 import net.sf.flock.filter.Limit;
19 import net.sf.flock.filter.MetaStartsWith;
20 import net.sf.flock.filter.SortByDate;
21 import net.sf.flock.support.Rss091Export;
22 import net.sf.flock.support.StylesheetExportFilter;
23
24 import org.jdom.Document;
25 import org.jdom.output.XMLOutputter;
26
27 /***
28 * @version $Revision: 1.19 $
29 * @author $Author: phraktle $
30 */
31 public class FlockServlet extends HttpServlet {
32
33 private final static int MAX_ARTICLES = 20;
34
35 private String defaultStylesheet;
36
37 public void init(ServletConfig config) throws ServletException {
38 super.init(config);
39
40 this.defaultStylesheet = config.getInitParameter("defaultStylesheet");
41 }
42
43 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44 doPost(request, response);
45 }
46
47 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48
49 //System.out.println("pathInfo: "+request.getPathInfo());
50 //System.out.println("servletPath: "+request.getServletPath());
51 String path = request.getPathInfo();
52 if (path==null) {
53 path = "/";
54 } else if (!path.startsWith("/")) {
55 path += "/";
56 }
57
58 String stylesheet = request.getParameter("stylesheet");
59 if (stylesheet==null) {
60 stylesheet = this.defaultStylesheet;
61 }
62
63 //stylesheet = request.getContextPath() + '/' + stylesheet;
64
65 ServletContext ctx = this.getServletContext();
66 FlockContext.setSubscriptionManager( (SubscriptionManagerI) ctx.getAttribute("flock.subscriptionManager") );
67
68 Collection aggregate;
69 try {
70
71 aggregate = FlockContext.getSubscriptionManager().findItems( new FilterI[] {
72 new MetaStartsWith("path", path),
73 new SortByDate(),
74 new Limit(MAX_ARTICLES),
75 });
76
77 } catch (FlockResourceException e) {
78 throw new ServletException(e);
79 }
80 if (aggregate==null) {
81 response.sendError(HttpServletResponse.SC_NOT_FOUND);
82 }
83
84 Document doc = new StylesheetExportFilter( stylesheet ).filter( new Rss091Export().export(path, aggregate) );
85
86 response.setContentType("text/xml; charset=UTF-8");
87 XMLOutputter outputter = new XMLOutputter(" ", true);
88
89 PrintWriter out = new PrintWriter( new OutputStreamWriter(response.getOutputStream(), "UTF8"), true );
90 outputter.output(doc, out);
91 }
92
93 }
This page was automatically generated by Maven