io.kamax.mxisd.dns.FederationDnsOverwrite.java Source code

Java tutorial

Introduction

Here is the source code for io.kamax.mxisd.dns.FederationDnsOverwrite.java

Source

/*
 * mxisd - Matrix Identity Server Daemon
 * Copyright (C) 2017 Maxime Dor
 *
 * https://max.kamax.io/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.kamax.mxisd.dns;

import io.kamax.mxisd.config.DnsOverwriteConfig;
import io.kamax.mxisd.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.kamax.mxisd.config.DnsOverwriteConfig.Entry;

@Component
public class FederationDnsOverwrite {

    private Logger log = LoggerFactory.getLogger(FederationDnsOverwrite.class);

    private ServerConfig srvCfg;
    private Map<String, Entry> mappings;

    @Autowired
    public FederationDnsOverwrite(DnsOverwriteConfig cfg, ServerConfig srvCfg) {
        this.srvCfg = srvCfg;

        mappings = new HashMap<>();
        cfg.getHomeserver().getFederation().forEach(e -> mappings.put(e.getName(), e));
    }

    public Optional<String> findHost(String lookup) {
        Entry mapping = mappings.get(lookup);
        if (mapping == null) {
            return Optional.empty();
        }

        return Optional.of(mapping.getValue());
    }

}