001/*
002 * Copyright 2011 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys.util;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.zip.ZipEntry;
023import java.util.zip.ZipOutputStream;
024
025import javax.servlet.http.HttpServletResponse;
026
027public class KfsWebUtils {
028
029    /**
030     * Zipoutput files that are not of type text/plain or text/html.
031     * 
032     * @param response
033     * @param contentType
034     * @param outputStreamMap<filename, outputStream>
035     * @param zipFileName
036     * @throws IOException
037     */
038    public static void saveMimeZipOutputStreamAsFile(HttpServletResponse response, String contentType, Map<String, ByteArrayOutputStream> outputStreamMap, String zipFileName) throws IOException {
039    
040        // set response
041        response.setContentType(contentType);
042        response.setHeader("Content-disposition", "attachment; filename=" + zipFileName);
043        response.setHeader("Expires", "0");
044        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
045        response.setHeader("Pragma", "public");
046    
047        // write to zipoutput
048        ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());
049        int totalSize = 0;
050        Iterator<String> fileNames = outputStreamMap.keySet().iterator();
051        while (fileNames.hasNext()) {
052            String fileName = fileNames.next();
053            ByteArrayOutputStream pdfStream = outputStreamMap.get(fileName);
054            totalSize += pdfStream.size();
055            zout.putNextEntry(new ZipEntry(fileName));
056            zout.write(pdfStream.toByteArray());
057            zout.closeEntry();
058        }
059        response.setContentLength(totalSize);
060        zout.flush();
061        zout.close();        
062    }
063    
064}