1 package org.tigris.scarab.screens; 2 3 48 49 import java.io.Writer ; 50 51 import javax.servlet.http.HttpServletResponse ; 52 53 import org.apache.fulcrum.parser.ParameterParser; 54 import org.apache.torque.TorqueException; 55 import org.apache.torque.om.NumberKey; 56 import org.apache.turbine.RunData; 57 import org.apache.turbine.TemplateContext; 58 import org.apache.turbine.TemplateScreen; 59 import org.tigris.scarab.feeds.Feed; 60 import org.tigris.scarab.feeds.IssueFeed; 61 import org.tigris.scarab.feeds.QueryFeed; 62 import org.tigris.scarab.om.Issue; 63 import org.tigris.scarab.om.IssueManager; 64 import org.tigris.scarab.om.Query; 65 import org.tigris.scarab.om.QueryManager; 66 import org.tigris.scarab.om.ScarabUser; 67 import org.tigris.scarab.om.ScarabUserManager; 68 import org.tigris.scarab.tools.ScarabLocalizationTool; 69 import org.tigris.scarab.tools.ScarabRequestTool; 70 import org.tigris.scarab.tools.ScarabToolManager; 71 import org.tigris.scarab.util.Log; 72 import org.tigris.scarab.util.ScarabConstants; 73 import org.tigris.scarab.util.ScarabLink; 74 75 import com.sun.syndication.feed.synd.SyndFeed; 76 import com.sun.syndication.io.FeedException; 77 import com.sun.syndication.io.SyndFeedOutput; 78 79 86 public class RSSDataExport extends TemplateScreen { 87 private static final String DEFAULT_FEED_FORMAT = "atom_0.3"; 88 89 private static final String MIME_TYPE = "application/xml; charset=UTF-8"; 90 91 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed"; 92 93 private static final String COULD_NOT_GENERATE_FEED_ERROR_DATABASE = "Could not retrive data successfully"; 94 95 public static final String QUERY_ID_KEY = "queryId"; 96 public static final String ISSUE_ID_KEY = "issueId"; 97 98 public static final String USER_ID_KEY = "userId"; 99 100 public static final String FEED_TYPE_KEY = "feedType"; 101 public static final String FEED_FORMAT_KEY = "type"; 102 103 108 public void doBuildTemplate(RunData data, TemplateContext context) 109 throws Exception { 110 super.doBuildTemplate(data, context); 111 112 data.getResponse().setContentType(MIME_TYPE); 113 114 data.setTarget(null); 116 117 Writer writer = data.getResponse().getWriter(); 118 119 try { 120 ParameterParser parser = data.getParameters(); 121 122 123 124 String feedType = parser.getString(FEED_TYPE_KEY); 125 String feedFormat = parser.getString(FEED_FORMAT_KEY); 126 127 ScarabLink scarabLink= getScarabLinkTool(context); 128 ScarabRequestTool scarabRequestTool= getScarabRequestTool(context); 129 130 131 132 Feed feedSource = null; 133 ScarabToolManager scarabToolManager = new ScarabToolManager(getLocalizationTool(context)); 134 if (feedType.equals("QueryFeed")){ 135 136 long queryId = parser.getLong(QUERY_ID_KEY); 137 long userId = parser.getLong(USER_ID_KEY); 138 if(queryId==0){ 139 throw new IllegalArgumentException ("Query ID is missing. Should be appended like: /queryId/xxx"); 140 } 141 if(userId==0){ 142 throw new IllegalArgumentException ("User ID is missing. Should be appended like: /userId/xxx"); 143 } 144 Query query = QueryManager.getInstance(new Long (queryId)); 145 ScarabUser user1 = ScarabUserManager.getInstance(new NumberKey(userId), false); 146 feedSource = new QueryFeed(query,user1,scarabToolManager,scarabLink); 147 } 148 else if (feedType.equals("IssueFeed")){ 149 String issueId = parser.getString(ISSUE_ID_KEY); 150 if(issueId.equals("")){ 151 throw new IllegalArgumentException ("Issue ID is missing. Should be appended like: /issueId/xxx"); 152 } 153 Issue issue = IssueManager.getIssueById(issueId); 154 feedSource = new IssueFeed(issue,scarabLink,scarabToolManager); 155 } 156 else { 157 throw new Exception ("Couldn't find feed for type:" + feedType); 158 } 159 SyndFeed feed = feedSource.getFeed(); 160 161 feedFormat = (feedFormat!=null) ? feedFormat : DEFAULT_FEED_FORMAT; 162 feed.setFeedType(feedFormat); 163 164 165 SyndFeedOutput output = new SyndFeedOutput(); 166 output.output(feed,writer); 167 } 168 catch(IllegalArgumentException iae){ 169 String msg = COULD_NOT_GENERATE_FEED_ERROR + ": " + iae.getMessage(); 170 Log.get().error(msg,iae); 171 data.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg); 172 } 173 catch(TorqueException te){ 174 String msg = COULD_NOT_GENERATE_FEED_ERROR_DATABASE; 175 Log.get().error(msg,te); 176 data.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg); 177 } 178 catch (FeedException ex) { 179 String msg = COULD_NOT_GENERATE_FEED_ERROR; 180 Log.get().error(msg,ex); 181 data.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg); 182 } 183 catch (Exception e) { 184 String msg = COULD_NOT_GENERATE_FEED_ERROR; 185 Log.get().error(msg,e); 186 data.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg); 187 } 188 } 189 190 193 protected final ScarabLocalizationTool 194 getLocalizationTool(TemplateContext context) 195 { 196 return (ScarabLocalizationTool) 197 context.get(ScarabConstants.LOCALIZATION_TOOL); 198 } 199 200 203 public ScarabRequestTool getScarabRequestTool(TemplateContext context) 204 { 205 return (ScarabRequestTool)context 206 .get(ScarabConstants.SCARAB_REQUEST_TOOL); 207 } 208 209 212 public ScarabLink getScarabLinkTool(TemplateContext context) 213 { 214 return (ScarabLink)context 215 .get(ScarabConstants.SCARAB_LINK_TOOL); 216 } 217 } | Popular Tags |