ChildNodeController.java 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package adt.mainnode.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@RestController
@RequestMapping("child-node")
@Api(
        value = "/child-node",
        tags = {"Child Node"}
)
@Slf4j
public class ChildNodeController {

    @GetMapping
    public void downloadChildNodeJar(HttpServletRequest request, HttpServletResponse response) {

        try {

            log.info("ADT jar download request received from {}", request.getRemoteAddr());

            InputStream childNodeJar = getClass().getClassLoader().getResourceAsStream("curex_child_node.jar");

            response.setContentType("application/java-archive");
            response.setHeader("Content-Disposition", "attachment; filename=\"curex_child_node.jar\"");

            IOUtils.copy(Objects.requireNonNull(childNodeJar), response.getOutputStream());

            log.info("Starting download of child node jar...");

            response.flushBuffer();

        } catch (IOException ex) {

            log.error("Error writing to child node output stream.");
            throw new RuntimeException("Error writing to output stream.");
        }

    }

}