Java tutorial
/** * 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.service.category; import com.seajas.search.bridge.profiler.model.feed.Feed; import com.seajas.search.bridge.profiler.model.feed.FeedUrl; import com.seajas.search.profiler.service.profiler.ProfilerService; import com.seajas.search.profiler.wsdl.FeedCategory; import com.seajas.search.profiler.wsdl.FeedCategoryDomain; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class CategoryService { @Autowired private ProfilerService profilerService; /** * FIXME * * @param includeDisabled * @return List<FeedCategory> */ public List<FeedCategory> getCategories(boolean includeDisabled) { List<Feed> feeds = profilerService.getFeeds(); List<FeedCategory> categories = new ArrayList<FeedCategory>(); // Social category FeedCategory socialCategory = new FeedCategory(); socialCategory.setName("social"); socialCategory.setDescription("Social API's"); socialCategory.getDomains() .add(new FeedCategoryDomain("twitter.com", Arrays.asList("api.twitter.com", "search.twitter.com"))); socialCategory.getDomains().add(new FeedCategoryDomain("facebook.com", "graph.facebook.com")); categories.add(socialCategory); // Image category FeedCategory imageCategory = new FeedCategory(); imageCategory.setName("image"); imageCategory.setDescription("Image API's"); imageCategory.getDomains().add(new FeedCategoryDomain("instagram.com", "api.instagram.com")); imageCategory.getDomains().add(new FeedCategoryDomain("flickr.com", "secure.flickr.com")); categories.add(imageCategory); // Video category FeedCategory videoCategory = new FeedCategory(); videoCategory.setName("video"); videoCategory.setDescription("Video API's"); videoCategory.getDomains().add(new FeedCategoryDomain("youtube.com", "gdata.youtube.com")); categories.add(videoCategory); // Web category FeedCategory webCategory = new FeedCategory(); webCategory.setName("web"); webCategory.setDescription("Web result API's"); webCategory.getDomains().add(new FeedCategoryDomain("google.com", "www.googleapis.com")); webCategory.getDomains().add(new FeedCategoryDomain("wordpress.com", "search.wordpress.com")); categories.add(webCategory); // RSS category FeedCategory rssCategory = new FeedCategory(); rssCategory.setName("rss"); rssCategory.setDescription("RSS feeds"); fillRssCategory(rssCategory, profilerService.getFeeds(), categories, includeDisabled); categories.add(rssCategory); return categories; } /** * Fill the RSS category based on those domains not belonging to any other categories. * * @param rssCategory * @param feeds */ private void fillRssCategory(final FeedCategory rssCategory, final List<Feed> feeds, final List<FeedCategory> otherCategories, final Boolean includeDisabled) { for (Feed feed : feeds) { if (!includeDisabled && !feed.getIsEnabled()) continue; for (FeedUrl feedUrl : feed.getFeedUrls()) { URI uri = URI.create(feedUrl.getUrl()); if (uri != null) { String host = uri.getHost(), cleanHost = cleanUrl(host); // Check if it already exists if (!exists(cleanHost, otherCategories) && !exists(cleanHost, rssCategory)) { FeedCategoryDomain domain = new FeedCategoryDomain(); domain.setUrl(cleanHost); domain.setOriginal(Arrays.asList(host)); rssCategory.getDomains().add(domain); } } } } } public boolean exists(final String domain, final List<FeedCategory> categories) { for (FeedCategory category : categories) if (exists(domain, category)) return true; return false; } public boolean exists(final String domain, final FeedCategory category) { for (FeedCategoryDomain otherDomain : category.getDomains()) if (otherDomain.getUrl().equals(domain)) return true; return false; } /** * Clean the given hostname. * * @param host * @return String */ private String cleanUrl(final String host) { String result = host; result = result.replace("www.", ""); result = result.replace("rss.", ""); result = result.replace("feed.", ""); result = result.replace("feeds.", ""); result = result.replace("secure.", ""); result = result.replace("search.", ""); result = result.replace("api.", ""); return result; } }