diff --git a/src/main/java/com/sangchu/patent/controller/PatentController.java b/src/main/java/com/sangchu/patent/controller/PatentController.java index 9614c92..d83941e 100644 --- a/src/main/java/com/sangchu/patent/controller/PatentController.java +++ b/src/main/java/com/sangchu/patent/controller/PatentController.java @@ -12,11 +12,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import java.io.IOException; -import java.net.URISyntaxException; @RestController @RequiredArgsConstructor @@ -30,7 +25,7 @@ public ResponseEntity> checkPatent( @RequestParam("keyword") String keyword, @RequestParam("custom") String custom, @RequestParam("storeNm") String storeNm - ) throws URISyntaxException, IOException, ParserConfigurationException, SAXException { + ) { PreferNameCreateDto preferName = PreferNameCreateDto.builder().keyword(keyword).custom(custom).name(storeNm).build(); preferService.createPreferName(preferName); Boolean response = patentService.checkDuplicated(storeNm); diff --git a/src/main/java/com/sangchu/patent/service/PatentService.java b/src/main/java/com/sangchu/patent/service/PatentService.java index 96ce776..63f7320 100644 --- a/src/main/java/com/sangchu/patent/service/PatentService.java +++ b/src/main/java/com/sangchu/patent/service/PatentService.java @@ -2,24 +2,25 @@ import com.sangchu.global.exception.custom.CustomException; import com.sangchu.global.util.statuscode.ApiStatus; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + import org.springframework.beans.factory.annotation.Value; + import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.w3c.dom.Document; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; + import java.io.ByteArrayInputStream; -import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -27,34 +28,44 @@ @RequiredArgsConstructor @Slf4j public class PatentService { - @Value("${kipris.service-key}") - private String serviceKey; - - @Value("${kipris.request-url}") - private String requestUrl; + @Value("${kipris.service-key}") + private String serviceKey; - private final RestTemplate restTemplate = new RestTemplate(); + @Value("${kipris.request-url}") + private String requestUrl; - public Boolean checkDuplicated(String storeNm) throws URISyntaxException, IOException, ParserConfigurationException, SAXException { - try { - String url = requestUrl + "?word=" + URLEncoder.encode(storeNm, StandardCharsets.UTF_8) + "&ServiceKey=" + serviceKey; - ResponseEntity response = restTemplate.getForEntity(new URI(url), String.class); + private final RestTemplate restTemplate = new RestTemplate(); - if (response.getStatusCode() != HttpStatus.OK) return false; + public Boolean checkDuplicated(String storeNm) { + try { + String url = + requestUrl + "?trademarkName=" + URLEncoder.encode(storeNm, StandardCharsets.UTF_8) + "&accessKey=" + + serviceKey; + ResponseEntity response = restTemplate.getForEntity(new URI(url), String.class); - String xml = response.getBody(); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); + if (response.getStatusCode() != HttpStatus.OK) + return false; - Document document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); + String xml = response.getBody(); - String totalCount = document.getElementsByTagName("totalCount").item(0).getTextContent(); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); - log.info("totalCount: " + totalCount); + Document document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); - return Integer.parseInt(totalCount) > 0; - } catch (Exception e) { - throw new CustomException(ApiStatus._PATENT_CHECK_FAIL); - } - } + String totalCountText = document.getElementsByTagName("TotalSearchCount").item(0).getTextContent(); + NodeList applicationStatusNodeList = document.getElementsByTagName("ApplicationStatus"); + boolean patentCheck = false; + for (int i = 0; i < applicationStatusNodeList.getLength(); i++) { + String applicationStatus = applicationStatusNodeList.item(i).getTextContent(); + if ("등록".equals(applicationStatus) || "출원".equals(applicationStatus)) { + patentCheck = true; + break; + } + } + return patentCheck || Integer.parseInt(totalCountText) > 0; + } catch (Exception e) { + throw new CustomException(ApiStatus._PATENT_CHECK_FAIL); + } + } }