View Javadoc

1   /**
2    * Copyright 2011 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.shared.controllers;
17  
18  import org.kuali.mobility.file.entity.File;
19  import org.kuali.mobility.file.service.FileService;
20  import org.springframework.beans.factory.annotation.Autowired;
21  import org.springframework.stereotype.Controller;
22  import org.springframework.ui.Model;
23  import org.springframework.web.bind.annotation.PathVariable;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.bind.annotation.RequestMethod;
26  import org.springframework.web.bind.annotation.ResponseBody;
27  import org.springframework.web.multipart.MultipartFile;
28  import org.springframework.web.multipart.MultipartHttpServletRequest;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.io.IOException;
33  import java.io.OutputStream;
34  import java.sql.Timestamp;
35  import java.util.List;
36  
37  @Controller 
38  @RequestMapping("/files")
39  public class FileController {
40  
41  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileController.class);
42  
43      @Autowired
44      private FileService fileService;
45  
46      @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
47      public void getFile(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) {
48      	    	
49      	File file = getFileService().findFileById(id);
50      	LOG.info("--- Retrieve File ---");
51      	LOG.info(file);
52  
53      	response.setContentType(file.getContentType());
54      	response.setContentLength(file.getBytes().length);
55      	try {
56  			OutputStream out = response.getOutputStream();
57  			out.write(file.getBytes());
58      	} catch (IOException e) {
59  			e.printStackTrace();
60  		}
61      	
62      	
63      }
64  
65      @RequestMapping(method = RequestMethod.GET)
66      public String index(HttpServletRequest request, Model uiModel) {
67      	List<File> files = getFileService().findAllFiles();
68      	uiModel.addAttribute("files", files);
69      	uiModel.addAttribute("fileCount", files.size());
70  
71      	return "files";
72      }
73      
74  	@SuppressWarnings("unchecked")
75  	@RequestMapping(value = "/remove/{fileHash}", method = RequestMethod.GET)
76  	public String removeFile(Model uiModel, HttpServletRequest request, @PathVariable("fileHash") Long fileHash) {
77  		File fileToDelete = getFileService().findFileById(fileHash);
78  		if(fileToDelete != null){
79  			LOG.info("Will delete file with Id: " + fileToDelete.getId());
80  			if(getFileService().removeFile(fileToDelete)){
81  				LOG.info("Did delete file.");
82  			}
83  		}
84  		
85      	return "files";		
86  	}	    
87      
88      @RequestMapping(value = "/save", method = RequestMethod.POST)
89      @ResponseBody
90      public String handleFormUpload(MultipartHttpServletRequest request) {
91      	    	
92      	MultipartFile mfile = request.getFile("file");
93      	// This constructor populates the fields in the File object. 
94      	File file = new File(mfile);
95  		file.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
96      	Long fileId = getFileService().saveFile(file);
97  
98      	LOG.info("--- Saving File ---");
99      	LOG.info(file);
100     	
101    		return "{\"name\":\"" + file.getFileName() + "\",\"fileid\":\"" + file.getId() + "\",\"size\":\"" + file.getBytes().length + "\"}";
102     }
103 
104 	public FileService getFileService() {
105 		return fileService;
106 	}
107 
108 	public void setFileService(FileService fileService) {
109 		this.fileService = fileService;
110 	}
111 }