-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCsvReaderService.java
57 lines (45 loc) · 1.86 KB
/
CsvReaderService.java
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
package community.whatever.onembackendjava.api.service;
import community.whatever.onembackendjava.api.domain.BlockedDomainInterface;
import community.whatever.onembackendjava.common.error.BusinessExceptionGenerator;
import community.whatever.onembackendjava.common.error.model.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
@Slf4j
@Service
public class CsvReaderService implements BlockedDomainInterface {
private Set<String> blockedDomains = new HashSet<>();
// CSV 파일을 읽어 BlockedDomains 리스트에 저장
public void loadBlockedDomains(MultipartFile file) {
if (file.isEmpty()) throw BusinessExceptionGenerator.createBusinessException(ErrorCode.DB001);
Set<String> domainSet = new HashSet<>();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
domainSet.add(line.trim());
}
} catch (Exception e) {
throw BusinessExceptionGenerator.createBusinessException("RU001", e.getMessage());
}
this.blockedDomains = domainSet; // 새로운 데이터로 업데이트
}
// 차단된 도메인 목록 반환
public Set<String> getBlockedDomains() {
return blockedDomains;
}
// URL이 차단된 도메인에 포함되는지 확인
public boolean isBlocked(String url) {
for (String domain : blockedDomains) {
if (url.contains(domain)) {
return true;
}
}
return false;
}
}