Guest User

Untitled

a guest
Aug 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package org.springframework.security.oauth.examples.sparklr.mvc;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.security.Principal;
  7. import java.util.Collection;
  8. import java.util.Iterator;
  9.  
  10. import org.springframework.http.HttpHeaders;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.security.oauth.examples.sparklr.PhotoInfo;
  15. import org.springframework.security.oauth.examples.sparklr.PhotoService;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21.  
  22. /**
  23. * @author Ryan Heaton
  24. * @author Dave Syer
  25. */
  26. @Controller
  27. public class PhotoController {
  28.  
  29. private PhotoService photoService;
  30.  
  31. @RequestMapping("/photos/{photoId}")
  32. public ResponseEntity<byte[]> getPhoto(@PathVariable("photoId") String id) throws IOException {
  33. InputStream photo = getPhotoService().loadPhoto(id);
  34. if (photo == null) {
  35. return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
  36. } else {
  37. ByteArrayOutputStream out = new ByteArrayOutputStream();
  38. byte[] buffer = new byte[1024];
  39. int len = photo.read(buffer);
  40. while (len >= 0) {
  41. out.write(buffer, 0, len);
  42. len = photo.read(buffer);
  43. }
  44. HttpHeaders headers = new HttpHeaders();
  45. headers.set("Content-Type", "image/jpeg");
  46. return new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK);
  47. }
  48. }
  49.  
  50. @RequestMapping(value = "/photos", params = "format=json")
  51. public ResponseEntity<String> getJsonPhotos(@RequestParam(value = "callback", required = false) String callback, Principal principal) {
  52. Collection<PhotoInfo> photos = getPhotoService().getPhotosForCurrentUser(principal.getName());
  53. StringBuilder out = new StringBuilder();
  54. if (callback != null) {
  55. out.append(callback).append("( ");
  56. }
  57. out.append("{ \"photos\" : [ ");
  58. Iterator<PhotoInfo> photosIt = photos.iterator();
  59. while (photosIt.hasNext()) {
  60. PhotoInfo photo = photosIt.next();
  61. out.append(String.format("{ \"id\" : \"%s\" , \"name\" : \"%s\" }", photo.getId(), photo.getName()));
  62. if (photosIt.hasNext()) {
  63. out.append(" , ");
  64. }
  65. }
  66. out.append("] }");
  67. if (callback != null) {
  68. out.append(" )");
  69. }
  70.  
  71. HttpHeaders headers = new HttpHeaders();
  72. headers.set("Content-Type", "application/json");
  73. return new ResponseEntity<String>(out.toString(), headers, HttpStatus.OK);
  74. }
  75.  
  76. @RequestMapping(value = "/photos", params = "format=xml")
  77. public ResponseEntity<String> getXmlPhotos(Principal principal) {
  78. Collection<PhotoInfo> photos = photoService.getPhotosForCurrentUser(principal.getName());
  79. StringBuilder out = new StringBuilder();
  80. out.append("<photos>");
  81. for (PhotoInfo photo : photos) {
  82. out.append(String.format("<photo id=\"%s\" name=\"%s\"/>", photo.getId(), photo.getName()));
  83. }
  84. out.append("</photos>");
  85.  
  86. HttpHeaders headers = new HttpHeaders();
  87. headers.set("Content-Type", "application/xml");
  88. return new ResponseEntity<String>(out.toString(), headers, HttpStatus.OK);
  89. }
  90.  
  91. @RequestMapping("/trusted/message")
  92. @PreAuthorize("oauthClientHasRole('ROLE_CLIENT')")
  93. @ResponseBody
  94. public String getTrustedClientMessage() {
  95. return "Hello, Trusted Client";
  96. }
  97.  
  98. @RequestMapping("/user/message")
  99. @ResponseBody
  100. public String getTrustedUserMessage() {
  101. return "Hello, Trusted User";
  102. }
  103.  
  104. public PhotoService getPhotoService() {
  105. return photoService;
  106. }
  107.  
  108. public void setPhotoService(PhotoService photoService) {
  109. this.photoService = photoService;
  110. }
  111.  
  112. }
Add Comment
Please, Sign In to add comment