001    package com.khubla.pragmatach.examples.rssviewer;
002    
003    import java.net.HttpURLConnection;
004    import java.net.URL;
005    
006    import com.khubla.pragmatach.framework.annotation.Controller;
007    import com.khubla.pragmatach.framework.annotation.Route;
008    import com.khubla.pragmatach.framework.annotation.Route.HttpMethod;
009    import com.khubla.pragmatach.framework.annotation.View;
010    import com.khubla.pragmatach.framework.api.PragmatachException;
011    import com.khubla.pragmatach.framework.api.Response;
012    import com.khubla.pragmatach.plugin.freemarker.FreemarkerController;
013    import com.sun.syndication.feed.synd.SyndFeed;
014    import com.sun.syndication.io.SyndFeedInput;
015    import com.sun.syndication.io.XmlReader;
016    
017    @Controller()
018    @View(view = "showfeed.html")
019    public class ShowFeedController extends FreemarkerController {
020       /**
021        * the URL
022        */
023       private String feedURL;
024       /**
025        * the feed
026        */
027       private SyndFeed syndFeed;
028    
029       public String getFeedURL() {
030          return feedURL;
031       }
032    
033       public SyndFeed getSyndFeed() {
034          return syndFeed;
035       }
036    
037       public void setFeedURL(String feedURL) {
038          this.feedURL = feedURL;
039       }
040    
041       public void setSyndFeed(SyndFeed syndFeed) {
042          this.syndFeed = syndFeed;
043       }
044    
045       @Route(uri = "/showfeed", method = HttpMethod.post)
046       public Response showFeed() throws PragmatachException {
047          try {
048             if (null != feedURL) {
049                /*
050                 * get feed
051                 */
052                final URL url = new URL(feedURL);
053                final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
054                /*
055                 * parse it
056                 */
057                final SyndFeedInput input = new SyndFeedInput();
058                syndFeed = input.build(new XmlReader(httpURLConnection));
059             }
060             /*
061              * render
062              */
063             return super.render();
064          } catch (final Exception e) {
065             throw new PragmatachException("Exception in showFeed", e);
066          }
067       }
068    }