001/*******************************************************************************
002 * Copyright 2018 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018/**
019 *
020 */
021package org.mitre.openid.connect.service.impl;
022
023import java.util.Collection;
024
025import org.mitre.openid.connect.model.BlacklistedSite;
026import org.mitre.openid.connect.repository.BlacklistedSiteRepository;
027import org.mitre.openid.connect.service.BlacklistedSiteService;
028import org.springframework.beans.factory.annotation.Autowired;
029import org.springframework.stereotype.Service;
030import org.springframework.transaction.annotation.Transactional;
031
032import com.google.common.base.Strings;
033
034/**
035 * @author jricher
036 *
037 */
038@Service
039@Transactional(value="defaultTransactionManager")
040public class DefaultBlacklistedSiteService implements BlacklistedSiteService {
041
042        @Autowired
043        private BlacklistedSiteRepository repository;
044
045        /* (non-Javadoc)
046         * @see org.mitre.openid.connect.service.BlacklistedSiteService#getAll()
047         */
048        @Override
049        public Collection<BlacklistedSite> getAll() {
050                return repository.getAll();
051        }
052
053        /* (non-Javadoc)
054         * @see org.mitre.openid.connect.service.BlacklistedSiteService#getById(java.lang.Long)
055         */
056        @Override
057        public BlacklistedSite getById(Long id) {
058                return repository.getById(id);
059        }
060
061        /* (non-Javadoc)
062         * @see org.mitre.openid.connect.service.BlacklistedSiteService#remove(org.mitre.openid.connect.model.BlacklistedSite)
063         */
064        @Override
065        public void remove(BlacklistedSite blacklistedSite) {
066                repository.remove(blacklistedSite);
067        }
068
069        /* (non-Javadoc)
070         * @see org.mitre.openid.connect.service.BlacklistedSiteService#saveNew(org.mitre.openid.connect.model.BlacklistedSite)
071         */
072        @Override
073        public BlacklistedSite saveNew(BlacklistedSite blacklistedSite) {
074                return repository.save(blacklistedSite);
075        }
076
077        /* (non-Javadoc)
078         * @see org.mitre.openid.connect.service.BlacklistedSiteService#update(org.mitre.openid.connect.model.BlacklistedSite, org.mitre.openid.connect.model.BlacklistedSite)
079         */
080        @Override
081        public BlacklistedSite update(BlacklistedSite oldBlacklistedSite, BlacklistedSite blacklistedSite) {
082                return repository.update(oldBlacklistedSite, blacklistedSite);
083        }
084
085        /* (non-Javadoc)
086         * @see org.mitre.openid.connect.service.BlacklistedSiteService#isBlacklisted(java.lang.String)
087         */
088        @Override
089        public boolean isBlacklisted(String uri) {
090
091                if (Strings.isNullOrEmpty(uri)) {
092                        return false; // can't be blacklisted if you don't exist
093                }
094
095                Collection<BlacklistedSite> sites = getAll();
096
097                // TODO: rewrite this to do regex matching and use the Guava predicates collection
098
099                for (BlacklistedSite blacklistedSite : sites) {
100                        if (Strings.nullToEmpty(blacklistedSite.getUri()).equals(uri)) {
101                                return true;
102                        }
103                }
104
105                return false;
106        }
107
108}