Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zufar.urlshortener.shorten.controller

import com.zufar.urlshortener.common.exception.ErrorResponse
import com.zufar.urlshortener.common.exception.InvalidRequestException
import com.zufar.urlshortener.shorten.dto.*
import com.zufar.urlshortener.shorten.repository.UrlRepository
import com.zufar.urlshortener.shorten.service.*
Expand Down Expand Up @@ -164,6 +165,12 @@ class UrlController(
httpServletRequest.getHeader("User-Agent")
)

val existingMapping = urlRepository.findByShortUrl(originalUrl)
if (existingMapping.isPresent) {
log.warn("Loop detected: originalUrl='{}' points to another short URL.", originalUrl)
throw InvalidRequestException("URL cannot point to a loop back address")
}

val urlHash = StringEncoder.encode(originalUrl)
val urlMapping = urlRepository.findByUrlHash(urlHash)
val shortUrl: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface UrlRepository : MongoRepository<UrlMapping, String> {
fun findByUrlHash(urlHash: String): Optional<UrlMapping>

fun findAllByUserId(userId: String, pageable: Pageable): Page<UrlMapping>

fun findByShortUrl(shortUrl: String): Optional<UrlMapping>
}