| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package sk.baka.webvm.rss; |
| 20 | |
|
| 21 | |
import sk.baka.webvm.analyzer.ProblemReport; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.io.PrintWriter; |
| 24 | |
import java.util.Date; |
| 25 | |
import java.util.List; |
| 26 | |
import javax.servlet.ServletException; |
| 27 | |
import javax.servlet.http.HttpServlet; |
| 28 | |
import javax.servlet.http.HttpServletRequest; |
| 29 | |
import javax.servlet.http.HttpServletResponse; |
| 30 | |
import sk.baka.webvm.WicketApplication; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | public final class RssFeed extends HttpServlet { |
| 37 | |
|
| 38 | |
private static final long serialVersionUID = 1L; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
| 48 | |
throws ServletException, IOException { |
| 49 | 0 | final String contextRoot = getContextRoot(request); |
| 50 | |
final String link; |
| 51 | 0 | if (contextRoot != null) { |
| 52 | 0 | link = contextRoot + "/a/Problems"; |
| 53 | |
} else { |
| 54 | 0 | link = "a/Problems"; |
| 55 | |
} |
| 56 | 0 | response.setContentType("application/rss+xml"); |
| 57 | 0 | PrintWriter out = response.getWriter(); |
| 58 | |
try { |
| 59 | 0 | out.println("<?xml version=\"1.0\"?>\n<rss version=\"2.0\">"); |
| 60 | 0 | out.println(" <channel>\n <title>WebVM feeds</title>\n <link>"); |
| 61 | 0 | out.println(link); |
| 62 | 0 | out.println("</link>\n <description>WebVM: Remote server problems</description>"); |
| 63 | 0 | out.println(" <language>en-us</language>\n <ttl>1</ttl>\n"); |
| 64 | 0 | final List<List<ProblemReport>> ph = WicketApplication.getHistory().getProblemHistory(); |
| 65 | 0 | for (final List<ProblemReport> problems : ph) { |
| 66 | 0 | final Date snapshotTaken = new Date(problems.get(0).created); |
| 67 | 0 | out.print(" <item>\n <title>WebVM: Problems report for "); |
| 68 | 0 | out.print(snapshotTaken); |
| 69 | 0 | out.print("</title>\n <link>"); |
| 70 | 0 | out.print(link); |
| 71 | 0 | out.print("</link>\n <description>Problems report:<br/>"); |
| 72 | 0 | out.print(ProblemReport.escape(ProblemReport.toHtml(problems))); |
| 73 | 0 | out.print("</description>\n <pubDate>"); |
| 74 | 0 | out.print(snapshotTaken); |
| 75 | 0 | out.print("</pubDate>\n <guid>"); |
| 76 | 0 | out.print(snapshotTaken.getTime()); |
| 77 | 0 | out.println("</guid>\n </item>"); |
| 78 | 0 | } |
| 79 | 0 | out.println(" </channel>\n</rss>\n"); |
| 80 | |
} finally { |
| 81 | 0 | out.close(); |
| 82 | 0 | } |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
private static String getContextRoot(HttpServletRequest request) { |
| 86 | 0 | final String requrl = request.getRequestURL().toString(); |
| 87 | 0 | final int contextRootEnd = requrl.indexOf("/rss.xml"); |
| 88 | 0 | if (contextRootEnd < 0) { |
| 89 | 0 | return null; |
| 90 | |
} |
| 91 | 0 | return requrl.substring(0, contextRootEnd); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Override |
| 102 | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 103 | |
throws ServletException, IOException { |
| 104 | 0 | processRequest(request, response); |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Override |
| 115 | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 116 | |
throws ServletException, IOException { |
| 117 | 0 | processRequest(request, response); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public String getServletInfo() { |
| 126 | 0 | return "RSS Feed"; |
| 127 | |
} |
| 128 | |
} |