com.seajas.search.profiler.wsdl.FeedTesting.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.wsdl.FeedTesting.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.profiler.wsdl;

import com.seajas.search.bridge.profiler.model.feed.Feed;
import com.seajas.search.profiler.service.profiler.ProfilerService;
import com.seajas.search.profiler.service.testing.TestingException;
import com.seajas.search.profiler.service.testing.TestingService;
import com.seajas.search.profiler.wsdl.FeedResult.Status;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * Simple feed testing service implementation.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@WebService(endpointInterface = "com.seajas.search.profiler.wsdl.IFeedTesting")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@Transactional
public class FeedTesting implements IFeedTesting {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(FeedTesting.class);

    /**
     * Profiler service.
     */
    @Autowired
    private ProfilerService profilerService;

    /**
     * Testing service.
     */
    @Autowired
    private TestingService testingService;

    /**
     * {@inheritDoc}
     */
    @Override
    public FeedResult isValidFeedUrl(final String url, final Boolean ignoreExisting) {
        FeedResult result = new FeedResult();

        if (!Boolean.TRUE.equals(ignoreExisting)) {
            List<Feed> existingFeeds = profilerService.getFeedsByUrl(url);

            if (existingFeeds != null && existingFeeds.size() > 0) {
                if (logger.isInfoEnabled())
                    logger.info("The given feed URL already exists");

                result.setStatus(Status.ALREADY_EXISTS);

                return result;
            }
        }

        // Validate the URL first

        URI validatedUri;

        try {
            validatedUri = new URI(url);

            if (!StringUtils.hasText(validatedUri.getScheme()) || !StringUtils.hasText(validatedUri.getHost()))
                throw new URISyntaxException("", "No scheme and/or host provided");
        } catch (URISyntaxException e) {
            if (logger.isInfoEnabled())
                logger.info("The given feed URL is not valid", e);

            result.setStatus(Status.UNKNOWN);

            return result;
        }

        // Then run it through the testing service for exploration

        try {
            Map<String, Boolean> testingResult = testingService.exploreUri(validatedUri);

            if (logger.isInfoEnabled())
                logger.info("Exploration resulted in " + testingResult.size() + " result URL(s)");

            List<String> validUrls = new ArrayList<String>();

            for (Entry<String, Boolean> testingResultEntry : testingResult.entrySet()) {
                validUrls.add(testingResultEntry.getKey());

                if (testingResultEntry.getValue()) {
                    if (logger.isInfoEnabled())
                        logger.info("The given URL '" + url + "' is a direct RSS feed");

                    result.setStatus(Status.VALID);

                    break;
                } else
                    result.setStatus(Status.INDIRECTLY_VALID);
            }

            result.setValidUrls(validUrls);
        } catch (TestingException e) {
            if (logger.isInfoEnabled())
                logger.info("URL exploration was unsuccessful", e);

            result.setStatus(Status.UNKNOWN);
        }

        return result;
    }
}