Пример #1
0
 @SuppressWarnings("unchecked")
 public static SyndFeed addOpenSearchModule(
     SyndFeed feed, int itemsPerPage, int startIdx, int totalResult, String searchDescriptionUrl) {
   if (feed == null) {
     throw new NullPointerException("feed is NULL");
   }
   List<Module> mods = null;
   mods = feed.getModules();
   if (mods == null) {
     mods = new ArrayList<Module>();
   }
   OpenSearchModule osm = new OpenSearchModuleImpl();
   osm.setItemsPerPage(itemsPerPage);
   osm.setStartIndex(startIdx);
   osm.setTotalResults(totalResult);
   if (searchDescriptionUrl != null) {
     Link link = new Link();
     link.setHref(searchDescriptionUrl);
     link.setType("application/opensearchdescription+xml");
     osm.setLink(link);
   }
   mods.add(osm);
   feed.setModules(mods);
   return feed;
 }
  @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);
        }
    }
  }