View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.applications.rss;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  
22  import javax.portlet.ActionRequest;
23  import javax.portlet.ActionResponse;
24  import javax.portlet.PortletConfig;
25  import javax.portlet.PortletException;
26  import javax.portlet.PortletPreferences;
27  import javax.portlet.RenderRequest;
28  import javax.portlet.RenderResponse;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
33  
34  import org.apache.velocity.context.Context;
35  
36  import com.sun.syndication.feed.synd.SyndFeed;
37  import com.sun.syndication.io.SyndFeedInput;
38  import com.sun.syndication.io.XmlReader;
39  
40  /***
41   * Rome RSS Portlet
42   * 
43   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
44   * @version $Id: RomeRSSPortlet.java 516448 2007-03-09 16:25:47Z ate $
45   */
46  public class RomeRSSPortlet extends GenericVelocityPortlet
47  {
48  
49      protected Log log = LogFactory.getLog(RomeRSSPortlet.class);
50  
51      /***
52       * @see javax.portlet.Portlet#init(javax.portlet.PortletConfig)
53       */
54      public void init(PortletConfig config) throws PortletException
55      {
56          super.init(config);
57  
58      }
59  
60      /***
61       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
62       */
63      public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
64      {
65  
66          response.setContentType("text/html");
67          Context velocityContext = this.getContext(request);
68          PortletPreferences prefs = request.getPreferences();
69          String url = prefs.getValue("url", "http://www.npr.org/rss/rss.php?topicId=4");
70          try
71          {
72              URL feedUrl = new URL(url);
73              SyndFeedInput input = new SyndFeedInput();
74  
75              SyndFeed feed = input.build(new XmlReader(feedUrl));
76  
77              RssInfo rssInfo = new RssInfo(feed, new Integer(prefs.getValue("itemdisplayed", "15")).intValue(), new Boolean(prefs
78                      .getValue("openinpopup", "true")).booleanValue(), new Boolean(prefs.getValue("showdescription", "true"))
79                      .booleanValue(), new Boolean(prefs.getValue("showtitle", "true")).booleanValue(), new Boolean(prefs.getValue(
80                      "showtextinput", "true")).booleanValue());
81              if (feed.getTitle() != null)
82                  response.setTitle(feed.getTitle());
83              velocityContext.put("rssInfo", rssInfo);
84  
85              super.doView(request, response);
86  
87          }
88          catch (Exception e)
89          {
90              throw new PortletException(new String("Failed to process RSS Feed: " + url + ", " + e));
91          }
92  
93      }
94  
95      /***
96       * 
97       * @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
98       */
99      public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
100     {
101         response.setContentType("text/html");
102         doPreferencesEdit(request, response);
103     }
104 
105     /***
106      * 
107      * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
108      */
109     public void processAction(ActionRequest request, ActionResponse actionResponse) throws PortletException, java.io.IOException
110     {
111         String add = request.getParameter("Save");
112         if (add != null)
113         { 
114             processPreferencesAction(request, actionResponse);
115         }
116     }
117 
118 }