added java backend (logic only)
This commit is contained in:
146
UrlShortener.java
Normal file
146
UrlShortener.java
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
UrlShortenerData.java
Normal file
38
UrlShortenerData.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package backendAlex;
|
||||||
|
|
||||||
|
|
||||||
|
public class UrlShortenerData {
|
||||||
|
|
||||||
|
private String longUrl;
|
||||||
|
|
||||||
|
private String shortUrl;
|
||||||
|
|
||||||
|
// Default constructor (required for Jackson)
|
||||||
|
public UrlShortenerData() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional: Convenience constructor
|
||||||
|
public UrlShortenerData(String longUrl, String shortUrl) {
|
||||||
|
this.longUrl = longUrl;
|
||||||
|
this.shortUrl = shortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standard Setter für longUrl
|
||||||
|
public void setLongUrl(String longUrl) {
|
||||||
|
this.longUrl = longUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standard Setter für shortUrl
|
||||||
|
public void setShortUrl(String shortUrl) {
|
||||||
|
this.shortUrl = shortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getter
|
||||||
|
public String getLongUrl() {
|
||||||
|
return this.longUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getShortUrl() {
|
||||||
|
return this.shortUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
test.java
Normal file
17
test.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package backendAlex;
|
||||||
|
|
||||||
|
class test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
UrlShortener url = new UrlShortener();
|
||||||
|
//String shortUrl = url.getShortUrl("longUrlTest2");
|
||||||
|
//System.out.println(shortUrl);
|
||||||
|
|
||||||
|
//String longUrl = url.getLongUrl(shortUrlTest3);
|
||||||
|
//System.out.println(longUrl);
|
||||||
|
String shortUrl = url.getShortUrl("longUrlTest6");
|
||||||
|
System.out.println(shortUrl);
|
||||||
|
|
||||||
|
//url.printJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user