//# 0 errors, 49 messages
//#
/*
    //#zipview.java:1:1: class: net.sourceforge.pebble.web.view.ZipView
    //#zipview.java:1:1: method: net.sourceforge.pebble.web.view.ZipView.net.sourceforge.pebble.web.view.ZipView__static_init
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble.web.view;

import net.sourceforge.pebble.domain.FileMetaData;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Represents a binary view component and prepares the model for display.
 *
 * @author    Simon Brown
 */
public class ZipView extends BinaryView {

  private List files;
  private String filename;

  public ZipView(List files, String filename) {
    //#zipview.java:58: method: void net.sourceforge.pebble.web.view.ZipView.net.sourceforge.pebble.web.view.ZipView(List, String)
    //#zipview.java:58: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.BinaryView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.ZipView
    //#    method: void net.sourceforge.pebble.web.view.ZipView(List, String)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.BinaryView()
    //#input(void net.sourceforge.pebble.web.view.ZipView(List, String)): filename
    //#input(void net.sourceforge.pebble.web.view.ZipView(List, String)): files
    //#input(void net.sourceforge.pebble.web.view.ZipView(List, String)): this
    //#output(void net.sourceforge.pebble.web.view.ZipView(List, String)): this.filename
    //#output(void net.sourceforge.pebble.web.view.ZipView(List, String)): this.files
    //#post(void net.sourceforge.pebble.web.view.ZipView(List, String)): this.filename == filename
    //#post(void net.sourceforge.pebble.web.view.ZipView(List, String)): init'ed(this.filename)
    //#post(void net.sourceforge.pebble.web.view.ZipView(List, String)): this.files == files
    //#post(void net.sourceforge.pebble.web.view.ZipView(List, String)): init'ed(this.files)
    this.files = files;
    this.filename = filename;
  }
    //#zipview.java:61: end of method: void net.sourceforge.pebble.web.view.ZipView.net.sourceforge.pebble.web.view.ZipView(List, String)

  /**
   * Gets the title of this view.
   *
   * @return the title as a String
   */
  public String getContentType() {
    return "application/zip";
    //#zipview.java:69: method: String net.sourceforge.pebble.web.view.ZipView.getContentType()
    //#output(String getContentType()): return_value
    //#post(String getContentType()): return_value == &"application.zip"
    //#zipview.java:69: end of method: String net.sourceforge.pebble.web.view.ZipView.getContentType()
  }

  public long getContentLength() {
    return 0;
    //#zipview.java:73: method: long net.sourceforge.pebble.web.view.ZipView.getContentLength()
    //#output(long getContentLength()): return_value
    //#post(long getContentLength()): return_value == 0
    //#zipview.java:73: end of method: long net.sourceforge.pebble.web.view.ZipView.getContentLength()
  }

  /**
   * Dispatches this view.
   *
   * @param request  the HttpServletRequest instance
   * @param response the HttpServletResponse instance
   * @param context
   */
  public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {
    try {
      response.setHeader("Content-Disposition", "filename=" + filename);
    //#zipview.java:85: method: void net.sourceforge.pebble.web.view.ZipView.dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): response
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.filename
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.files
    //#pre[1] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): response != null
    //#pre[3] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): init'ed(this.filename)
    //#pre[4] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.files != null
    //#presumption(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.util.Iterator:next(...)@90 != null
    //#presumption(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): net.sourceforge.pebble.domain.FileMetaData:getAbsolutePath(...)@96 != null
    //#test_vector(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.io.FileInputStream:read(...)@98: {1..4_294_967_295}, {-2_147_483_648..0}
    //#test_vector(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.util.Iterator:hasNext(...)@89: {1}, {0}
    //#test_vector(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): net.sourceforge.pebble.domain.FileMetaData:isDirectory(...)@91: {0}, {1}
      byte[] buf = new byte[1024];
      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
      Iterator it = files.iterator();
      while (it.hasNext()) {
        FileMetaData file = (FileMetaData)it.next();
        if (file.isDirectory()) {
    //#zipview.java:91: Warning: method not available
    //#    -- call on bool net.sourceforge.pebble.domain.FileMetaData:isDirectory()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.ZipView
    //#    method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#    unanalyzed callee: bool net.sourceforge.pebble.domain.FileMetaData:isDirectory()
          continue;
        }

        FileInputStream in = new FileInputStream(file.getFile());
    //#zipview.java:95: Warning: method not available
    //#    -- call on File net.sourceforge.pebble.domain.FileMetaData:getFile()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.ZipView
    //#    method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#    unanalyzed callee: File net.sourceforge.pebble.domain.FileMetaData:getFile()
        out.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(1)));
    //#zipview.java:96: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.FileMetaData:getAbsolutePath()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.ZipView
    //#    method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.FileMetaData:getAbsolutePath()
        int len;
        while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
      }

      out.close();
    } catch (IOException ioe) {
      throw new ServletException(ioe);
    }
  }
    //#zipview.java:109: end of method: void net.sourceforge.pebble.web.view.ZipView.dispatch(HttpServletRequest, HttpServletResponse, ServletContext)

}
    //#output(net.sourceforge.pebble.web.view.ZipView__static_init): __Descendant_Table[net/sourceforge/pebble/web/view/ZipView]
    //#output(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.dispatch(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/ServletContext;)V
    //#output(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.getContentLength()J
    //#output(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.getContentType()Ljava/lang/String;
    //#post(net.sourceforge.pebble.web.view.ZipView__static_init): __Descendant_Table[net/sourceforge/pebble/web/view/ZipView] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.dispatch(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/ServletContext;)V == &dispatch
    //#post(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.getContentLength()J == &getContentLength
    //#post(net.sourceforge.pebble.web.view.ZipView__static_init): __Dispatch_Table.getContentType()Ljava/lang/String; == &getContentType
    //#zipview.java:: end of method: net.sourceforge.pebble.web.view.ZipView.net.sourceforge.pebble.web.view.ZipView__static_init
    //#zipview.java:: end of class: net.sourceforge.pebble.web.view.ZipView
