Skip to content

Instantly share code, notes, and snippets.

@rakeshsingh
Last active January 4, 2024 03:42
Show Gist options
  • Save rakeshsingh/64918583972dd5a08012 to your computer and use it in GitHub Desktop.
Save rakeshsingh/64918583972dd5a08012 to your computer and use it in GitHub Desktop.
Java Code - Url Shortener
package testjava;
import java.util.HashMap;
import java.util.Random;
/*
* URL Shortener
*/
public class URLShortener {
// storage for generated keys
private HashMap<String, String> keyMap; // key-url map
private HashMap<String, String> valueMap;// url-key map to quickly check
// whether an url is
// already entered in our system
private String domain; // Use this attribute to generate urls for a custom
// domain name defaults to http://fkt.in
private char myChars[]; // This array is used for character to number
// mapping
private Random myRand; // Random object used to generate random integers
private int keyLength; // the key length in URL defaults to 8
// Default Constructor
URLShortener() {
keyMap = new HashMap<String, String>();
valueMap = new HashMap<String, String>();
myRand = new Random();
keyLength = 8;
myChars = new char[62];
for (int i = 0; i < 62; i++) {
int j = 0;
if (i < 10) {
j = i + 48;
} else if (i > 9 && i <= 35) {
j = i + 55;
} else {
j = i + 61;
}
myChars[i] = (char) j;
}
domain = "http://fkt.in";
}
// Constructor which enables you to define tiny URL key length and base URL
// name
URLShortener(int length, String newDomain) {
this();
this.keyLength = length;
if (!newDomain.isEmpty()) {
newDomain = sanitizeURL(newDomain);
domain = newDomain;
}
}
// shortenURL
// the public method which can be called to shorten a given URL
public String shortenURL(String longURL) {
String shortURL = "";
if (validateURL(longURL)) {
longURL = sanitizeURL(longURL);
if (valueMap.containsKey(longURL)) {
shortURL = domain + "/" + valueMap.get(longURL);
} else {
shortURL = domain + "/" + getKey(longURL);
}
}
// add http part
return shortURL;
}
// expandURL
// public method which returns back the original URL given the shortened url
public String expandURL(String shortURL) {
String longURL = "";
String key = shortURL.substring(domain.length() + 1);
longURL = keyMap.get(key);
return longURL;
}
// Validate URL
// not implemented, but should be implemented to check whether the given URL
// is valid or not
boolean validateURL(String url) {
return true;
}
// sanitizeURL
// This method should take care various issues with a valid url
// e.g. www.google.com,www.google.com/, http://www.google.com,
// http://www.google.com/
// all the above URL should point to same shortened URL
// There could be several other cases like these.
String sanitizeURL(String url) {
if (url.substring(0, 7).equals("http://"))
url = url.substring(7);
if (url.substring(0, 8).equals("https://"))
url = url.substring(8);
if (url.charAt(url.length() - 1) == '/')
url = url.substring(0, url.length() - 1);
return url;
}
/*
* Get Key method
*/
private String getKey(String longURL) {
String key;
key = generateKey();
keyMap.put(key, longURL);
valueMap.put(longURL, key);
return key;
}
// generateKey
private String generateKey() {
String key = "";
boolean flag = true;
while (flag) {
key = "";
for (int i = 0; i <= keyLength; i++) {
key += myChars[myRand.nextInt(62)];
}
// System.out.println("Iteration: "+ counter + "Key: "+ key);
if (!keyMap.containsKey(key)) {
flag = false;
}
}
return key;
}
// test the code
public static void main(String args[]) {
URLShortener u = new URLShortener(5, "www.tinyurl.com/");
String urls[] = { "www.google.com/", "www.google.com",
"http://www.yahoo.com", "www.yahoo.com/", "www.amazon.com",
"www.amazon.com/page1.php", "www.amazon.com/page2.php",
"www.flipkart.in", "www.rediff.com", "www.techmeme.com",
"www.techcrunch.com", "www.lifehacker.com", "www.icicibank.com" };
for (int i = 0; i < urls.length; i++) {
System.out.println("URL:" + urls[i] + "\tTiny: "
+ u.shortenURL(urls[i]) + "\tExpanded: "
+ u.expandURL(u.shortenURL(urls[i])));
}
}
}
@brahmanand012
Copy link

I have executed this program and when i hit the shorter url on browser.I got error "Error: Unable to find URL to redirect to."

URL:www.google.com/ Tiny: www.tinyurl.com/H8Y4zT Expanded: www.google.com

please let me know if i missing something

@rambosir
Copy link

good job!

@stream2stream
Copy link

Excellent, really cool...

@inaki-ortizdelandaluce
Copy link

Same here, www.google.com gets shortened to www.tinyurl.com/8pwluu but does not work either...

@janabai
Copy link

janabai commented Jan 21, 2019

Same here, www.google.com gets shortened to www.tinyurl.com/8pwluu but does not work either...

@janabai
Copy link

janabai commented Jan 21, 2019

I have executed this program and when i hit the shorter url on browser.I got error "Error: Unable to find URL to redirect to."

URL:www.google.com/ Tiny: www.tinyurl.com/H8Y4zT Expanded: www.google.com
please let me know if i missing something

@gotidhavalh
Copy link

Thank you for this.

@jaypatel512
Copy link

For people who are seeing "Error: Unable to find URL to redirect to.", the above code is to create your own shortner. It does not use tinyurl or any other external service. If you just want to shorten a url using tinyurl, or bitly, you need to use their APIs. This is not for that purpose.

@adithya-18
Copy link

Redirection is not part of this code. You need something like a nginx setup with rules to perform that.

@hientq
Copy link

hientq commented Jun 18, 2021

Thank you so much!

@shindepayal1610
Copy link

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment