001: /**
002: * $Id: RssPortlet.java,v 1.7 2006/08/02 17:48:59 jtb Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.rssportlet;
014:
015: import javax.portlet.ActionRequest;
016: import javax.portlet.ActionResponse;
017: import javax.portlet.PortletException;
018: import javax.portlet.PortletMode;
019: import javax.portlet.PortletSecurityException;
020: import javax.portlet.UnavailableException;
021:
022: import java.io.IOException;
023: import java.io.FileNotFoundException;
024: import java.net.MalformedURLException;
025: import java.net.UnknownHostException;
026:
027: import java.util.LinkedList;
028: import java.util.Arrays;
029: import java.util.ResourceBundle;
030:
031: import javax.portlet.PortletModeException;
032: import javax.portlet.PortletSession;
033:
034: import com.sun.syndication.io.FeedException;
035: import javax.portlet.GenericPortlet;
036: import javax.portlet.PortletConfig;
037: import javax.portlet.PortletContext;
038: import javax.portlet.PortletRequestDispatcher;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041:
042: /**
043: * This class implements the Rss portlet.
044: *
045: * The following is a design overview of the RSS portlet ...
046: *
047: * The entry points into the application are the view and edit JSPs.
048: * These correspond to the portlet's view and edit modes, respectively.
049: * The JSPs use the *Handler classes to prepare the *Bean classes for use
050: * in the display logic. Control is passed to the JSPs in the do*() methods.
051: * The only logic in this
052: * class is the processing of portlet actions. This occurs for the "go"
053: * form in the portlet's view mode, and the forms in the portlet's edit mode.
054: *
055: * The portlet's edit mode allows the user to modify a subset of the portlet
056: * preferences to personalize the portlet.
057: *
058: */
059: public class RssPortlet extends GenericPortlet implements FormNames {
060: private PortletContext portletContext;
061:
062: public void init(PortletConfig config) throws PortletException {
063: super .init(config);
064: portletContext = config.getPortletContext();
065: }
066:
067: /** Include "view" JSP. */
068: public void doView(RenderRequest request, RenderResponse response)
069: throws PortletException, IOException {
070: include(request, response, "/WEB-INF/jsp/view.jsp");
071: }
072:
073: /** Include "edit" JSP. */
074: public void doEdit(RenderRequest request, RenderResponse response)
075: throws PortletException {
076: include(request, response, "/WEB-INF/jsp/edit.jsp");
077: }
078:
079: /** Include "help" JSP. */
080: public void doHelp(RenderRequest request, RenderResponse response)
081: throws PortletException {
082: ResourceBundle bundle = ResourceBundle.getBundle(
083: "com.sun.portal.rssportlet.RssPortlet", request
084: .getLocale());
085: include(request, response, bundle.getString("help_jsp"));
086: }
087:
088: /** Include a page. */
089: private void include(RenderRequest request,
090: RenderResponse response, String pageName)
091: throws PortletException {
092: response.setContentType(request.getResponseContentType());
093: if (pageName == null || pageName.length() == 0) {
094: // assert
095: throw new NullPointerException("null or empty page name");
096: }
097: try {
098: PortletRequestDispatcher dispatcher = portletContext
099: .getRequestDispatcher(pageName);
100: dispatcher.include(request, response);
101: } catch (IOException ioe) {
102: throw new PortletException(ioe);
103: }
104: }
105:
106: /** Process actions from the view mode "go" form, and the edit mode forms. */
107: public void processAction(ActionRequest request,
108: ActionResponse response) throws UnavailableException,
109: PortletSecurityException, PortletException, IOException {
110: Resources resources = new Resources(
111: "com.sun.portal.rssportlet.RssPortlet", request
112: .getLocale());
113:
114: // get objects from portlet session
115: // these are shared by the JSP
116: AlertHandler ah = (AlertHandler) request.getPortletSession()
117: .getAttribute("alertHandler",
118: PortletSession.PORTLET_SCOPE);
119:
120: // construct a new bean and populate with current values
121: SettingsBean readBean = new SettingsBean();
122: SettingsHandler handler = new SettingsHandler();
123: handler.setPortletConfig(getPortletConfig());
124: handler.setPortletRequest(request);
125: handler.setSettingsBean(readBean);
126:
127: // this bean contains the changes (if any) submitted in this
128: // action
129: SettingsBean writeBean = new SettingsBean();
130:
131: if (request.getParameter(SUBMIT_ADD) != null) {
132: //
133: // handle "add" button on edit page
134: // to add a new feed
135: //
136: processEditAddAction(request, response, ah, resources,
137: readBean, writeBean);
138: } else if (request.getParameter(SUBMIT_GO) != null
139: || request.getParameter(INPUT_SELECT_FEED) != null) {
140: //
141: // handle "go" button in view mode
142: // to change the selected feed
143: //
144: processGoAction(request, response, writeBean);
145: } else if (request.getParameter(SUBMIT_CANCEL) != null) {
146: //
147: // handle "cancel" button on edit page
148: // return to view mode
149: //
150: processEditCancelAction(request, response);
151: } else if (request.getParameter(SUBMIT_EDIT) != null) {
152: //
153: // handle various functions on the
154: // edit page
155: //
156: processEditAction(request, response, ah, resources,
157: readBean, writeBean);
158: }
159:
160: // if there were any changes, persist them
161: handler.persistSettingsBean(writeBean);
162: }
163:
164: private void processEditAddAction(ActionRequest request,
165: ActionResponse response, AlertHandler alertHandler,
166: Resources resources, SettingsBean readBean,
167: SettingsBean writeBean) {
168: String url = request.getParameter(INPUT_ADD_FEED);
169:
170: try {
171: // see if the url exists
172: // if there's no exception, then the feed exists and is valid
173: FeedHelper.getInstance().getFeed(readBean, url);
174:
175: //add to the existing values
176: LinkedList feeds = readBean.getFeeds();
177: feeds.add(url);
178: writeBean.setFeeds(feeds);
179:
180: //
181: // set newly added feed as selected feed
182: //
183: writeBean.setSelectedFeed(url);
184:
185: // we stay in edit mode here
186: } catch (MalformedURLException mue) {
187: alertHandler.setError(resources.get("invalid_url"), mue
188: .getMessage());
189: getPortletConfig().getPortletContext().log(
190: "could not add feed", mue);
191: } catch (UnknownHostException uhe) {
192: alertHandler.setError(resources.get("invalid_url"), uhe
193: .getMessage());
194: } catch (FileNotFoundException fnfe) {
195: alertHandler.setError(resources.get("invalid_url"), fnfe
196: .getMessage());
197: getPortletConfig().getPortletContext().log(
198: "could not add feed", fnfe);
199: } catch (IllegalArgumentException iae) {
200: alertHandler.setError(resources.get("invalid_url"), iae
201: .getMessage());
202: getPortletConfig().getPortletContext().log(
203: "could not add feed", iae);
204: } catch (FeedException fe) {
205: alertHandler.setError(resources.get("invalid_url"), fe
206: .getMessage());
207: getPortletConfig().getPortletContext().log(
208: "could not add feed", fe);
209: } catch (IOException ioe) {
210: alertHandler.setError(resources.get("invalid_url"), ioe
211: .getMessage());
212: getPortletConfig().getPortletContext().log(
213: "could not add feed", ioe);
214: }
215: }
216:
217: private void processGoAction(ActionRequest request,
218: ActionResponse response, SettingsBean writeBean) {
219: String selectedFeed = request.getParameter(INPUT_SELECT_FEED);
220:
221: if (null != selectedFeed) {
222: writeBean.setSelectedFeed(selectedFeed);
223: }
224: }
225:
226: private void processEditCancelAction(ActionRequest request,
227: ActionResponse response) throws PortletModeException {
228: response.setPortletMode(PortletMode.VIEW);
229: }
230:
231: private void processEditAction(ActionRequest request,
232: ActionResponse response, AlertHandler alertHandler,
233: Resources resources, SettingsBean readBean,
234: SettingsBean writeBean) throws PortletModeException {
235: String[] checkedFeeds = request.getParameterValues(INPUT_FEEDS);
236:
237: if (checkedFeeds == null) {
238: //
239: // no feeds
240: // empty the feeds list
241: //
242: writeBean.setFeeds(new LinkedList());
243: } else {
244: //
245: // entries selected, reset feed list to
246: // the selected entries.
247: //
248: LinkedList feeds = new LinkedList(Arrays
249: .asList(checkedFeeds));
250: writeBean.setFeeds(feeds);
251:
252: //
253: // start feed
254: // only set the start feed if the feeds list in non-null
255: // (that's the only way we get here)
256: // make sure the start feed is in the new feeds list
257: // catch the case where the user deletes the start feed
258: //
259: String startFeed = request.getParameter(INPUT_START_FEED);
260: if (startFeed != null && feeds.contains(startFeed)) {
261: writeBean.setStartFeed(startFeed);
262: }
263:
264: //
265: // case where we delete the selected feed
266: // set to the start feed
267: //
268: if (readBean.getSelectedFeed() != null
269: && !feeds.contains(readBean.getSelectedFeed())) {
270: String selectedFeed = writeBean.getStartFeed();
271: writeBean.setSelectedFeed(selectedFeed);
272: }
273:
274: //
275: // case where selected feed is null, because feed
276: // list was previously null
277: // set the selected feed to the start feed
278: //
279: if (readBean.getFeeds().size() == 0) {
280: writeBean.setSelectedFeed(writeBean.getStartFeed());
281: }
282: }
283:
284: String s = request.getParameter(INPUT_MAX_AGE);
285: if (s != null && s.length() > 0) {
286: //
287: // test to make sure it's an int
288: //
289: try {
290: int n = Integer.parseInt(s);
291: if (n < 1) {
292: alertHandler
293: .setError(resources
294: .get("enter_a_whole_number_greater_than_zero"));
295: } else {
296: writeBean.setMaxAge(n);
297: }
298: } catch (NumberFormatException nfe) {
299: alertHandler.setError(resources
300: .get("enter_a_whole_number_greater_than_zero"));
301: }
302: }
303:
304: String maxEntries = request.getParameter(INPUT_MAX_ENTRIES);
305: if (maxEntries != null && maxEntries.length() > 0) {
306: //
307: // test to make sure it's an int
308: //
309: try {
310: int n = Integer.parseInt(maxEntries);
311: if (n < 1) {
312: alertHandler
313: .setError(resources
314: .get("enter_a_whole_number_greater_than_zero"));
315: } else {
316: writeBean.setMaxEntries(n);
317: }
318: } catch (NumberFormatException nfe) {
319: alertHandler.setError(resources
320: .get("enter_a_whole_number_greater_than_zero"));
321: }
322: }
323:
324: String[] disableMaxAge = request
325: .getParameterValues(INPUT_DISABLE_MAX_AGE);
326:
327: if (disableMaxAge != null && disableMaxAge.length > 0) {
328: writeBean.setDisableMaxAge(true);
329: } else {
330: writeBean.setDisableMaxAge(false);
331: }
332:
333: String[] newWindow = request.getParameterValues(INPUT_NEWWIN);
334:
335: if (newWindow != null && newWindow.length > 0) {
336: writeBean.setNewWindow(true);
337: } else {
338: writeBean.setNewWindow(false);
339: }
340:
341: if (!alertHandler.isErrorExists()) {
342: // if there were no errors, then go back to
343: // view mode
344: response.setPortletMode(PortletMode.VIEW);
345: }
346: }
347: }
|