KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > rssChannel > RssChannelsAction


1 /*
2  * $Id: RssChannelsAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.webapp.tiles.rssChannel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.digester.rss.Channel;
28 import org.apache.commons.digester.rss.RSSDigester;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36 import org.apache.struts.tiles.ComponentContext;
37 import org.apache.struts.tiles.actions.TilesAction;
38
39 /**
40  * Read and parse RSS files found at on a given
41  * list in Tile attribute, save the Channel
42  * beans in Tile attribute,and forward to "continue".
43  * Tiles input attributes :
44  * <ul>
45  * <li>urls : list of channel urls</li>
46  * <li>url : a channel url (if urls not use)</li>
47  * </ul>
48  * Tiles output attributes :
49  * <ul>
50  * <li>channels : List of Digester Channel beans</li>
51  * </ul>
52  *
53  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
54  */

55 public final class RssChannelsAction extends TilesAction {
56
57     /**
58      * Commons Logging instance.
59      */

60     private static Log log = LogFactory.getLog(RssChannelsAction.class);
61
62     /**
63      * Tile attribute key for saving Channel bean
64      */

65     public static final String JavaDoc CHANNELS_KEY = "CHANNELS";
66
67     /**
68      * Tile attribute key for getting Channel urls list
69      */

70     public static final String JavaDoc CHANNEL_URLS_KEY = "urls";
71
72     /**
73      * Tile attribute key for getting Channel url attribute
74      */

75     public static final String JavaDoc CHANNEL_URL_KEY = "url";
76
77     /**
78      * Main process of class. Reads, parses
79      * @param context The current Tile context, containing Tile attributes.
80      * @param mapping The ActionMapping used to select this instance.
81      * @param form The optional ActionForm bean for this request (if any).
82      * @param request The HTTP request we are processing.
83      * @param response The HTTP response we are creating.
84      *
85      * @exception Exception if the application business logic throws
86      * an exception
87      * @since Struts 1.1
88      */

89     public ActionForward execute(
90         ComponentContext context,
91         ActionMapping mapping,
92         ActionForm form,
93         HttpServletRequest JavaDoc request,
94         HttpServletResponse JavaDoc response)
95         throws Exception JavaDoc {
96
97         log.debug("Enter Rss Channel Action");
98
99         ActionMessages errors = new ActionMessages();
100
101         // -- Retrieve parameters --
102
// Urls can come from a list, or from a single attribute.
103

104         List JavaDoc channels = (List JavaDoc) context.getAttribute(CHANNEL_URLS_KEY);
105         if (channels == null) {
106             Object JavaDoc url = context.getAttribute(CHANNEL_URL_KEY);
107             channels = new ArrayList JavaDoc(1);
108             channels.add(url);
109         }
110
111         log.debug("urls count" + channels.size());
112
113         // -- Loop through channels --
114
List JavaDoc channelBeans = new ArrayList JavaDoc(channels.size());
115         try {
116             for (int i = 0; i < channels.size(); i++) {
117                 RSSDigester digester = new RSSDigester();
118                 String JavaDoc url = (String JavaDoc) channels.get(i);
119                 // Add application path if needed
120
if (url.startsWith("/")) {
121                     url = toFullUrl(request, url);
122                 }
123
124                 log.debug("Channel url=" + url);
125
126                 Channel obj = (Channel) digester.parse(url);
127
128                 log.debug("Channel:" + obj);
129
130                 channelBeans.add(obj);
131             }
132             
133         } catch (Throwable JavaDoc t) {
134             errors.add(
135                 ActionMessages.GLOBAL_MESSAGE,
136                 new ActionMessage("rss.access.error"));
137
138             servlet.log(t.toString());
139         }
140
141         // -- Handle Errors ---
142
if (!errors.isEmpty()) {
143             this.saveErrors(request, errors);
144             // If no input page, use error forwarding
145

146             log.debug("Exit Rss Channel Action : error");
147
148             return null;
149         }
150
151         // -- Save Bean, and Continue ---
152

153         log.debug("Exit Rss Channel Action");
154
155         // Use Tile context to pass channels
156
context.putAttribute(CHANNELS_KEY, channelBeans);
157
158         return null;
159     }
160
161     /**
162      * Compute Full local url from an url starting with "/".
163      */

164     private String JavaDoc toFullUrl(HttpServletRequest JavaDoc request, String JavaDoc url) {
165         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
166
167         buff.append(request.getScheme()).append("://").append(
168             request.getServerName());
169
170         if (request.getServerPort() != 80) {
171             buff.append(":").append(request.getServerPort());
172         }
173
174         buff.append(request.getContextPath()).append(url);
175
176         return buff.toString();
177     }
178
179 }
180
Popular Tags