001package voyeur;
002/*-
003 * ##########################################################################
004 * Local Area Network Voyeur
005 * %%
006 * Copyright (C) 2019 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.spring.AbstractController;
022import ball.upnp.ssdp.SSDPMessage;
023import java.net.URI;
024import java.util.List;
025import java.util.Map;
026import java.util.concurrent.ConcurrentSkipListMap;
027import lombok.NoArgsConstructor;
028import lombok.ToString;
029import lombok.extern.log4j.Log4j2;
030import org.springframework.beans.factory.annotation.Autowired;
031import org.springframework.stereotype.Controller;
032import org.springframework.ui.Model;
033import org.springframework.web.bind.annotation.ModelAttribute;
034import org.springframework.web.bind.annotation.RequestMapping;
035
036import static java.util.stream.Collectors.groupingBy;
037import static java.util.stream.Collectors.mapping;
038import static java.util.stream.Collectors.toList;
039
040/**
041 * UI {@link Controller} implementation.
042 *
043 * {@injected.fields}
044 *
045 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
046 */
047@Controller
048@NoArgsConstructor @ToString @Log4j2
049public class UIController extends AbstractController {
050    @Autowired private SSDP ssdp = null;
051    @Autowired private NetworkInterfaces interfaces = null;
052    @Autowired private ARPCache arp = null;
053    @Autowired private Nmap nmap = null;
054
055    @ModelAttribute("upnp")
056    public Map<URI,List<URI>> upnp() {
057        var map =
058            ssdp().values().stream()
059            .collect(groupingBy(SSDPMessage::getLocation,
060                                ConcurrentSkipListMap::new,
061                                mapping(SSDPMessage::getUSN, toList())));
062
063        return map;
064    }
065
066    @ModelAttribute("ssdp")
067    public SSDP ssdp() { return ssdp; }
068
069    @ModelAttribute("interfaces")
070    public NetworkInterfaces interfaces() { return interfaces; }
071
072    @ModelAttribute("arp")
073    public ARPCache arp() { return arp; }
074
075    @ModelAttribute("nmap")
076    public Nmap nmap() { return nmap; }
077
078    @RequestMapping(value = {
079                        "/",
080                        "/upnp/devices", "/upnp/ssdp",
081                        "/network/interfaces", "/network/arp", "/network/nmap"
082                    })
083    public String root(Model model) { return getViewName(); }
084
085    @RequestMapping(value = { "/index", "/index.htm", "/index.html" })
086    public String index() { return "redirect:/"; }
087}