Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Controller
@RequestMapping("/whitelabel-resources")
public class WhitelabelResourceController {
@Autowired
private WhitelabelService whitelabelService;
@GetMapping("/logo.png")
private ResponseEntity getLogo(@AuthenticationPrincipal LoginAuthenticationToken token,
@RequestParam(name = "key", required = false) String key) {
WhitelabelStyling styling = getWhitelabelStyling(token, key);
byte[] logo = styling.getLogo();
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG)
.cacheControl(CacheControl.maxAge(1, TimeUnit.DAYS))
.body(logo);
}
@GetMapping("/style.css")
private ResponseEntity getStyle(@AuthenticationPrincipal LoginAuthenticationToken token,
@RequestParam(name = "key", required = false) String key) {
WhitelabelStyling styling = getWhitelabelStyling(token, key);
String css = styling.getCss();
return ResponseEntity.ok()
.contentType(MediaType.valueOf("text/css"))
.cacheControl(CacheControl.maxAge(1, TimeUnit.DAYS))
.body(css);
}
private WhitelabelStyling getWhitelabelStyling(LoginAuthenticationToken token, String key) {
WhitelabelStyling styling;
if (token != null && token.getUser() != null && token.getUser().getWhitelabelStyling() != null) {
styling = token.getUser().getWhitelabelStyling();
} else if (key != null) {
styling = whitelabelService.getWhitelabelStyling(key)
.orElseThrow(() -> new IllegalArgumentException("No styling found for key " + key));
} else {
throw new IllegalArgumentException("Neither current user's organization has whitelabeling, nor a key was passed");
}
return styling;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment