KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > RSSDataExport


1 package org.tigris.scarab.screens;
2
3 /* ================================================================
4  * Copyright (c) 2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.collab.net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.io.Writer JavaDoc;
50
51 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
80  * <p>
81  * Sends rss content directly to the output stream, setting the
82  * <code>Content-Type</code> to application/xml; charset=UTF-8.
83  *
84  * @author <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh </a>
85  */

86 public class RSSDataExport extends TemplateScreen {
87     private static final String JavaDoc DEFAULT_FEED_FORMAT = "atom_0.3";
88
89     private static final String JavaDoc MIME_TYPE = "application/xml; charset=UTF-8";
90
91     private static final String JavaDoc COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
92
93     private static final String JavaDoc COULD_NOT_GENERATE_FEED_ERROR_DATABASE = "Could not retrive data successfully";
94
95     public static final String JavaDoc QUERY_ID_KEY = "queryId";
96     public static final String JavaDoc ISSUE_ID_KEY = "issueId";
97
98     public static final String JavaDoc USER_ID_KEY = "userId";
99
100     public static final String JavaDoc FEED_TYPE_KEY = "feedType";
101     public static final String JavaDoc FEED_FORMAT_KEY = "type";
102
103     /**
104      * Sets the <code>Content-Type</code> header for the response. Since this
105      * assumes we're writing the reponse ourself, indicates no target to render
106      * by setting it to <code>null</code>.
107      */

108     public void doBuildTemplate(RunData data, TemplateContext context)
109             throws Exception JavaDoc {
110         super.doBuildTemplate(data, context);
111
112         data.getResponse().setContentType(MIME_TYPE);
113
114         // we will send the response, so no target to render
115
data.setTarget(null);
116         
117         Writer JavaDoc writer = data.getResponse().getWriter();
118         
119         try {
120             ParameterParser parser = data.getParameters();
121
122
123
124             String JavaDoc feedType = parser.getString(FEED_TYPE_KEY);
125             String JavaDoc 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 JavaDoc("Query ID is missing. Should be appended like: /queryId/xxx");
140                 }
141                 if(userId==0){
142                     throw new IllegalArgumentException JavaDoc("User ID is missing. Should be appended like: /userId/xxx");
143                 }
144                 Query query = QueryManager.getInstance(new Long JavaDoc(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 JavaDoc issueId = parser.getString(ISSUE_ID_KEY);
150                 if(issueId.equals("")){
151                     throw new IllegalArgumentException JavaDoc("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 JavaDoc("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 JavaDoc iae){
169             String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
184             String JavaDoc 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     /**
191      * Helper method to retrieve the ScarabLocalizationTool from the Context
192      */

193     protected final ScarabLocalizationTool
194         getLocalizationTool(TemplateContext context)
195     {
196         return (ScarabLocalizationTool)
197             context.get(ScarabConstants.LOCALIZATION_TOOL);
198     }
199     
200     /**
201      * Helper method to retrieve the ScarabRequestTool from the Context
202      */

203     public ScarabRequestTool getScarabRequestTool(TemplateContext context)
204     {
205         return (ScarabRequestTool)context
206             .get(ScarabConstants.SCARAB_REQUEST_TOOL);
207     }
208     
209     /**
210      * Helper method to retrieve the ScarabRequestTool from the Context
211      */

212     public ScarabLink getScarabLinkTool(TemplateContext context)
213     {
214         return (ScarabLink)context
215             .get(ScarabConstants.SCARAB_LINK_TOOL);
216     }
217 }
Popular Tags