com.seajas.search.bridge.contender.metadata.SyndEntryWriteConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.bridge.contender.metadata.SyndEntryWriteConverter.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.bridge.contender.metadata;

import com.seajas.search.utilities.web.WebFeeds;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;

/**
 * Convert the given SyndEntry to a Mongo object.
 *
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@WritingConverter
public class SyndEntryWriteConverter implements Converter<SyndEntry, String> {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(SyndEntryWriteConverter.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public String convert(final SyndEntry source) {
        SyndFeed feed = new SyndFeedImpl(), sourceFeed = source.getSource();

        try {
            WebFeeds.validate(feed, URI.create("http://foo/"));

            // Make this element a child of this feed temporarily

            source.setSource(feed);

            feed.getEntries().add(source);

            // And serialize it to an RSS 2.0 string

            SyndFeedOutput serializer = new SyndFeedOutput();

            return serializer.outputString(feed, true);
        } catch (FeedException e) {
            logger.error("Unable to generate new feed to wrap the given element into", e);

            return null;
        } finally {
            source.setSource(sourceFeed);
        }
    }
}