예제 #1
0
  @Override
  public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    Writer writer = null;
    try {
      writer = res.getWriter();

      // Get the section placed in the request context by ApplicationDataInterceptor
      RequestContext requestContext = ThreadLocalRequestContext.getRequestContext();
      Section section = (Section) requestContext.getValue("section");
      WebSite webSite = (WebSite) requestContext.getValue("webSite");

      // Get the collection property from the page definition
      String collectionName = requestContext.getPage().getProperty("collection");
      if (collectionName == null)
        throw new WebScriptException("collectionName property must be supplied");

      // Fetch the named collection
      AssetCollection collection =
          (AssetCollection) collectionFactory.getCollection(section.getId(), collectionName);

      // Use ROME library to output the colleciton as a syndication feed
      SyndFeed feed = new SyndFeedImpl();
      feed.setFeedType(FEED_TYPE);

      feed.setTitle(section.getTitle());
      feed.setLink(urlUtils.getWebsiteDomain(webSite) + urlUtils.getUrl(section));
      feed.setDescription(section.getDescription());

      List<SyndEntry> entries = new ArrayList<SyndEntry>();
      for (Asset asset : collection.getAssets()) {
        SyndEntry entry = new SyndEntryImpl();
        entry.setTitle(asset.getTitle());
        entry.setLink(urlUtils.getWebsiteDomain(webSite) + urlUtils.getUrl(asset));
        entry.setUri(urlUtils.getWebsiteDomain(webSite) + urlUtils.getShortUrl(asset));
        entry.setPublishedDate((Date) asset.getProperties().get(Asset.PROPERTY_PUBLISHED_TIME));
        SyndContent description = new SyndContentImpl();
        description.setType("text/html");
        description.setValue(asset.getDescription());
        entry.setDescription(description);
        entries.add(entry);
      }
      feed.setEntries(entries);

      SyndFeedOutput output = new SyndFeedOutput();
      output.output(feed, writer);
      writer.flush();
    } catch (IOException e) {
      log.error("Unable to output rss", e);
    } catch (FeedException e) {
      log.error("Unable to output rss", e);
    }
  }
  @Test
  public void testMountedResourceResponse() throws IOException, FeedException {
    tester.startResource(new RSSProducerResource());
    String responseTxt = tester.getLastResponse().getDocument();
    // write the RSS feed used in the test into a ByteArrayOutputStream
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(outputStream);
    SyndFeedOutput output = new SyndFeedOutput();

    output.output(RSSProducerResource.getFeed(), writer);
    // the response and the written RSS must be equal
    Assert.assertEquals(responseTxt, outputStream.toString());
  }
예제 #3
0
  @Path("/entriesAsFeed")
  @GET
  @ApiOperation(value = "Get category entries as feed", notes = "Get a feed of category entries")
  @Produces(MediaType.APPLICATION_XML)
  @SecurityCheck(value = Role.USER, apiKeyAllowed = true)
  public Response getCategoryEntriesAsFeed(
      @ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id")
          String id) {

    Preconditions.checkNotNull(id);

    ReadingMode readType = ReadingMode.all;
    ReadingOrder order = ReadingOrder.desc;
    int offset = 0;
    int limit = 20;

    Entries entries =
        (Entries)
            getCategoryEntries(id, readType, null, offset, limit, order, null, false).getEntity();

    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("CommaFeed - " + entries.getName());
    feed.setDescription("CommaFeed - " + entries.getName());
    String publicUrl = applicationSettingsService.get().getPublicUrl();
    feed.setLink(publicUrl);

    List<SyndEntry> children = Lists.newArrayList();
    for (Entry entry : entries.getEntries()) {
      children.add(entry.asRss());
    }
    feed.setEntries(children);

    SyndFeedOutput output = new SyndFeedOutput();
    StringWriter writer = new StringWriter();
    try {
      output.output(feed, writer);
    } catch (Exception e) {
      writer.write("Could not get feed information");
      log.error(e.getMessage(), e);
    }
    return Response.ok(writer.toString()).build();
  }
예제 #4
0
  // Logs a new ATOM entry
  public static synchronized void addATOMEntry(
      String title, String link, String description, File atomFile, String context) {
    try {

      if (atomFile.exists()) {

        // System.out.println("ATOM file found!");
        /** Namespace URI for content:encoded elements */
        String CONTENT_NS = "http://www.w3.org/2005/Atom";

        /** Parses RSS or Atom to instantiate a SyndFeed. */
        SyndFeedInput input = new SyndFeedInput();

        /** Transforms SyndFeed to RSS or Atom XML. */
        SyndFeedOutput output = new SyndFeedOutput();

        // Load the feed, regardless of RSS or Atom type
        SyndFeed feed = input.build(new XmlReader(atomFile));

        // Set the output format of the feed
        feed.setFeedType("atom_1.0");

        List<SyndEntry> items = feed.getEntries();
        int numItems = items.size();
        if (numItems > 9) {
          items.remove(0);
          feed.setEntries(items);
        }

        SyndEntry newItem = new SyndEntryImpl();
        newItem.setTitle(title);
        newItem.setLink(link);
        newItem.setUri(link);
        SyndContent desc = new SyndContentImpl();
        desc.setType("text/html");
        desc.setValue(description);
        newItem.setDescription(desc);
        desc.setType("text/html");
        newItem.setPublishedDate(new java.util.Date());

        List<SyndCategory> categories = new ArrayList<SyndCategory>();
        if (CommonConfiguration.getProperty("htmlTitle", context) != null) {
          SyndCategory category2 = new SyndCategoryImpl();
          category2.setName(CommonConfiguration.getProperty("htmlTitle", context));
          categories.add(category2);
        }
        newItem.setCategories(categories);
        if (CommonConfiguration.getProperty("htmlAuthor", context) != null) {
          newItem.setAuthor(CommonConfiguration.getProperty("htmlAuthor", context));
        }
        items.add(newItem);
        feed.setEntries(items);

        feed.setPublishedDate(new java.util.Date());

        FileWriter writer = new FileWriter(atomFile);
        output.output(feed, writer);
        writer.toString();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the ATOM file.");
      ioe.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the ATOM file.");
      e.printStackTrace();
    }
  }
예제 #5
0
 /** send the output to designated Writer */
 public void output(java.io.Writer writer) throws FeedException, IOException {
   SyndFeedOutput feedWriter = new SyndFeedOutput();
   feedWriter.output(feed, writer);
 }
  @SuppressWarnings("unchecked")
  private void serializeToFeed(
      OutputStream outputStream,
      GeolocResultsDto geolocResultsDto,
      String feedVersion,
      int startPaginationIndex) {
    SyndFeed synFeed = new SyndFeedImpl();
    Writer oWriter = null;
    try {

      synFeed.setFeedType(feedVersion);

      synFeed.setTitle(Constants.FEED_TITLE);
      synFeed.setLink(Constants.FEED_LINK);
      synFeed.setDescription(Constants.FEED_DESCRIPTION);
      List<SyndEntry> entries = new ArrayList<SyndEntry>();

      for (GisFeatureDistance gisFeatureDistance : geolocResultsDto.getResult()) {

        SyndEntry entry = new SyndEntryImpl();
        GeoRSSModule geoRSSModuleGML = new GMLModuleImpl();
        OpenSearchModule openSearchModule = new OpenSearchModuleImpl();

        geoRSSModuleGML.setLatitude(gisFeatureDistance.getLat());
        geoRSSModuleGML.setLongitude(gisFeatureDistance.getLng());

        openSearchModule.setItemsPerPage(Pagination.DEFAULT_MAX_RESULTS);
        openSearchModule.setTotalResults(geolocResultsDto.getNumFound());
        openSearchModule.setStartIndex(startPaginationIndex);

        entry.getModules().add(openSearchModule);
        entry.getModules().add(geoRSSModuleGML);
        entry.setTitle(gisFeatureDistance.getName());
        entry.setAuthor(Constants.MAIL_ADDRESS);
        entry.setLink(Constants.GISFEATURE_BASE_URL + +gisFeatureDistance.getFeatureId());
        SyndContent description = new SyndContentImpl();
        description.setType(OutputFormat.ATOM.getContentType());
        description.setValue(gisFeatureDistance.getName());
        entry.setDescription(description);
        entries.add(entry);
      }

      synFeed.setEntries(entries);

      try {
        oWriter = new OutputStreamWriter(outputStream, Constants.CHARSET);
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("unknow encoding " + Constants.CHARSET);
      }

      // Copy synfeed to output
      SyndFeedOutput output = new SyndFeedOutput();
      try {
        output.output(synFeed, oWriter);
        // Flush
        oWriter.flush();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }

    } finally {
      if (oWriter != null)
        try {
          oWriter.close();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      if (outputStream != null)
        try {
          outputStream.close();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
    }
  }