me.bulat.jivr.webmin.web.defended.admin.ConsulController.java Source code

Java tutorial

Introduction

Here is the source code for me.bulat.jivr.webmin.web.defended.admin.ConsulController.java

Source

/*
 * Licensed to JIVR under one or more contributor
 *  license agreements. See the NOTICE file distributed with
 *  this work for additional information regarding copyright
 *  ownership. JIVR licenses this file to you under
 *  the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package me.bulat.jivr.webmin.web.defended.admin;

import me.bulat.jivr.consul.manager.ConsulManager;
import me.bulat.jivr.consul.model.NodeKey;
import me.bulat.jivr.consul.model.ServiceKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alex Bogatikov
 *         Created on 16/09/16.
 */
@Controller
public class ConsulController {

    private ConsulManager consul;

    @Autowired
    public ConsulController(final ConsulManager consul) {
        this.consul = consul;
    }

    @RequestMapping(value = { "/admin/consul" }, method = { RequestMethod.GET })
    public ModelAndView consulPage() {
        List<String> services = consul.getServiceList().stream().map(ServiceKey::getServiceName)
                .collect(Collectors.toList());
        List<String> nodes = consul.getNodeList().stream().map(NodeKey::getNodeName).collect(Collectors.toList());
        ModelAndView model = new ModelAndView();
        NodeForm nodeForm = new NodeForm();
        ServiceForm serviceForm = new ServiceForm();
        model.addObject("title", "Jivr Web Console Nodes page");
        model.addObject("message", "Nodes Page!");
        model.setViewName("admin/consul");
        model.addObject("nodeForm", nodeForm);
        model.addObject("services", services);
        model.addObject("nodes", nodes);
        model.addObject("nodeToDelete", new NodeToDelete());
        model.addObject("serviceToDelete", new ServiceToDelete());
        model.addObject("serviceForm", serviceForm);
        return model;
    }

    @RequestMapping(value = { "/admin/consul/addhost" }, method = { RequestMethod.POST })
    public ModelAndView addNode(@Valid @ModelAttribute("nodeForm") NodeForm nodeForm, BindingResult binding) {
        List<String> nodes = consul.getNodeList().stream().map(NodeKey::getNodeName).collect(Collectors.toList());
        ModelAndView model;
        if (binding.hasErrors()) {
            model = consulPage();
            model.addObject("error", "Incorrect data!");
            return model;
        } else {
            if (nodes.contains(nodeForm.getNodeName())) {
                model = consulPage();
                model.addObject("error", "Node exist!");
            } else {
                consul.addNode(nodeForm.getNodeName(), nodeForm.getServices());
                model = consulPage();
                model.addObject("result", "Adding success!");
            }
            return model;
        }
    }

    @RequestMapping(value = { "/admin/consul/addservice" }, method = { RequestMethod.POST })
    public ModelAndView addNode(@Valid @ModelAttribute("serviceForm") ServiceForm serviceForm,
            BindingResult binding) {
        List<String> services = consul.getServiceList().stream().map(ServiceKey::getServiceName)
                .collect(Collectors.toList());
        ModelAndView model;
        if (binding.hasErrors()) {
            model = consulPage();
            model.addObject("error", "Incorrect data!");
            return model;
        } else {
            if (services.contains(serviceForm.getServiceName())) {
                model = consulPage();
                model.addObject("error", "Service exist!");
            } else {
                consul.addService(serviceForm.getServiceName(), serviceForm.getAgiScriptClassName(),
                        serviceForm.getHost(), serviceForm.getPort(), Boolean.valueOf(serviceForm.getActive()),
                        serviceForm.getConf());
                model = consulPage();
                model.addObject("result", "Adding success!");
            }
            return model;
        }
    }

    @RequestMapping(value = { "/admin/consul/deleteservice" }, method = { RequestMethod.POST })
    public ModelAndView deleteService(@ModelAttribute("serviceToDelete") ServiceToDelete service) {
        consul.deleteService(service.getServiceName());
        return consulPage();
    }

    @RequestMapping(value = { "/admin/consul/deletenode" }, method = { RequestMethod.POST })
    public ModelAndView deleteNode(@ModelAttribute("nodeToDelete") NodeToDelete node) {
        consul.deleteNode(node.getNodeName());
        return consulPage();
    }
}