forked from spring-attic/spring-security-oauth
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SECOAUTH-68: use @MVC features in sparklr2
- Loading branch information
1 parent
b976b43
commit 6cdaf4a
Showing
11 changed files
with
158 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
...src/main/java/org/springframework/security/oauth/examples/sparklr/mvc/HomeController.java
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
...rc/main/java/org/springframework/security/oauth/examples/sparklr/mvc/PhotoController.java
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
...c/main/java/org/springframework/security/oauth/examples/sparklr/mvc/PhotosController.java
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
.../main/java/org/springframework/security/oauth/examples/sparklr/mvc/SparklrController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.springframework.security.oauth.examples.sparklr.mvc; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.oauth.examples.sparklr.PhotoInfo; | ||
import org.springframework.security.oauth.examples.sparklr.PhotoService; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
/** | ||
* @author Ryan Heaton | ||
* @author Dave Syer | ||
*/ | ||
@Controller | ||
public class SparklrController { | ||
|
||
private PhotoService photoService; | ||
|
||
@RequestMapping("/rest/jpg/photo/{photoId}") | ||
public ResponseEntity<byte[]> getPhoto(@PathVariable("photoId") String id) throws IOException { | ||
InputStream photo = getPhotoService().loadPhoto(id); | ||
if (photo == null) { | ||
return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND); | ||
} else { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int len = photo.read(buffer); | ||
while (len >= 0) { | ||
out.write(buffer, 0, len); | ||
len = photo.read(buffer); | ||
} | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Content-Type", "image/jpeg"); | ||
return new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK); | ||
} | ||
} | ||
|
||
@RequestMapping("/json/photos") | ||
public ResponseEntity<String> getJsonPhotos(@RequestParam(value = "callback", required = false) String callback) { | ||
Collection<PhotoInfo> photos = getPhotoService().getPhotosForCurrentUser(); | ||
StringBuilder out = new StringBuilder(); | ||
if (callback != null) { | ||
out.append(callback).append("( "); | ||
} | ||
out.append("{ \"photos\" : [ "); | ||
Iterator<PhotoInfo> photosIt = photos.iterator(); | ||
while (photosIt.hasNext()) { | ||
PhotoInfo photo = photosIt.next(); | ||
out.append(String.format("{ \"id\" : \"%s\" , \"name\" : \"%s\" }", photo.getId(), photo.getName())); | ||
if (photosIt.hasNext()) { | ||
out.append(" , "); | ||
} | ||
} | ||
out.append("] }"); | ||
if (callback != null) { | ||
out.append(" )"); | ||
} | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Content-Type", "application/json"); | ||
return new ResponseEntity<String>(out.toString(), headers, HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping("/rest/photos") | ||
public ResponseEntity<String> getXmlPhotos() { | ||
Collection<PhotoInfo> photos = photoService.getPhotosForCurrentUser(); | ||
StringBuilder out = new StringBuilder(); | ||
out.append("<photos>"); | ||
for (PhotoInfo photo : photos) { | ||
out.append(String.format("<photo id=\"%s\" name=\"%s\"/>", photo.getId(), photo.getName())); | ||
} | ||
out.append("</photos>"); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Content-Type", "application/xml"); | ||
return new ResponseEntity<String>(out.toString(), headers, HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping("/trusted/message") | ||
@PreAuthorize("oauthClientHasRole('ROLE_TRUSTED_CLIENT')") | ||
@ResponseBody | ||
public String getTrustedClientMessage() { | ||
return "Hello, Trusted Client"; | ||
} | ||
|
||
public PhotoService getPhotoService() { | ||
return photoService; | ||
} | ||
|
||
public void setPhotoService(PhotoService photoService) { | ||
this.photoService = photoService; | ||
} | ||
|
||
} |
34 changes: 0 additions & 34 deletions
34
...java/org/springframework/security/oauth/examples/sparklr/mvc/TrustedClientController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.