Search in sources :

Example 1 with Feed

use of org.opencastproject.feed.api.Feed in project opencast by opencast.

the class AbstractFeedGenerator method createFeed.

/**
 * {@inheritDoc}
 */
public final Feed createFeed(Feed.Type type, String[] query, int size, Organization organization) {
    logger.debug("Started to create {} feed", type);
    SearchResult result = null;
    if (type == null)
        throw new IllegalArgumentException("Feed type must not be null");
    if (size <= 0) {
        logger.trace("Using the feed's configured size of {}", this.size);
        size = this.size;
    }
    // Check if the feed generator is correctly set up
    if (uri == null)
        throw new IllegalStateException("Feed uri (feed.uri) must be configured");
    if (name == null)
        throw new IllegalStateException("Feed name (feed.name) must be configured");
    if (getHome(organization) == null)
        throw new IllegalStateException("Feed url (feed.home) must be configured");
    if (getLinkTemplate(organization) == null)
        throw new IllegalStateException("Feed link template (feed.entry) must be configured");
    // Have the concrete implementation load the feed data
    result = loadFeedData(type, query, size, DEFAULT_OFFSET);
    if (result == null) {
        logger.debug("Cannot retrieve solr result for feed '{}' with query '{}'", type.toString(), query);
        return null;
    }
    // Create the feed
    Feed f = createFeed(type, getIdentifier(), new ContentImpl(getName()), new ContentImpl(getDescription()), getFeedLink(organization));
    f.setEncoding(ENCODING);
    // Set iTunes tags
    ITunesFeedExtension iTunesFeed = new ITunesFeedExtension();
    f.addModule(iTunesFeed);
    if (cover != null)
        f.setImage(new ImageImpl(cover, "Feed Image"));
    // Check if a default format has been specified
    // TODO: Parse flavor and set member variable rssTrackFlavor
    // String rssFlavor = query.length > 1 ? query[query.length - 1] : null;
    // Iterate over the feed data and create the entries
    int itemCount = 0;
    for (SearchResultItem resultItem : result.getItems()) {
        try {
            if (resultItem.getType().equals(SearchResultItemType.Series))
                addSeries(f, query, resultItem, organization);
            else
                addEpisode(f, query, resultItem, organization);
        } catch (Throwable t) {
            logger.error("Error creating entry with id {} for feed {}", resultItem.getId(), this, t);
        }
        itemCount++;
        if (itemCount >= size)
            break;
    }
    return f;
}
Also used : SearchResultItem(org.opencastproject.search.api.SearchResultItem) SearchResult(org.opencastproject.search.api.SearchResult) Feed(org.opencastproject.feed.api.Feed)

Example 2 with Feed

use of org.opencastproject.feed.api.Feed in project opencast by opencast.

the class FeedServiceImpl method getFeed.

/*
   * Note: We're using Regex matching for the path here, instead of normal JAX-RS paths.  Previously this class was a servlet,
   * which was fine except that it had auth issues.  Removing the servlet fixed the auth issues, but then the paths (as written
   * in the RestQuery docs) don't work because  JAX-RS does not support having "/" characters as part of the variable's value.
   *
   * So, what we've done instead is match everything that comes in under the /feeds/ namespace, and then substring it out the way
   * the old servlet code did.  But without the servlet, or auth issues :)
   */
@GET
@Produces(MediaType.TEXT_XML)
@Path("{query: .*}")
// FIXME: These Opencast REST classes do not support this path style, and need to have that support added
@RestQuery(name = "getFeed", description = "Gets an Atom or RSS feed", pathParameters = { @RestParameter(description = "The feed type", name = "type", type = Type.STRING, isRequired = true), @RestParameter(description = "The feed version", name = "version", type = Type.STRING, isRequired = true), @RestParameter(description = "The feed query", name = "query", type = Type.STRING, isRequired = true) }, reponses = { @RestResponse(description = "Return the feed of the appropriate type", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "", responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) }, returnDescription = "")
public Response getFeed(@Context HttpServletRequest request) {
    String contentType = null;
    logger.debug("Requesting RSS or Atom feed.");
    FeedInfo feedInfo = null;
    Organization organization = securityService.getOrganization();
    // Try to extract requested feed type and content
    try {
        feedInfo = extractFeedInfo(request);
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Set the content type
    if (feedInfo.getType().equals(Feed.Type.Atom))
        contentType = "application/atom+xml";
    else if (feedInfo.getType().equals(Feed.Type.RSS))
        contentType = "application/rss+xml";
    // Have a feed generator create the requested feed
    Feed feed = null;
    for (FeedGenerator generator : feeds) {
        if (generator.accept(feedInfo.getQuery())) {
            feed = generator.createFeed(feedInfo.getType(), feedInfo.getQuery(), feedInfo.getSize(), organization);
            if (feed == null) {
                return Response.serverError().build();
            }
            break;
        }
    }
    // Have we found a feed generator?
    if (feed == null) {
        logger.debug("RSS/Atom feed could not be generated");
        return Response.status(Status.NOT_FOUND).build();
    }
    // Set character encoding
    Variant v = new Variant(MediaType.valueOf(contentType), null, feed.getEncoding());
    String outputString = null;
    try {
        if (feedInfo.getType().equals(Feed.Type.RSS)) {
            logger.debug("Creating RSS feed output.");
            SyndFeedOutput output = new SyndFeedOutput();
            outputString = output.outputString(new RomeRssFeed(feed, feedInfo));
        } else {
            logger.debug("Creating Atom feed output.");
            WireFeedOutput output = new WireFeedOutput();
            outputString = output.outputString(new RomeAtomFeed(feed, feedInfo));
        }
    } catch (FeedException e) {
        return Response.serverError().build();
    }
    return Response.ok(outputString, v).build();
}
Also used : Variant(javax.ws.rs.core.Variant) Organization(org.opencastproject.security.api.Organization) FeedGenerator(org.opencastproject.feed.api.FeedGenerator) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) FeedException(com.rometools.rome.io.FeedException) Feed(org.opencastproject.feed.api.Feed) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

Feed (org.opencastproject.feed.api.Feed)2 FeedException (com.rometools.rome.io.FeedException)1 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)1 WireFeedOutput (com.rometools.rome.io.WireFeedOutput)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Variant (javax.ws.rs.core.Variant)1 FeedGenerator (org.opencastproject.feed.api.FeedGenerator)1 SearchResult (org.opencastproject.search.api.SearchResult)1 SearchResultItem (org.opencastproject.search.api.SearchResultItem)1 Organization (org.opencastproject.security.api.Organization)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1