RSS.java 4.0 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
K
kohsuke 已提交
24 25 26
package hudson.model;

import hudson.FeedAdapter;
27
import hudson.util.RunList;
28
import jenkins.model.Jenkins;
K
kohsuke 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;

/**
 * RSS related code.
 *
 * @author Kohsuke Kawaguchi
 */
42
public final class RSS {
K
kohsuke 已提交
43 44 45 46 47 48 49

    /**
     * Sends the RSS feed to the client.
     *
     * @param title
     *      Title of the feed.
     * @param url
K
kohsuke 已提交
50
     *      URL of the model object that owns this feed. Relative to the context root.
K
kohsuke 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
     * @param entries
     *      Entries to be listed in the RSS feed.
     * @param adapter
     *      Controls how to render entries to RSS.
     */
    public static <E> void forwardToRss(String title, String url, Collection<? extends E> entries, FeedAdapter<E> adapter, StaplerRequest req, HttpServletResponse rsp) throws IOException, ServletException {
        req.setAttribute("adapter",adapter);
        req.setAttribute("title",title);
        req.setAttribute("url",url);
        req.setAttribute("entries",entries);

        String flavor = req.getParameter("flavor");
        if(flavor==null)    flavor="atom";
64
        flavor = flavor.replace('/', '_'); // Don't allow path to any jelly
K
kohsuke 已提交
65

66 67 68 69 70 71
        if (flavor.equals("atom")) {
            rsp.setContentType("application/atom+xml; charset=UTF-8");
        } else {
            rsp.setContentType("text/xml; charset=UTF-8");
        }

72
        req.getView(Jenkins.get(),"/hudson/"+flavor+".jelly").forward(req,rsp);
K
kohsuke 已提交
73
    }
74 75 76 77 78 79 80 81 82 83

    /**
     * Sends the RSS feed to the client using a default feed adapter.
     *
     * @param title
     *      Title of the feed.
     * @param url
     *      URL of the model object that owns this feed. Relative to the context root.
     * @param runList
     *      Entries to be listed in the RSS feed.
84
     * @since 2.215
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
     */
    public static void rss(StaplerRequest req, StaplerResponse rsp, String title, String url, RunList runList) throws IOException, ServletException {
        rss(req, rsp, title, url, runList, null);
    }

    /**
     * Sends the RSS feed to the client using a specific feed adapter.
     *
     * @param title
     *      Title of the feed.
     * @param url
     *      URL of the model object that owns this feed. Relative to the context root.
     * @param runList
     *      Entries to be listed in the RSS feed.
     * @param feedAdapter
     *      Controls how to render entries to RSS.
101
     * @since 2.215
102 103 104 105 106
     */
    public static void rss(StaplerRequest req, StaplerResponse rsp, String title, String url, RunList runList, FeedAdapter<Run> feedAdapter) throws IOException, ServletException {
        final FeedAdapter<Run> feedAdapter_ = feedAdapter == null ? Run.FEED_ADAPTER : feedAdapter;
        forwardToRss(title, url, runList, feedAdapter_, req, rsp);
    }
K
kohsuke 已提交
107
}