mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 17:26:45 +00:00
For random shorturl adjective-name pair is used
This commit is contained in:
parent
e5f1839822
commit
3598199957
@ -20,20 +20,25 @@ public class Routes {
|
|||||||
var body = req.body();
|
var body = req.body();
|
||||||
var split = body.split(";");
|
var split = body.split(";");
|
||||||
String longUrl = split[0];
|
String longUrl = split[0];
|
||||||
|
boolean unique = false;
|
||||||
|
|
||||||
String shortUrl;
|
String shortUrl;
|
||||||
try {
|
try {
|
||||||
shortUrl = split[1];
|
shortUrl = split[1];
|
||||||
|
shortUrl = shortUrl.toLowerCase();
|
||||||
|
if (urlRepository.findForShortUrl(shortUrl).isEmpty()) {
|
||||||
|
unique = true;
|
||||||
|
}
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
shortUrl = Utils.randomString();
|
do {
|
||||||
|
shortUrl = Utils.randomName();
|
||||||
|
if (urlRepository.findForShortUrl(shortUrl).isEmpty()) {
|
||||||
|
unique = true;
|
||||||
|
}
|
||||||
|
} while (unique == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
shortUrl = shortUrl.toLowerCase();
|
if (unique && Utils.validate(shortUrl)) {
|
||||||
|
|
||||||
var shortUrlPresent = urlRepository
|
|
||||||
.findForShortUrl(shortUrl);
|
|
||||||
|
|
||||||
if (shortUrlPresent.isEmpty() && Utils.validate(shortUrl)) {
|
|
||||||
return urlRepository.addUrl(longUrl, shortUrl);
|
return urlRepository.addUrl(longUrl, shortUrl);
|
||||||
} else {
|
} else {
|
||||||
res.status(HttpStatus.BAD_REQUEST_400);
|
res.status(HttpStatus.BAD_REQUEST_400);
|
||||||
|
@ -8,20 +8,39 @@ public class Utils {
|
|||||||
|
|
||||||
private static final String SHORT_URL_PATTERN = "[a-z0-9-_]+";
|
private static final String SHORT_URL_PATTERN = "[a-z0-9-_]+";
|
||||||
private static final Pattern PATTERN = Pattern.compile(SHORT_URL_PATTERN);
|
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() {
|
private static final String[] name = new String[] {"agnesi", "albattani", "allen", "almeida", "antonelli", "archimedes", "ardinghelli", "aryabhata", "austin",
|
||||||
int leftLimit = 48; // numeral '0'
|
"babbage", "banach", "banzai", "bardeen", "bartik", "bassi", "beaver", "bell", "benz", "bhabha", "bhaskara", "black", "blackburn", "blackwell",
|
||||||
int rightLimit = 122; // letter 'z'
|
"bohr", "booth", "borg", "bose", "bouman", "boyd", "brahmagupta", "brattain", "brown", "buck", "burnell", "cannon", "carson", "cartwright",
|
||||||
int targetStringLength = 10;
|
"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",
|
||||||
return random.ints(leftLimit, rightLimit + 1)
|
"einstein", "elbakyan", "elgamal", "elion", "ellis", "engelbart", "euclid", "euler", "faraday", "feistel", "fermat", "fermi", "feynman", "franklin",
|
||||||
.filter(i -> (i <= 57 || i >= 97))
|
"gagarin", "galileo", "galois", "ganguly", "gates", "gauss", "germain", "goldberg", "goldstine", "goldwasser", "golick", "goodall", "gould", "greider",
|
||||||
.limit(targetStringLength)
|
"grothendieck", "haibt", "hamilton", "hardy", "haslett", "hawking", "heisenberg", "hellman", "hermann", "herschel", "hertz", "heyrovsky", "hodgkin",
|
||||||
.collect(StringBuilder::new,
|
"hofstadter", "hoover", "hopper", "hugle", "hypatia", "ishizaka", "jackson", "jang", "jemison", "jennings", "jepsen", "johnson", "joliot", "jones",
|
||||||
StringBuilder::appendCodePoint,
|
"kalam", "kapitsa", "kare", "keldysh", "keller", "kepler", "khayyam", "khorana", "kilby", "kirch", "knuth", "kowalevski", "lalande", "lamarr",
|
||||||
StringBuilder::append)
|
"lamport", "leakey", "leavitt", "lederberg", "lehmann", "lewin", "lichterman", "liskov", "lovelace", "lumiere", "mahavira", "margulis", "matsumoto",
|
||||||
.toString();
|
"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) {
|
public static boolean validate(String shortUrl) {
|
||||||
return PATTERN.matcher(shortUrl)
|
return PATTERN.matcher(shortUrl)
|
||||||
@ -37,4 +56,8 @@ public class Utils {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String randomName() {
|
||||||
|
return adjective[random.nextInt(adjective.length)]+"-"+name[random.nextInt(name.length)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@
|
|||||||
<br>
|
<br>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Short URL</td>
|
<td>Short URL (click to copy)</td>
|
||||||
<td>Long URL</td>
|
<td>Long URL</td>
|
||||||
<td>Hits</td>
|
<td>Hits</td>
|
||||||
<td name="deleteBtn">×</td>
|
<td name="deleteBtn">×</td>
|
||||||
|
Loading…
Reference in New Issue
Block a user