View Javadoc
1   /**
2    * Copyright 2011-2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.writer.controllers;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.kuali.mobility.writer.entity.Media;
20  import org.kuali.mobility.writer.service.WriterService;
21  import org.springframework.beans.factory.annotation.Autowired;
22  import org.springframework.beans.factory.annotation.Qualifier;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.ui.Model;
25  import org.springframework.web.bind.annotation.*;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  
33  /**
34   * Controller for Writer media
35   *
36   * @author Kuali Mobility Team (mobility.collab@kuali.org)
37   * @since 3.1
38   */
39  @Controller 
40  @RequestMapping("/writer/{instance}/media")
41  public class WriterMediaController {
42  
43  	/**
44  	 * Reference to the WriterService
45  	 */
46  	@Autowired
47      @Qualifier("writerService")
48  	private WriterService service;
49  	
50  
51  	@RequestMapping(value = "/{mediaId}", method = RequestMethod.GET)
52  	public void getMedia(
53  			@PathVariable int mediaId,
54  			@RequestParam(value="thumb", required=false, defaultValue="false") boolean thumb,
55  			HttpServletRequest request,
56  			HttpServletResponse response) throws IOException {
57  		
58  		long dateChanged = request.getDateHeader("If-Modified-Since") / 1000;
59  		
60  		File mediaFile = service.getMediaFile(mediaId, thumb);
61  		long mediaChanged = mediaFile.lastModified() / 1000;
62  		if (dateChanged == mediaChanged){
63  			response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
64  			return;
65  		}
66  		Media media = service.getMedia(mediaId);
67  		response.setContentType(media.getMimeType());
68  		response.setDateHeader("Last-Modified", mediaFile.lastModified());
69  		
70  		int bytesWritten = IOUtils.copy(new FileInputStream(mediaFile), response.getOutputStream());
71  		response.setContentLength(bytesWritten);
72  		
73  	}
74  
75  	/**
76  	 * Controller to view an article's image
77  	 */
78  	@RequestMapping(value = "/view/{mediaId}", method = RequestMethod.GET)
79  	public String viewImage(
80  			@PathVariable(value="mediaId") long mediaId,
81  			@PathVariable("instance") String instance,
82  			Model uiModel) {
83  		
84  		// add to uiModel
85  		uiModel.addAttribute("mediaId", mediaId);
86  		uiModel.addAttribute("toolInstance", instance);
87  		return "writer/viewImage";
88  	}
89  	
90  }