001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.response;
014
015import java.io.*;
016import java.util.zip.*;
017
018import org.apache.juneau.rest.*;
019import org.apache.juneau.rest.annotation.*;
020import org.apache.juneau.utils.*;
021import org.apache.juneau.utils.ZipFileList.*;
022
023/**
024 * Response handler for ZipFileList objects.
025 * 
026 * <p>
027 * Can be associated with a REST resource using the {@link RestResource#responseHandlers} annotation.
028 * 
029 * <p>
030 * Sets the following headers:
031 * <ul class='spaced-list'>
032 *    <li>
033 *       <code>Content-Type</code> - <code>application/zip</code>
034 *    <li>
035 *       <code>Content-Disposition=attachment;filename=X</code> - Sets X to the file name passed in through the
036 *       constructor {@link ZipFileList#ZipFileList(String)}.
037 * </ul>
038 * 
039 * <h5 class='section'>See Also:</h5>
040 * <ul>
041 *    <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.MethodReturnTypes">Overview &gt; juneau-rest-server &gt; Method Return Types</a>
042 * </ul>
043 */
044public class ZipFileListResponseHandler implements ResponseHandler {
045
046   @Override /* ResponseHandler */
047   public boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException {
048      if (output.getClass() == ZipFileList.class) {
049         ZipFileList m = (ZipFileList)output;
050         res.setContentType("application/zip");
051         res.setHeader("Content-Disposition", "attachment;filename=" + m.fileName); //$NON-NLS-2$
052         try (OutputStream os = res.getOutputStream()) {
053            try (ZipOutputStream zos = new ZipOutputStream(os)) {
054               for (ZipFileEntry e : m)
055                  e.write(zos);
056            } catch (Exception e) {
057               e.printStackTrace();
058            }
059         }
060         return true;
061      }
062      return false;
063   }
064}