upgrade spring, add javadoc, formatting

This commit is contained in:
2022-04-13 16:38:23 +02:00
parent 8ebed47574
commit eb829bfa26
168 changed files with 1554 additions and 2020 deletions
@@ -56,9 +56,9 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
public static final String SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND = "urlShortener.notFoundRedirect";
public static final String SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD = "urlShortener.passwordRedirect";
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
@Override
public void afterSingletonsInstantiated() {
@@ -105,74 +105,71 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
/**
* Gets the.
*
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param descending the descending
* @param search the search
* @param search the search
* @return the page
*/
public Page<ShortenedUrl> get(int page, int size, String sortBy, boolean descending,
String search) {
public Page<ShortenedUrl> get(int page, int size, String sortBy, boolean descending, String search) {
if (StringUtils.hasText(search)) {
return shortenedUrlRepository.findAll(
qShortenedUrl.note.containsIgnoreCase(search)
.or(qShortenedUrl.url.containsIgnoreCase(search))
qShortenedUrl.note.containsIgnoreCase(search).or(qShortenedUrl.url.containsIgnoreCase(search))
.or(qShortenedUrl.link.containsIgnoreCase(search)),
PageRequest.of(page, size, descending ? Sort.by(sortBy).descending()
: Sort.by(sortBy).ascending()));
PageRequest.of(page, size,
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending()));
}
return shortenedUrlRepository.findAll(PageRequest.of(page, size,
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending()));
return shortenedUrlRepository.findAll(
PageRequest.of(page, size, descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending()));
}
/**
* Gets the for user id.
*
* @param userId the user id
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param userId the user id
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param descending the descending
* @param search the search
* @param search the search
* @return the for user id
*/
public Page<ShortenedUrl> getForUserId(Long userId, int page, int size, String sortBy,
boolean descending, String search) {
public Page<ShortenedUrl> getForUserId(Long userId, int page, int size, String sortBy, boolean descending,
String search) {
// delete expired urls
shortenedUrlRepository.deleteAll(shortenedUrlRepository.findAll(
qShortenedUrl.owner.eq(userId).and(qShortenedUrl.expires.before(Instant.now()))));
shortenedUrlRepository.deleteAll(shortenedUrlRepository
.findAll(qShortenedUrl.owner.eq(userId).and(qShortenedUrl.expires.before(Instant.now()))));
BooleanBuilder query = new BooleanBuilder();
query.and(qShortenedUrl.owner.eq(userId));
if (StringUtils.hasText(search)) {
query.and(qShortenedUrl.note.containsIgnoreCase(search)
.or(qShortenedUrl.url.containsIgnoreCase(search))
query.and(qShortenedUrl.note.containsIgnoreCase(search).or(qShortenedUrl.url.containsIgnoreCase(search))
.or(qShortenedUrl.link.containsIgnoreCase(search)));
}
return shortenedUrlRepository.findAll(query.getValue(), PageRequest.of(page, size,
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending()));
return shortenedUrlRepository.findAll(query.getValue(),
PageRequest.of(page, size, descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending()));
}
/**
* Creates the.
*
* @param owner the owner
* @param url the url
* @param note the note
* @param code the code
* @param expires the expires
* @param password the password
* @param owner the owner
* @param url the url
* @param note the note
* @param code the code
* @param expires the expires
* @param password the password
* @param queryParameters the query parameters
* @param quota the quota
* @param quota the quota
* @return the shortened url
*/
public ShortenedUrl create(Long owner, String url, String note, String code, Instant expires,
String password, boolean queryParameters, boolean quota) {
public ShortenedUrl create(Long owner, String url, String note, String code, Instant expires, String password,
boolean queryParameters, boolean quota) {
ShortenedUrl shortenedUrl = new ShortenedUrl();
shortenedUrl.setOwner(owner);
@@ -204,8 +201,7 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
shortenedUrl = shortenedUrlRepository.save(shortenedUrl);
if (quota) {
Quota shortenedUrlsQuota = quotaManager.get(shortenedUrl.getOwner(),
ShortenedUrlQuotas.URL_SHORTENER);
Quota shortenedUrlsQuota = quotaManager.get(shortenedUrl.getOwner(), ShortenedUrlQuotas.URL_SHORTENER);
if (shortenedUrlsQuota != null) {
shortenedUrlsQuota.setValue(shortenedUrlsQuota.getValue() - 1);
quotaManager.update(shortenedUrlsQuota);
@@ -235,15 +231,14 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
* Delete.
*
* @param shortenedUrl the shortened url
* @param quota the quota
* @param quota the quota
*/
public void delete(ShortenedUrl shortenedUrl, boolean quota) {
if (quota) {
Quota shortenedUrlsQuota = quotaManager.get(shortenedUrl.getOwner(),
ShortenedUrlQuotas.URL_SHORTENER);
Quota shortenedUrlsQuota = quotaManager.get(shortenedUrl.getOwner(), ShortenedUrlQuotas.URL_SHORTENER);
if (shortenedUrlsQuota == null) {
shortenedUrlsQuota = quotaManager.create(shortenedUrl.getOwner(),
ShortenedUrlQuotas.URL_SHORTENER, 0, "#", true);
shortenedUrlsQuota = quotaManager.create(shortenedUrl.getOwner(), ShortenedUrlQuotas.URL_SHORTENER, 0,
"#", true);
}
shortenedUrlsQuota.setValue(shortenedUrlsQuota.getValue() + 1);
@@ -268,7 +263,6 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
}
}
/*
* @see de.bstly.we.businesslogic.UserDataProvider#getId()
*/
@@ -277,7 +271,6 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
return "shortend-urls";
}
/*
* @see de.bstly.we.businesslogic.UserDataProvider#getUserData(java.lang.Long)
*/
@@ -290,7 +283,6 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
return result;
}
/*
* @see de.bstly.we.businesslogic.UserDataProvider#purgeUserData(java.lang.Long)
*/
@@ -306,8 +298,7 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
*/
@Scheduled(cron = "0 */5 * * * *")
public void clearOrUpdateShortenedUrls() {
shortenedUrlRepository.deleteAll(
shortenedUrlRepository.findAll(qShortenedUrl.expires.before(Instant.now())));
shortenedUrlRepository.deleteAll(shortenedUrlRepository.findAll(qShortenedUrl.expires.before(Instant.now())));
String link = systemPropertyManager.get(SYSTEM_PROPERTY_URL_SHORTENER_LINK);
String linkRegex = "";
@@ -316,9 +307,8 @@ public class ShortenedUrlManager implements SmartInitializingSingleton, UserData
}
if (StringUtils.hasText(linkRegex)) {
for (ShortenedUrl shortenedUrl : shortenedUrlRepository
.findAll(qShortenedUrl.link.isNull().or(qShortenedUrl.link.isEmpty()
.or(qShortenedUrl.link.matches(linkRegex).not())))) {
for (ShortenedUrl shortenedUrl : shortenedUrlRepository.findAll(qShortenedUrl.link.isNull()
.or(qShortenedUrl.link.isEmpty().or(qShortenedUrl.link.matches(linkRegex).not())))) {
shortenedUrl.setLink(String.format(link, shortenedUrl.getCode()));
shortenedUrl = shortenedUrlRepository.save(shortenedUrl);
}
@@ -66,8 +66,8 @@ public class ShortenedUrlController extends BaseController {
/**
* Gets the shortened url link.
*
* @param code the code
* @param request the request
* @param code the code
* @param request the request
* @param response the response
* @return the shortened url link
* @throws IOException Signals that an I/O exception has occurred.
@@ -79,19 +79,18 @@ public class ShortenedUrlController extends BaseController {
try {
if (shortenedUrl == null) {
if (StringUtils.hasText(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND))) {
response.sendRedirect(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND));
if (StringUtils.hasText(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND))) {
response.sendRedirect(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND));
} else {
response.sendError(404, "code not found");
}
} else if (StringUtils.hasText(shortenedUrl.getPasswordHash())) {
if (StringUtils.hasText(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD))) {
if (StringUtils.hasText(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD))) {
response.sendRedirect(String.format(
systemPropertyManager.get(
ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD),
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD),
shortenedUrl.getCode()));
} else {
response.sendError(401, "password protected");
@@ -107,25 +106,25 @@ public class ShortenedUrlController extends BaseController {
/**
* Gets the protected shortened url link.
*
* @param code the code
* @param code the code
* @param password the password
* @param request the request
* @param request the request
* @param response the response
* @return the protected shortened url link
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping("/{code}")
public void getProtectedShortenedUrlLink(@PathVariable("code") String code,
@ModelAttribute("password") String password, HttpServletRequest request,
HttpServletResponse response) throws IOException {
@ModelAttribute("password") String password, HttpServletRequest request, HttpServletResponse response)
throws IOException {
ShortenedUrl shortenedUrl = shortenedUrlManager.get(code);
try {
if (shortenedUrl == null) {
if (StringUtils.hasText(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND))) {
response.sendRedirect(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND));
if (StringUtils.hasText(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND))) {
response.sendRedirect(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_NOT_FOUND));
} else {
response.sendError(404, "code not found");
}
@@ -133,11 +132,10 @@ public class ShortenedUrlController extends BaseController {
|| passwordEncoder.matches(password, shortenedUrl.getPasswordHash())) {
sendValidRedirect(shortenedUrl, request, response);
} else {
if (StringUtils.hasText(systemPropertyManager
.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD))) {
if (StringUtils.hasText(
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD))) {
String passwordErrorUrl = String.format(
systemPropertyManager.get(
ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD),
systemPropertyManager.get(ShortenedUrlManager.SYSTEM_PROPERTY_URL_SHORTENER_PASSWORD),
shortenedUrl.getCode());
URI passwordErrorUri;
try {
@@ -165,8 +163,8 @@ public class ShortenedUrlController extends BaseController {
* Send valid redirect.
*
* @param shortenedUrl the shortened url
* @param request the request
* @param response the response
* @param request the request
* @param response the response
* @throws IOException Signals that an I/O exception has occurred.
*/
protected void sendValidRedirect(ShortenedUrl shortenedUrl, HttpServletRequest request,
@@ -218,30 +216,26 @@ public class ShortenedUrlController extends BaseController {
/**
* Gets the shortened urls.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param sortParameter the sort parameter
* @param descParameter the desc parameter
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param sortParameter the sort parameter
* @param descParameter the desc parameter
* @param searchParameter the search parameter
* @return the shortened urls
*/
@PreAuthorize("isAuthenticated()")
@GetMapping
public Page<ShortenedUrl> getShortenedUrls(
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("sort") Optional<String> sortParameter,
public Page<ShortenedUrl> getShortenedUrls(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter, @RequestParam("sort") Optional<String> sortParameter,
@RequestParam("desc") Optional<Boolean> descParameter,
@RequestParam("search") Optional<String> searchParameter) {
if (!permissionManager.hasPermission(getCurrentUserId(),
ShortenedUrlPermissions.URL_SHORTENER)) {
if (!permissionManager.hasPermission(getCurrentUserId(), ShortenedUrlPermissions.URL_SHORTENER)) {
shortenedUrlManager.deleteAll(getCurrentUserId(), true);
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
}
return shortenedUrlManager.getForUserId(getCurrentUserId(), pageParameter.orElse(0),
sizeParameter.orElse(10), sortParameter.orElse("code"), descParameter.orElse(false),
searchParameter.orElse(""));
return shortenedUrlManager.getForUserId(getCurrentUserId(), pageParameter.orElse(0), sizeParameter.orElse(10),
sortParameter.orElse("code"), descParameter.orElse(false), searchParameter.orElse(""));
}
/**
@@ -253,15 +247,13 @@ public class ShortenedUrlController extends BaseController {
@PreAuthorize("isAuthenticated()")
@PostMapping
public ShortenedUrl createShortenedUrl(@RequestBody ShortenedUrlModel shortenedUrlModel) {
if (!permissionManager.hasPermission(getCurrentUserId(),
ShortenedUrlPermissions.URL_SHORTENER)
if (!permissionManager.hasPermission(getCurrentUserId(), ShortenedUrlPermissions.URL_SHORTENER)
|| !permissionManager.isFullUser(getCurrentUserId())) {
shortenedUrlManager.deleteAll(getCurrentUserId(), true);
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
}
Quota shortenedUrlsQuota = quotaManager.get(getCurrentUserId(),
ShortenedUrlQuotas.URL_SHORTENER);
Quota shortenedUrlsQuota = quotaManager.get(getCurrentUserId(), ShortenedUrlQuotas.URL_SHORTENER);
if (shortenedUrlsQuota == null || shortenedUrlsQuota.getValue() < 1) {
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
}
@@ -273,9 +265,8 @@ public class ShortenedUrlController extends BaseController {
throw new EntityResponseStatusException(errors.getAllErrors(), HttpStatus.CONFLICT);
}
return shortenedUrlManager.create(getCurrentUserId(), shortenedUrlModel.getUrl(),
shortenedUrlModel.getNote(), shortenedUrlModel.getCode(),
shortenedUrlModel.getExpires(), shortenedUrlModel.getPassword(),
return shortenedUrlManager.create(getCurrentUserId(), shortenedUrlModel.getUrl(), shortenedUrlModel.getNote(),
shortenedUrlModel.getCode(), shortenedUrlModel.getExpires(), shortenedUrlModel.getPassword(),
shortenedUrlModel.isQueryParameters(), true);
}
@@ -288,8 +279,7 @@ public class ShortenedUrlController extends BaseController {
@PreAuthorize("isAuthenticated()")
@PatchMapping
public ShortenedUrl updateShortenedUrl(@RequestBody ShortenedUrlModel shortenedUrlModel) {
if (!permissionManager.hasPermission(getCurrentUserId(),
ShortenedUrlPermissions.URL_SHORTENER)
if (!permissionManager.hasPermission(getCurrentUserId(), ShortenedUrlPermissions.URL_SHORTENER)
|| !permissionManager.isFullUser(getCurrentUserId())) {
shortenedUrlManager.deleteAll(getCurrentUserId(), true);
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
@@ -306,8 +296,7 @@ public class ShortenedUrlController extends BaseController {
Errors errors = new RequestBodyErrors(shortenedUrlModel);
if (StringUtils.hasText(shortenedUrlModel.getNewCode())
&& !shortenedUrlModel.getNewCode().equals(code)) {
if (StringUtils.hasText(shortenedUrlModel.getNewCode()) && !shortenedUrlModel.getNewCode().equals(code)) {
shortenedUrlModel.setCode(shortenedUrlModel.getNewCode());
newShortenedUrl = true;
} else {
@@ -335,8 +324,7 @@ public class ShortenedUrlController extends BaseController {
if (shortenedUrlModel.isNewPassword()) {
if (StringUtils.hasText(shortenedUrlModel.getPassword())) {
shortenedUrl
.setPasswordHash(passwordEncoder.encode(shortenedUrlModel.getPassword()));
shortenedUrl.setPasswordHash(passwordEncoder.encode(shortenedUrlModel.getPassword()));
} else {
shortenedUrl.setPasswordHash(null);
}
@@ -353,8 +341,7 @@ public class ShortenedUrlController extends BaseController {
@PreAuthorize("isAuthenticated()")
@DeleteMapping("/{code}")
public void deleteShortenedUrl(@PathVariable("code") String code) {
if (!permissionManager.hasPermission(getCurrentUserId(),
ShortenedUrlPermissions.URL_SHORTENER)) {
if (!permissionManager.hasPermission(getCurrentUserId(), ShortenedUrlPermissions.URL_SHORTENER)) {
shortenedUrlManager.deleteAll(getCurrentUserId(), true);
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
}
@@ -36,19 +36,18 @@ public class ShortenedUrlManagementController extends BaseController {
/**
* Gets the shortened urls.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param searchParameter the search parameter
* @return the shortened urls
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping
public Page<ShortenedUrl> getShortenedUrls(
@RequestParam("page") Optional<Integer> pageParameter,
public Page<ShortenedUrl> getShortenedUrls(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("search") Optional<String> searchParameter) {
return shortenedUrlManager.get(pageParameter.orElse(0), sizeParameter.orElse(10), "code",
true, searchParameter.orElse(""));
return shortenedUrlManager.get(pageParameter.orElse(0), sizeParameter.orElse(10), "code", true,
searchParameter.orElse(""));
}
/**
@@ -66,13 +65,12 @@ public class ShortenedUrlManagementController extends BaseController {
/**
* Delete shortened url.
*
* @param code the code
* @param code the code
* @param quota the quota
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("/{code}")
public void deleteShortenedUrl(@PathVariable("code") String code,
@RequestParam("quota") Optional<Boolean> quota) {
public void deleteShortenedUrl(@PathVariable("code") String code, @RequestParam("quota") Optional<Boolean> quota) {
ShortenedUrl shortenedUrl = shortenedUrlManager.get(code);
if (shortenedUrl == null) {
@@ -90,8 +88,7 @@ public class ShortenedUrlManagementController extends BaseController {
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("/all/{owner}")
public void deleteAll(@PathVariable("owner") Long owner,
@RequestParam("quota") Optional<Boolean> quota) {
public void deleteAll(@PathVariable("owner") Long owner, @RequestParam("quota") Optional<Boolean> quota) {
shortenedUrlManager.deleteAll(owner, quota.isPresent() && quota.get().booleanValue());
}
@@ -26,7 +26,6 @@ public class ShortenedUrlModelValidator implements Validator {
private UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES);
protected static final String codePart = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\\\.[A-Z0-9_!#$%&'*+/=?`{|}~^-]+)*$";
/*
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@@ -35,9 +34,9 @@ public class ShortenedUrlModelValidator implements Validator {
return clazz.isAssignableFrom(ShortenedUrlModel.class);
}
/*
* @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
*/
@Override
public void validate(Object target, Errors errors) {
@@ -55,8 +54,7 @@ public class ShortenedUrlModelValidator implements Validator {
}
}
if (shortenedUrlModel.getExpires() != null
&& shortenedUrlModel.getExpires().isBefore(Instant.now())) {
if (shortenedUrlModel.getExpires() != null && shortenedUrlModel.getExpires().isBefore(Instant.now())) {
errors.rejectValue("expires", "NOT_VALID");
}