@RequestMapping(value = "/link", method = RequestMethod.POST) public ResponseEntity<?> shortener( @RequestParam("url") String url, @RequestParam(value = "custom", required = false) String custom, @RequestParam(value = "expire", required = false) String expireDate, @RequestParam(value = "hasToken", required = false) String hasToken, @RequestParam(value = "emails[]", required = false) String[] emails, @RequestParam(value = "days", required = false) String days, HttpServletRequest request, Principal principal) throws Exception { JSONObject jn = getFreegeoip(request); String country = jn.getString("country_name"); ShortURL su = createAndSaveIfValid( url, custom, hasToken, expireDate, extractIP(request), emails, principal, country); if (su != null) { /* If there is an expire date, it sets an alert */ if (!expireDate.equals("")) { Date alertDate = processAlertDate(expireDate, days); logger.info("New alert date: " + alertDate); String mail = UrlShortenerControllerWithLogs.getOwnerMail(); Alert alert = new Alert(mail, su.getHash(), alertDate); alertRepository.save(alert); } HttpHeaders h = new HttpHeaders(); h.setLocation(su.getUri()); return new ResponseEntity<>(su, h, HttpStatus.CREATED); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } }
/** Changes the expire date and alert for the new specified */ @RequestMapping(value = "/changeExpire", method = RequestMethod.GET) public ResponseEntity<?> changeExpireDate( HttpServletRequest request, @RequestParam(value = "url", required = false) String hash, @RequestParam(value = "expire", required = false) String expire, @RequestParam(value = "days", required = false) String days) { hash = hash.substring(1, hash.length() - 1); /* Changes expire date */ ShortURL su = shortURLRepository.findByHash(hash); logger.info("su: " + su); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date newExpire = null; try { newExpire = sdf.parse(expire); } catch (ParseException e) { e.printStackTrace(); logger.info("Error with introduced alert date"); } su.setExpire(newExpire); logger.info("Updating ShortURL: " + su); shortURLRepository.save(su); /* Changes alert date */ Alert a = alertRepository.findByHash(hash); Date alertDate = processAlertDate(expire, days); if (a != null) { /* If alert already exists, updates its alert date */ a.setDate(alertDate); logger.info("Updating alert: " + a); alertRepository.save(a); } else { /* If alert does not exist, creates a new one */ String mail = UrlShortenerControllerWithLogs.getOwnerMail(); logger.info("Setting new alert"); alertRepository.save(new Alert(mail, hash, alertDate)); } return new ResponseEntity<>(HttpStatus.OK); }