/** * If shortURL is valid, creates it and saves it XXX: at the moment, it just ignores unknown * emails, with no feedback for users. */ protected ShortURL createAndSaveIfValid( String url, String custom, String hasToken, String expireDate, String ip, String[] emails, Principal principal, String country) { UrlValidator urlValidator = new UrlValidator(new String[] {"http", "https"}); /* * Check if url comes through http or https */ if (urlValidator.isValid(url)) { logger.info("Shortening valid url " + url); /* * Creates a hash from the current date in case this is not custom */ String id; String now = new Date().toString(); if (custom.equals("")) { id = Hashing.murmur3_32().hashString(now, StandardCharsets.UTF_8).toString(); } else { id = custom; } /* * Has Token */ String token = null; if (hasToken != null && !hasToken.equals("")) { token = UUID.randomUUID().toString(); } /* * Expire date */ Date expire = null; if (!expireDate.equals("")) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { expire = sdf.parse(expireDate); } catch (ParseException e) { e.printStackTrace(); logger.info("Error: badly introduced date."); } } /* * Checks every mail inserted by the user, and maintains a list with * those corresponding to registered users. */ List<String> trueEmails = new ArrayList<String>(); for (int i = 0; i < emails.length; i++) { if (!emails[i].equals("")) { User foundUser = null; foundUser = userRepository.findByMail(emails[i]); if (foundUser != null) { trueEmails.add(foundUser.getMail()); } } } /* * If no valid emails are introduced, link will be public and it * wont have an email list. */ boolean isPrivate = false; if (trueEmails.size() > 0) { isPrivate = true; } else { trueEmails = null; } /* * Gets email */ String owner = getOwnerMail(); /* * Creates ShortURL object */ ShortURL su = new ShortURL( id, url, linkTo( methodOn(UrlShortenerControllerWithLogs.class) .redirectTo(id, null, null, null, null)) .toUri(), new Date(System.currentTimeMillis()), expire, owner, token, HttpStatus.TEMPORARY_REDIRECT.value(), ip, country, isPrivate, trueEmails); /* * Insert to DB */ return shortURLRepository.save(su); } else { logger.info("Not valid url " + url); return null; } }
protected ShortURL createAndSaveIfValid( String url, String sponsor, String brand, String owner, String ip, String name, String username, boolean advert, int seconds, boolean useConditional) { UrlValidator urlValidator = new UrlValidator(new String[] {"http", "https"}); if (urlValidator.isValid(url) || (url.equals("") && useConditional)) { ShortURL su = null; ResponseEntity<String> entity = new RestTemplate().getForEntity("http://ipinfo.io/" + ip + "/json", String.class); String json = entity.getBody().toString(); String country = ""; try { JSONObject obj = new JSONObject(json); country = obj.getString("country"); } catch (JSONException e) { } if (!name.equals("")) { su = new ShortURL( name, url, linkTo(methodOn(UrlShortenerControllerWithLogs.class).redirectTo(name, null)) .toUri(), sponsor, new Date(System.currentTimeMillis()), owner, HttpStatus.TEMPORARY_REDIRECT.value(), true, ip, country, username, advert, seconds); } else { String id = Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString(); su = new ShortURL( id, url, linkTo(methodOn(UrlShortenerController.class).redirectTo(id, null)).toUri(), sponsor, new Date(System.currentTimeMillis()), owner, HttpStatus.TEMPORARY_REDIRECT.value(), true, ip, country, username, advert, seconds); } return shortURLRepositoryExtended.save(su); } else { return null; } }