KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Channels.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.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.digester.rss.Channel;
29 import org.apache.commons.digester.rss.RSSDigester;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.action.Action;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38 import org.apache.struts.tiles.ComponentContext;
39
40 /**
41  * Read and parse RSS files found at on a given
42  * list in user request or session, save the Channel
43  * beans in request scope,and forward to "continue".
44  * @expects path={uri} on command line or as parameter property to ActionMapping.
45  * @expects an input page or error forwarding if exception digesting RSS
46  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
47  */

48 public final class Channels extends Action {
49
50     /**
51      * Commons Logging instance.
52      */

53     private static Log log = LogFactory.getLog(Channels.class);
54
55     /**
56      * Tile attribute key for saving Channel bean
57      */

58     public static final String JavaDoc CHANNELS_KEY = "CHANNELS";
59
60     /**
61      * Tile attribute key for getting Channel urls list
62      */

63     public static final String JavaDoc CHANNEL_URLS_KEY = "urls";
64
65     /**
66      * Tile attribute key for getting Channel url attribute
67      */

68     public static final String JavaDoc CHANNEL_URL_KEY = "url";
69
70     /**
71      * Main process of class. Reads, parses
72      */

73     public ActionForward execute(
74         ActionMapping mapping,
75         ActionForm form,
76         HttpServletRequest JavaDoc request,
77         HttpServletResponse JavaDoc response)
78         throws Exception JavaDoc {
79
80         log.debug("Enter Rss Channel Action");
81
82         // Try to retrieve tile context
83
ComponentContext context = ComponentContext.getContext(request);
84         if (context == null) {
85             throw new ServletException JavaDoc("This action must be called by a Tile, not directly");
86         }
87
88         ActionMessages errors = new ActionMessages();
89
90         // -- Retrieve parameters --
91
// Urls can come from a list, or from a single attribute.
92

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

139             log.debug("Exit Rss Channel Action : error");
140
141             return mapping.findForward("error");
142         }
143
144         // -- Save Bean, and Continue ---
145

146         log.debug("Exit Rss Channel Action");
147
148         // Use Tile context to pass channels
149
context.putAttribute(CHANNELS_KEY, channelBeans);
150
151         return mapping.findForward("continue");
152     }
153
154     private String JavaDoc toFullUrl(HttpServletRequest JavaDoc request, String JavaDoc url) {
155         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
156
157         buff.append(request.getScheme()).append("://").append(
158             request.getServerName());
159
160         if (request.getServerPort() != 80) {
161             buff.append(":").append(request.getServerPort());
162         }
163
164         buff.append(request.getContextPath()).append(url);
165
166         return buff.toString();
167     }
168
169 }
170
Popular Tags