Search in sources :

Example 1 with PhotoInfo

use of org.springframework.security.oauth.examples.sparklr.PhotoInfo in project spring-security-oauth by spring-projects.

the class PhotoController method getJsonPhotos.

@RequestMapping(value = "/photos", params = "format=json")
public ResponseEntity<String> getJsonPhotos(Principal principal) {
    Collection<PhotoInfo> photos = getPhotoService().getPhotosForCurrentUser(principal.getName());
    StringBuilder out = new StringBuilder();
    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("] }");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "application/javascript");
    return new ResponseEntity<String>(out.toString(), headers, HttpStatus.OK);
}
Also used : PhotoInfo(org.springframework.security.oauth.examples.sparklr.PhotoInfo) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with PhotoInfo

use of org.springframework.security.oauth.examples.sparklr.PhotoInfo in project spring-security-oauth by spring-projects.

the class PhotoController method getXmlPhotos.

@RequestMapping(value = "/photos", params = "format=xml")
@ResponseBody
public ResponseEntity<String> getXmlPhotos() throws Exception {
    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);
}
Also used : PhotoInfo(org.springframework.security.oauth.examples.sparklr.PhotoInfo) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with PhotoInfo

use of org.springframework.security.oauth.examples.sparklr.PhotoInfo in project spring-security-oauth by spring-projects.

the class PhotoServiceImpl method loadPhoto.

public InputStream loadPhoto(String id) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication.getPrincipal() instanceof UserDetails) {
        UserDetails details = (UserDetails) authentication.getPrincipal();
        String username = details.getUsername();
        for (PhotoInfo photoInfo : getPhotos()) {
            if (id.equals(photoInfo.getId()) && username.equals(photoInfo.getUserId())) {
                URL resourceURL = getClass().getResource(photoInfo.getResourceURL());
                if (resourceURL != null) {
                    try {
                        return resourceURL.openStream();
                    } catch (IOException e) {
                    // fall through...
                    }
                }
            }
        }
    }
    return null;
}
Also used : PhotoInfo(org.springframework.security.oauth.examples.sparklr.PhotoInfo) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with PhotoInfo

use of org.springframework.security.oauth.examples.sparklr.PhotoInfo in project spring-security-oauth by spring-projects.

the class PhotoServiceImpl method getPhotosForCurrentUser.

public Collection<PhotoInfo> getPhotosForCurrentUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication.getPrincipal() instanceof UserDetails) {
        UserDetails details = (UserDetails) authentication.getPrincipal();
        String username = details.getUsername();
        ArrayList<PhotoInfo> infos = new ArrayList<PhotoInfo>();
        for (PhotoInfo info : getPhotos()) {
            if (username.equals(info.getUserId())) {
                infos.add(info);
            }
        }
        return infos;
    } else {
        throw new BadCredentialsException("Bad credentials: not a username/password authentication.");
    }
}
Also used : PhotoInfo(org.springframework.security.oauth.examples.sparklr.PhotoInfo) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 5 with PhotoInfo

use of org.springframework.security.oauth.examples.sparklr.PhotoInfo in project spring-security-oauth by spring-projects.

the class PhotoController method getXmlPhotos.

@RequestMapping(value = "/photos", params = "format=xml")
public ResponseEntity<String> getXmlPhotos(Principal principal) {
    Collection<PhotoInfo> photos = photoService.getPhotosForCurrentUser(principal.getName());
    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);
}
Also used : PhotoInfo(org.springframework.security.oauth.examples.sparklr.PhotoInfo) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PhotoInfo (org.springframework.security.oauth.examples.sparklr.PhotoInfo)8 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ArrayList (java.util.ArrayList)2 Authentication (org.springframework.security.core.Authentication)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Bean (org.springframework.context.annotation.Bean)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 PhotoServiceImpl (org.springframework.security.oauth.examples.sparklr.impl.PhotoServiceImpl)1 ContentNegotiationManagerFactoryBean (org.springframework.web.accept.ContentNegotiationManagerFactoryBean)1