NewPortController.java 1.83 KB
Newer Older
David Fernandez Lobon's avatar
David Fernandez Lobon committed
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 52 53 54 55 56 57 58 59 60 61 62 63
package adt.mainnode.controller;

import adt.mainnode.dto.Asset;
import adt.mainnode.entity.Credentials;
import adt.mainnode.entity.PortDetection;
import adt.mainnode.repository.CredentialsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("sniffing")
@Api(
        value = "/sniffing",
        tags = {"Add data to sniffing snapshots"}
)
@Slf4j
public class NewPortController {

    private final CredentialsRepository credentialsRepository;

    @Autowired
    public NewPortController(CredentialsRepository credentialsRepository) {
        this.credentialsRepository = credentialsRepository;
    }

    @CrossOrigin
    @ApiOperation(value = "Save a new set of a list of new ports", response = Long.class)
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    Long saveAssets(HttpServletResponse response, @RequestBody Asset asset, @RequestParam String token) {
        Credentials credential = credentialsRepository.findByToken(token);

        if(credential != null && credential.getStatus().equals("OPERATIVE")) {

            if(credential.getAvailableEndpoint().contains("(POST) sniffing/")) {

                log.info("New assets received");

               //Guardar la lista

                return null;

            }
        }

        try {
            response.sendError(400, "Unrecognized token. Access denied.");
        }catch(IOException ioe){
            //
        }

        return null;
    }
}