001: package org.tigris.scarab.screens;
002:
003: /* ================================================================
004: * Copyright (c) 2003 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by CollabNet <http://www.collab.net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of CollabNet.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of CollabNet.
047: */
048:
049: import java.io.Writer;
050:
051: import javax.servlet.http.HttpServletResponse;
052:
053: import org.apache.fulcrum.parser.ParameterParser;
054: import org.apache.torque.TorqueException;
055: import org.apache.turbine.RunData;
056: import org.apache.turbine.TemplateContext;
057: import org.apache.turbine.TemplateScreen;
058: import org.tigris.scarab.feeds.Feed;
059: import org.tigris.scarab.feeds.IssueFeed;
060: import org.tigris.scarab.om.Issue;
061: import org.tigris.scarab.om.IssueManager;
062: import org.tigris.scarab.tools.ScarabLocalizationTool;
063: import org.tigris.scarab.tools.ScarabRequestTool;
064: import org.tigris.scarab.tools.ScarabToolManager;
065: import org.tigris.scarab.util.Log;
066: import org.tigris.scarab.util.ScarabConstants;
067: import org.tigris.scarab.util.ScarabLink;
068:
069: import com.sun.syndication.feed.synd.SyndFeed;
070: import com.sun.syndication.io.FeedException;
071: import com.sun.syndication.io.SyndFeedOutput;
072:
073: /**
074: * <p>
075: * Sends rss content directly to the output stream, setting the
076: * <code>Content-Type</code> to application/xml; charset=UTF-8.
077: *
078: * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh </a>
079: */
080: public class RSSDataExport extends TemplateScreen {
081: public static final String DEFAULT_FEED_FORMAT = "atom_0.3";
082:
083: private static final String MIME_TYPE = "application/xml; charset=UTF-8";
084:
085: private static final String COULD_NOT_FIND_FEED_TYPE = "Parser did not find a valid feedType";
086:
087: private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
088:
089: private static final String COULD_NOT_GENERATE_FEED_ERROR_DATABASE = "Could not retrive data successfully";
090:
091: public static final String QUERY_ID_KEY = "queryId";
092: public static final String ISSUE_ID_KEY = "issueId";
093:
094: public static final String USER_ID_KEY = "userId";
095:
096: public static final String FEED_TYPE_KEY = "feedType";
097: public static final String FEED_FORMAT_KEY = "type";
098:
099: /**
100: * Sets the <code>Content-Type</code> header for the response. Since this
101: * assumes we're writing the reponse ourself, indicates no target to render
102: * by setting it to <code>null</code>.
103: */
104: public void doBuildTemplate(RunData data, TemplateContext context)
105: throws Exception {
106: super .doBuildTemplate(data, context);
107:
108: data.getResponse().setContentType(MIME_TYPE);
109:
110: // we will send the response, so no target to render
111: data.setTarget(null);
112:
113: Writer writer = data.getResponse().getWriter();
114:
115: try {
116: ParameterParser parser = data.getParameters();
117:
118: String feedType = parser.getString(FEED_TYPE_KEY);
119: if (feedType == null) {
120: String msg = COULD_NOT_FIND_FEED_TYPE;
121: Log.get().error(msg);
122: data.getResponse().sendError(
123: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
124: msg);
125: return;
126: }
127:
128: String feedFormat = parser.getString(FEED_FORMAT_KEY);
129:
130: ScarabLink scarabLink = getScarabLinkTool(context);
131:
132: Feed feedSource = null;
133: ScarabToolManager scarabToolManager = new ScarabToolManager(
134: getLocalizationTool(context));
135: if (feedType.equals("IssueFeed")) {
136: String issueId = parser.getString(ISSUE_ID_KEY);
137: if (issueId.equals("")) {
138: throw new IllegalArgumentException(
139: "Issue ID is missing. Should be appended like: /issueId/xxx");
140: }
141: Issue issue = IssueManager.getIssueById(issueId);
142: feedSource = new IssueFeed(issue, scarabLink,
143: scarabToolManager, getLocalizationTool(context));
144: } else {
145: throw new Exception("Couldn't find feed for type:"
146: + feedType);
147: }
148: SyndFeed feed = feedSource.getFeed();
149:
150: feedFormat = (feedFormat != null) ? feedFormat
151: : DEFAULT_FEED_FORMAT;
152: feed.setFeedType(feedFormat);
153:
154: SyndFeedOutput output = new SyndFeedOutput();
155: output.output(feed, writer);
156: } catch (IllegalArgumentException iae) {
157: String msg = COULD_NOT_GENERATE_FEED_ERROR + ": "
158: + iae.getMessage();
159: Log.get().error(msg, iae);
160: data.getResponse().sendError(
161: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
162: } catch (TorqueException te) {
163: String msg = COULD_NOT_GENERATE_FEED_ERROR_DATABASE;
164: Log.get().error(msg, te);
165: data.getResponse().sendError(
166: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
167: } catch (FeedException ex) {
168: String msg = COULD_NOT_GENERATE_FEED_ERROR;
169: Log.get().error(msg, ex);
170: data.getResponse().sendError(
171: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
172: } catch (Exception e) {
173: String msg = COULD_NOT_GENERATE_FEED_ERROR;
174: Log.get().error(msg, e);
175: data.getResponse().sendError(
176: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
177: }
178: }
179:
180: /**
181: * Helper method to retrieve the ScarabLocalizationTool from the Context
182: */
183: protected final ScarabLocalizationTool getLocalizationTool(
184: TemplateContext context) {
185: return (ScarabLocalizationTool) context
186: .get(ScarabConstants.LOCALIZATION_TOOL);
187: }
188:
189: /**
190: * Helper method to retrieve the ScarabRequestTool from the Context
191: */
192: public ScarabRequestTool getScarabRequestTool(
193: TemplateContext context) {
194: return (ScarabRequestTool) context
195: .get(ScarabConstants.SCARAB_REQUEST_TOOL);
196: }
197:
198: /**
199: * Helper method to retrieve the ScarabRequestTool from the Context
200: */
201: public ScarabLink getScarabLinkTool(TemplateContext context) {
202: return (ScarabLink) context
203: .get(ScarabConstants.SCARAB_LINK_TOOL);
204: }
205: }
|