Project

General

Profile

« Previous | Next » 

Revision 4951

Added by daigle almost 15 years ago

Add scheduler and workflow schedule functionality

View differences:

ResponseUtil.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.util;
28 28

  
29
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
29 31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.OutputStream;
30 34

  
31 35
import javax.servlet.http.HttpServletRequest;
32 36
import javax.servlet.http.HttpServletResponse;
33 37

  
34 38
import org.apache.log4j.Logger;
35 39

  
40
import edu.ucsb.nceas.utilities.FileUtil;
41

  
36 42
public class ResponseUtil {
37 43
	
38 44
	private static Logger logMetacat = Logger.getLogger(ResponseUtil.class);
39 45
	
46
	private static int DEFAULT_BUFFER_SIZE = 4 * 1024; // 4K buffer
47
	
40 48
	/**
41 49
	 * private constructor - all methods are static so there is no
42 50
     * no need to instantiate.
......
52 60
	 *            the context-relative URL to which the request is forwarded
53 61
	 */
54 62
	public static void redirectResponse(HttpServletRequest request,
55
			HttpServletResponse response, String destination)
56
			throws IOException {
57
		logMetacat.debug("Redirecting response to " + request.getContextPath() + destination);
58
		response.sendRedirect(request.getContextPath() + destination);
63
			HttpServletResponse response, String destination) throws MetacatUtilException {
64
		try {
65
			logMetacat.debug("Redirecting response to " + request.getContextPath() + destination);
66
			response.sendRedirect(request.getContextPath() + destination);
67
		} catch (IOException ioe) {
68
			throw new MetacatUtilException("I/O error when redirecting response to: " + destination);
69
		}
59 70
	}
71
	
72
	public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName)
73
		throws MetacatUtilException {
74
		
75
		writeFileToOutput(response, fileDir, fileName, DEFAULT_BUFFER_SIZE);
76
	}
77
	
78
	public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName, int bufferSize)
79
			throws MetacatUtilException {
80
		String filePath = "";
81
		try {
82
			filePath = fileDir + FileUtil.getFS() + fileName;
83
			
84
			int lastFileSep = fileName.lastIndexOf(FileUtil.getFS());
85
			String shortFileName = fileName.substring(lastFileSep + 1, fileName.length());
86
			response.setHeader("Content-Disposition", "attachment; filename=\"" + shortFileName + "\"");
87
			
88
			InputStream inputStream = new FileInputStream(filePath);
89
			OutputStream outputStream = response.getOutputStream();
90
			
91
			byte[] byteBuffer = new byte[bufferSize]; 
92

  
93
			int b = 0;
94
			while ((b = inputStream.read(byteBuffer)) != -1) {
95
				outputStream.write(byteBuffer, 0, b);
96
			}	
97
			
98
		} catch (FileNotFoundException fnfe) {
99
			throw new MetacatUtilException("Error finding file: " + filePath 
100
					+ " when writing to output");
101
		} catch (IOException ioe) {
102
			throw new MetacatUtilException("I/O Error when writing: " + filePath 
103
					+ "  to output");
104
		}
105
	}
60 106
}

Also available in: Unified diff