From 401add54419e880f05e29ddbcb18b5082c69b853 Mon Sep 17 00:00:00 2001 From: Gyubin-Han Date: Tue, 17 Jun 2025 19:06:19 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20URL=20=EB=8B=A8=EC=B6=95=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UrlValidationFailedException.java | 7 ++++++ .../gyu/urlShortener/service/MainService.java | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/main/java/be/gyu/urlShortener/exception/UrlValidationFailedException.java diff --git a/src/main/java/be/gyu/urlShortener/exception/UrlValidationFailedException.java b/src/main/java/be/gyu/urlShortener/exception/UrlValidationFailedException.java new file mode 100644 index 0000000..a8fc090 --- /dev/null +++ b/src/main/java/be/gyu/urlShortener/exception/UrlValidationFailedException.java @@ -0,0 +1,7 @@ +package be.gyu.urlShortener.exception; + +// TODO: url-short branch에서 추가 및 커밋할 것. +public class UrlValidationFailedException extends RuntimeException{ + public UrlValidationFailedException(){ super("적절한 HTTP(S) URL이 아닙니다."); } + public UrlValidationFailedException(String msg){ super(msg); } +} diff --git a/src/main/java/be/gyu/urlShortener/service/MainService.java b/src/main/java/be/gyu/urlShortener/service/MainService.java index 72b9b40..c785602 100644 --- a/src/main/java/be/gyu/urlShortener/service/MainService.java +++ b/src/main/java/be/gyu/urlShortener/service/MainService.java @@ -5,8 +5,11 @@ import org.springframework.stereotype.Service; import be.gyu.urlShortener.entity.UrlMap; import be.gyu.urlShortener.exception.ShortUrlNotFoundException; +import be.gyu.urlShortener.exception.UrlValidationFailedException; import be.gyu.urlShortener.repository.UrlMapRepository; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.time.LocalDateTime; import java.security.MessageDigest; @@ -92,4 +95,24 @@ public class MainService { return optional.get().getUrlMapOriginal(); } + + // 단축 URL 생성 메소드 (검증 및 생성) + public Map generateShortUrl(String originalUrl){ + Map result=new HashMap<>(); + + // 원본 URL 무결점 검증 (HTTP(S) URL이 맞는지 검증) + boolean isCorrectUrl=validOriginalUrl(originalUrl); + if(!isCorrectUrl){ + // 올바른 HTTP(S) URL이 아닌 경우, 예외 발생 + throw new UrlValidationFailedException(); + } + + // 단축 URL 생성 + String shortUrl=createUrlShort(originalUrl); + result.put("status","success"); + result.put("message","생성 성공"); + result.put("shortUrl",shortUrl); + + return result; + } }