commit 6276b54cb10ad447672d8733a155a7ec631cc8af Author: alexandrawerner Date: Wed Mar 25 15:10:04 2026 +0100 added java backend (logic only) diff --git a/UrlShortener.java b/UrlShortener.java new file mode 100644 index 0000000..e283922 --- /dev/null +++ b/UrlShortener.java @@ -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 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 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 getAllLinkSuffix() { + readJson(); + HashSet 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>(){}); + } 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); + } + } +} diff --git a/UrlShortenerData.java b/UrlShortenerData.java new file mode 100644 index 0000000..3b0db65 --- /dev/null +++ b/UrlShortenerData.java @@ -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; + } +} \ No newline at end of file diff --git a/test.java b/test.java new file mode 100644 index 0000000..5c5f786 --- /dev/null +++ b/test.java @@ -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(); + } + +}