Skip to content

Instantly share code, notes, and snippets.

@Glamdring
Created July 15, 2018 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glamdring/9da507a3f456dad1286be1a4830e1b8a to your computer and use it in GitHub Desktop.
Save Glamdring/9da507a3f456dad1286be1a4830e1b8a to your computer and use it in GitHub Desktop.
@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