1 18 19 package org.apache.struts.webapp.tiles.rssChannel; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 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 48 public final class Channels extends Action { 49 50 53 private static Log log = LogFactory.getLog(Channels.class); 54 55 58 public static final String CHANNELS_KEY = "CHANNELS"; 59 60 63 public static final String CHANNEL_URLS_KEY = "urls"; 64 65 68 public static final String CHANNEL_URL_KEY = "url"; 69 70 73 public ActionForward execute( 74 ActionMapping mapping, 75 ActionForm form, 76 HttpServletRequest request, 77 HttpServletResponse response) 78 throws Exception { 79 80 log.debug("Enter Rss Channel Action"); 81 82 ComponentContext context = ComponentContext.getContext(request); 84 if (context == null) { 85 throw new ServletException ("This action must be called by a Tile, not directly"); 86 } 87 88 ActionMessages errors = new ActionMessages(); 89 90 93 List channels = (List ) context.getAttribute(CHANNEL_URLS_KEY); 94 if (channels == null) { 95 Object url = context.getAttribute(CHANNEL_URL_KEY); 96 channels = new ArrayList (1); 97 channels.add(url); 98 } 99 100 log.debug("urls count" + channels.size()); 101 102 ArrayList channelBeans = new ArrayList (channels.size()); 104 try { 105 for (int i = 0; i < channels.size(); i++) { 106 RSSDigester digester = new RSSDigester(); 107 String url = (String ) channels.get(i); 108 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 t) { 122 errors.add( 123 ActionMessages.GLOBAL_MESSAGE, 124 new ActionMessage("rss.access.error")); 125 126 servlet.log(t.toString()); 127 } 128 129 if (!errors.isEmpty()) { 131 this.saveErrors(request, errors); 132 133 if (mapping.getInput() != null) { 134 return new ActionForward(mapping.getInput()); 135 } 136 137 139 log.debug("Exit Rss Channel Action : error"); 140 141 return mapping.findForward("error"); 142 } 143 144 146 log.debug("Exit Rss Channel Action"); 147 148 context.putAttribute(CHANNELS_KEY, channelBeans); 150 151 return mapping.findForward("continue"); 152 } 153 154 private String toFullUrl(HttpServletRequest request, String url) { 155 StringBuffer buff = new StringBuffer (); 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 |