Guest User

Untitled

a guest
Aug 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package org.springframework.security.oauth.examples.sparklr.impl;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.List;
  9.  
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.security.core.userdetails.UserDetails;
  13. import org.springframework.security.oauth.examples.sparklr.PhotoInfo;
  14. import org.springframework.security.oauth.examples.sparklr.PhotoService;
  15.  
  16. /**
  17. * Basic implementation for the photo service.
  18. *
  19. * @author Ryan Heaton
  20. */
  21. public class PhotoServiceImpl implements PhotoService {
  22.  
  23. private List<PhotoInfo> photos;
  24.  
  25. public Collection<PhotoInfo> getPhotosForCurrentUser(String username) {
  26.  
  27. ArrayList<PhotoInfo> infos = new ArrayList<PhotoInfo>();
  28. for (PhotoInfo info : getPhotos()) {
  29. if (username.equals(info.getUserId())) {
  30. infos.add(info);
  31. }
  32. }
  33. return infos;
  34.  
  35. }
  36.  
  37. public InputStream loadPhoto(String id) {
  38. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  39. if (authentication.getPrincipal() instanceof UserDetails) {
  40. UserDetails details = (UserDetails) authentication.getPrincipal();
  41. String username = details.getUsername();
  42. for (PhotoInfo photoInfo : getPhotos()) {
  43. if (id.equals(photoInfo.getId()) && username.equals(photoInfo.getUserId())) {
  44. URL resourceURL = getClass().getResource(photoInfo.getResourceURL());
  45. if (resourceURL != null) {
  46. try {
  47. return resourceURL.openStream();
  48. } catch (IOException e) {
  49. // fall through...
  50. }
  51. }
  52. }
  53. }
  54. }
  55. return null;
  56. }
  57.  
  58. public List<PhotoInfo> getPhotos() {
  59. return photos;
  60. }
  61.  
  62. public void setPhotos(List<PhotoInfo> photos) {
  63. this.photos = photos;
  64. }
  65. }
Add Comment
Please, Sign In to add comment