Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
2.0 kB
3
Indexable
Never
package com.sec.smp.rm.service.filereader;

import com.sec.smp.rm.enums.ResponseCode;
import com.sec.smp.rm.exceptions.ForwardException;
import com.sec.smp.rm.service.aidregister.helper.ApplicationManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class CSVReaderService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CSVReaderService.class);

    public void processFile(MultipartFile file) throws IOException {
        validateFileType(file);

        Map<String, Set<String>> clientIPMap = new HashMap<>();
        String cvsSplitBy = "\t";

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            String line;

            while ((line = reader.readLine()) != null) {
                String[] data = line.split(cvsSplitBy);

                if (data.length != 2) {
                    LOGGER.debug("Invalid data format in file: {}", Arrays.toString(data));
                    
                    continue; // Skip this line and move to the next one
                }
                String aid = data[0];
                String ip = data[1];
            }

            for (Map.Entry<String, Set<String>> entry : clientIPMap.entrySet()) {
                ApplicationManager.getInstance().setInHashset(entry.getKey(), entry.getValue());
            }
        }
    }

    private void validateFileType(MultipartFile file) {
        // Check if the file type is not .csv
        if (!file.getOriginalFilename().endsWith(".csv")) {
            throw new ForwardException(ResponseCode.INVALID_FILE_TYPE);
        }
    }
}
Leave a Comment