From 359819995787bbc275ae49b41fdce9dcd95b0af5 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Fri, 11 Nov 2022 17:33:31 -0600 Subject: [PATCH] For random shorturl adjective-name pair is used --- src/main/java/tk/draganczuk/url/Routes.java | 19 +++++--- src/main/java/tk/draganczuk/url/Utils.java | 49 +++++++++++++++------ src/main/resources/public/index.html | 2 +- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/main/java/tk/draganczuk/url/Routes.java b/src/main/java/tk/draganczuk/url/Routes.java index 75ab61d..d141e8c 100644 --- a/src/main/java/tk/draganczuk/url/Routes.java +++ b/src/main/java/tk/draganczuk/url/Routes.java @@ -20,20 +20,25 @@ public class Routes { var body = req.body(); var split = body.split(";"); String longUrl = split[0]; + boolean unique = false; String shortUrl; try { shortUrl = split[1]; + shortUrl = shortUrl.toLowerCase(); + if (urlRepository.findForShortUrl(shortUrl).isEmpty()) { + unique = true; + } } catch (ArrayIndexOutOfBoundsException e) { - shortUrl = Utils.randomString(); + do { + shortUrl = Utils.randomName(); + if (urlRepository.findForShortUrl(shortUrl).isEmpty()) { + unique = true; + } + } while (unique == false); } - shortUrl = shortUrl.toLowerCase(); - - var shortUrlPresent = urlRepository - .findForShortUrl(shortUrl); - - if (shortUrlPresent.isEmpty() && Utils.validate(shortUrl)) { + if (unique && Utils.validate(shortUrl)) { return urlRepository.addUrl(longUrl, shortUrl); } else { res.status(HttpStatus.BAD_REQUEST_400); diff --git a/src/main/java/tk/draganczuk/url/Utils.java b/src/main/java/tk/draganczuk/url/Utils.java index 8410e5a..dafc615 100644 --- a/src/main/java/tk/draganczuk/url/Utils.java +++ b/src/main/java/tk/draganczuk/url/Utils.java @@ -8,20 +8,39 @@ public class Utils { private static final String SHORT_URL_PATTERN = "[a-z0-9-_]+"; private static final Pattern PATTERN = Pattern.compile(SHORT_URL_PATTERN); + + // The following lists are modified versions of the strings in + // https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go + + private static final String[] adjective = new String[] {"admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful", + "blissful", "bold", "boring", "brave", "busy", "charming", "clever", "compassionate", "competent", "condescending", "confident", "cool", + "cranky", "crazy", "dazzling", "determined", "distracted", "dreamy", "eager", "ecstatic", "elastic", "elated", "elegant", "eloquent", "epic", + "exciting", "fervent", "festive", "flamboyant", "focused", "friendly", "frosty", "funny", "gallant", "gifted", "goofy", "gracious", + "great", "happy", "hardcore", "heuristic", "hopeful", "hungry", "infallible", "inspiring", "intelligent", "interesting", "jolly", + "jovial", "keen", "kind", "laughing", "loving", "lucid", "magical", "modest", "musing", "mystifying", "naughty", "nervous", "nice", + "nifty", "nostalgic", "objective", "optimistic", "peaceful", "pedantic", "pensive", "practical", "priceless", "quirky", "quizzical", + "recursing", "relaxed", "reverent", "romantic", "sad", "serene", "sharp", "silly", "sleepy", "stoic", "strange", "stupefied", "suspicious", + "sweet", "tender", "thirsty", "trusting", "unruffled", "upbeat", "vibrant", "vigilant", "vigorous", "wizardly", "wonderful", "xenodochial", + "youthful", "zealous", "zen"}; - public static String randomString() { - int leftLimit = 48; // numeral '0' - int rightLimit = 122; // letter 'z' - int targetStringLength = 10; - - return random.ints(leftLimit, rightLimit + 1) - .filter(i -> (i <= 57 || i >= 97)) - .limit(targetStringLength) - .collect(StringBuilder::new, - StringBuilder::appendCodePoint, - StringBuilder::append) - .toString(); - } + private static final String[] name = new String[] {"agnesi", "albattani", "allen", "almeida", "antonelli", "archimedes", "ardinghelli", "aryabhata", "austin", + "babbage", "banach", "banzai", "bardeen", "bartik", "bassi", "beaver", "bell", "benz", "bhabha", "bhaskara", "black", "blackburn", "blackwell", + "bohr", "booth", "borg", "bose", "bouman", "boyd", "brahmagupta", "brattain", "brown", "buck", "burnell", "cannon", "carson", "cartwright", + "carver", "cauchy", "cerf", "chandrasekhar", "chaplygin", "chatelet", "chatterjee", "chaum", "chebyshev", "clarke", "cohen", "colden", "cori", + "cray", "curie", "curran", "darwin", "davinci", "dewdney", "dhawan", "diffie", "dijkstra", "dirac", "driscoll", "dubinsky", "easley", "edison", + "einstein", "elbakyan", "elgamal", "elion", "ellis", "engelbart", "euclid", "euler", "faraday", "feistel", "fermat", "fermi", "feynman", "franklin", + "gagarin", "galileo", "galois", "ganguly", "gates", "gauss", "germain", "goldberg", "goldstine", "goldwasser", "golick", "goodall", "gould", "greider", + "grothendieck", "haibt", "hamilton", "hardy", "haslett", "hawking", "heisenberg", "hellman", "hermann", "herschel", "hertz", "heyrovsky", "hodgkin", + "hofstadter", "hoover", "hopper", "hugle", "hypatia", "ishizaka", "jackson", "jang", "jemison", "jennings", "jepsen", "johnson", "joliot", "jones", + "kalam", "kapitsa", "kare", "keldysh", "keller", "kepler", "khayyam", "khorana", "kilby", "kirch", "knuth", "kowalevski", "lalande", "lamarr", + "lamport", "leakey", "leavitt", "lederberg", "lehmann", "lewin", "lichterman", "liskov", "lovelace", "lumiere", "mahavira", "margulis", "matsumoto", + "maxwell", "mayer", "mccarthy", "mcclintock", "mclaren", "mclean", "mcnulty", "meitner", "mendel", "mendeleev", "meninsky", "merkle", "mestorf", + "mirzakhani", "montalcini", "moore", "morse", "moser", "murdock", "napier", "nash", "neumann", "newton", "nightingale", "nobel", "noether", "northcutt", + "noyce", "panini", "pare", "pascal", "pasteur", "payne", "perlman", "pike", "poincare", "poitras", "proskuriakova", "ptolemy", "raman", "ramanujan", + "rhodes", "ride", "riemann", "ritchie", "robinson", "roentgen", "rosalind", "rubin", "saha", "sammet", "sanderson", "satoshi", "shamir", "shannon", + "shaw", "shirley", "shockley", "shtern", "sinoussi", "snyder", "solomon", "spence", "stonebraker", "sutherland", "swanson", "swartz", "swirles", + "taussig", "tesla", "tharp", "thompson", "torvalds", "tu", "turing", "varahamihira", "vaughan", "vaughn", "villani", "visvesvaraya", "volhard", + "wescoff", "weierstrass", "wilbur", "wiles", "williams", "williamson", "wilson", "wing", "wozniak", "wright", "wu", "yalow", "yonath", "zhukovsky"}; public static boolean validate(String shortUrl) { return PATTERN.matcher(shortUrl) @@ -37,4 +56,8 @@ public class Utils { return true; } + + public static String randomName() { + return adjective[random.nextInt(adjective.length)]+"-"+name[random.nextInt(name.length)]; + } } diff --git a/src/main/resources/public/index.html b/src/main/resources/public/index.html index 44a7128..379c6d8 100644 --- a/src/main/resources/public/index.html +++ b/src/main/resources/public/index.html @@ -85,7 +85,7 @@
- Short URL + Short URL (click to copy) Long URL Hits ×