Coverage Report - sk.baka.webvm.rss.RssFeed
 
Classes in this File Line Coverage Branch Coverage Complexity
RssFeed
0%
0/41
0%
0/6
1.8
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * Provides a RSS feed with the "Problems" report.
 34  
  * @author Martin Vysny
 35  
  */
 36  0
 public final class RssFeed extends HttpServlet {
 37  
 
 38  
     private static final long serialVersionUID = 1L;
 39  
 
 40  
     /** 
 41  
      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 42  
      * @param request servlet request
 43  
      * @param response servlet response
 44  
      * @throws ServletException if a servlet-specific error occurs
 45  
      * @throws IOException if an I/O error occurs
 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:&lt;br/&gt;");
 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  
      * Handles the HTTP <code>GET</code> method.
 96  
      * @param request servlet request
 97  
      * @param response servlet response
 98  
      * @throws ServletException if a servlet-specific error occurs
 99  
      * @throws IOException if an I/O error occurs
 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  
      * Handles the HTTP <code>POST</code> method.
 109  
      * @param request servlet request
 110  
      * @param response servlet response
 111  
      * @throws ServletException if a servlet-specific error occurs
 112  
      * @throws IOException if an I/O error occurs
 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  
      * Returns a short description of the servlet.
 122  
      * @return a String containing servlet description
 123  
      */
 124  
     @Override
 125  
     public String getServletInfo() {
 126  0
         return "RSS Feed";
 127  
     }
 128  
 }