147 lines
4.4 KiB
Java
147 lines
4.4 KiB
Java
package backendAlex;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.List;
|
|
import java.security.SecureRandom;
|
|
import java.util.HashSet;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
public class UrlShortener {
|
|
private Path file = Paths.get("/Users/alexandrawerner/project/urlShortenerTest/data.json");
|
|
private List<UrlShortenerData> urlData;
|
|
|
|
private String linkPrefix = "https://bolt-link.de/"; //https://shorturl.at/RDKwO
|
|
private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
private static final int CODE_LENGTH = 6;
|
|
private static final SecureRandom random = new SecureRandom();
|
|
|
|
public String getShortUrl(String longUrl){
|
|
readJson();
|
|
boolean newUrl = true;
|
|
String shortUrl = new String();
|
|
for (UrlShortenerData url : urlData) {
|
|
if(url.getLongUrl().equals(longUrl)){
|
|
shortUrl = url.getShortUrl();
|
|
newUrl = false;
|
|
break;
|
|
}
|
|
}
|
|
if(newUrl){
|
|
shortUrl = createShortUrl();
|
|
setNewUrl(shortUrl, longUrl);
|
|
}
|
|
return shortUrl;
|
|
}
|
|
|
|
public String getLongUrl(String shortUrl){
|
|
readJson();
|
|
String longUrl = "url not found";
|
|
for (UrlShortenerData url : urlData) {
|
|
if(url.getLongUrl().equals(shortUrl)){
|
|
longUrl = url.getShortUrl();
|
|
}
|
|
}
|
|
return longUrl;
|
|
}
|
|
|
|
private String createShortUrl(){
|
|
String url = linkPrefix + generateUniqueCode();
|
|
return url;
|
|
}
|
|
|
|
private String generateUniqueCode() {
|
|
HashSet<String> usedCodes = new HashSet<>();
|
|
usedCodes = getAllLinkSuffix();
|
|
|
|
String code;
|
|
do {
|
|
code = generateRandomCode();
|
|
} while (usedCodes.contains(code)); // Check if already used //TODO mit equals checken und nicht mir contains
|
|
|
|
usedCodes.add(code);
|
|
return code;
|
|
}
|
|
|
|
private String generateRandomCode() {
|
|
StringBuilder sb = new StringBuilder(CODE_LENGTH);
|
|
|
|
for (int i = 0; i < CODE_LENGTH; i++) {
|
|
int index = random.nextInt(CHARACTERS.length());
|
|
sb.append(CHARACTERS.charAt(index));
|
|
}
|
|
|
|
return sb.toString();
|
|
}
|
|
|
|
private HashSet<String> getAllLinkSuffix() {
|
|
readJson();
|
|
HashSet<String> usedCodes = new HashSet<>();
|
|
|
|
for (UrlShortenerData url : urlData) {
|
|
String shortUrl = url.getShortUrl();
|
|
try{
|
|
URI uri = new URI(shortUrl);
|
|
|
|
String path = uri.getPath();
|
|
|
|
if (path.contains("/")) {
|
|
usedCodes.add(path.substring(path.lastIndexOf('/') + 1));
|
|
}
|
|
}catch(URISyntaxException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return usedCodes;
|
|
}
|
|
|
|
private void setNewUrl(String shortUrl, String longUrl){
|
|
readJson();
|
|
UrlShortenerData newUrl = new UrlShortenerData(longUrl, shortUrl);
|
|
urlData.add(newUrl);
|
|
writeJson();
|
|
}
|
|
|
|
private void readJson(){
|
|
try {
|
|
String contentWithCharset = Files.readString(file, StandardCharsets.UTF_8);
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
urlData = mapper.readValue(contentWithCharset, new TypeReference<List<UrlShortenerData>>(){});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void writeJson(){
|
|
try{
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
// Liste -> JSON Array
|
|
String jsonString = mapper.writeValueAsString(urlData);
|
|
Files.writeString(file, jsonString);
|
|
}catch(IOException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
//test function
|
|
public void printJson(){
|
|
readJson();
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
try{
|
|
String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(urlData);
|
|
System.out.println(prettyJson);
|
|
}catch(JsonProcessingException e){
|
|
e.printStackTrace();
|
|
System.out.println(e);
|
|
}
|
|
}
|
|
}
|