View Javadoc

1   package org.springframework.security.oauth.examples.tonr.mvc;
2   
3   import java.awt.image.BufferedImage;
4   import java.io.InputStream;
5   import java.util.Iterator;
6   
7   import javax.imageio.ImageIO;
8   import javax.imageio.ImageReadParam;
9   import javax.imageio.ImageReader;
10  import javax.imageio.stream.MemoryCacheImageInputStream;
11  import javax.servlet.UnavailableException;
12  
13  import org.springframework.http.HttpHeaders;
14  import org.springframework.http.HttpStatus;
15  import org.springframework.http.MediaType;
16  import org.springframework.http.ResponseEntity;
17  import org.springframework.http.converter.HttpMessageNotReadableException;
18  import org.springframework.security.oauth.examples.tonr.SparklrService;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.ui.Model;
21  import org.springframework.web.bind.annotation.PathVariable;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  
24  /**
25   * @author Ryan Heaton
26   * @author Dave Syer
27   */
28  @Controller
29  public class SparklrController {
30  
31  	private SparklrService sparklrService;
32  
33  	@RequestMapping("/sparklr/photos")
34  	public String photos(Model model) throws Exception {
35  		model.addAttribute("photoIds", sparklrService.getSparklrPhotoIds());
36  		return "sparklr";
37  	}
38  
39  	@RequestMapping("/sparklr/photos/{id}")
40  	public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
41  		InputStream photo = sparklrService.loadSparklrPhoto(id);
42  		if (photo == null) {
43  			throw new UnavailableException("The requested photo does not exist");
44  		}
45  		BufferedImage body;
46  		MediaType contentType = MediaType.IMAGE_JPEG;
47  		Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
48  		if (imageReaders.hasNext()) {
49  			ImageReader imageReader = imageReaders.next();
50  			ImageReadParam irp = imageReader.getDefaultReadParam();
51  			imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
52  			body = imageReader.read(0, irp);
53  		} else {
54  			throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type ["
55  					+ contentType + "]");
56  		}
57  		HttpHeaders headers = new HttpHeaders();
58  		headers.setContentType(MediaType.IMAGE_JPEG);
59  		return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
60  	}
61  
62  	@RequestMapping("/trusted/message")
63  	public String trusted(Model model) throws Exception {
64  		model.addAttribute("message", this.sparklrService.getTrustedMessage());
65  		return "home";
66  	}
67  
68  	public void setSparklrService(SparklrService sparklrService) {
69  		this.sparklrService = sparklrService;
70  	}
71  
72  }