//# 0 errors, 99 messages
//#
/*
    //#fileview.java:1:1: class: net.sourceforge.pebble.web.view.FileView
 * 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.util.FileUtils;
import net.sourceforge.pebble.domain.AbstractBlog;
import net.sourceforge.pebble.domain.Blog;
import net.sourceforge.pebble.domain.FileMetaData;
import net.sourceforge.pebble.Constants;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

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

  /** the log used by this class */
  private static Log log = LogFactory.getLog(FileView.class);
    //#fileview.java:57: method: net.sourceforge.pebble.web.view.FileView.net.sourceforge.pebble.web.view.FileView__static_init
    //#fileview.java:57: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.FileView
    //#    method: net.sourceforge.pebble.web.view.FileView__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Descendant_Table[net/sourceforge/pebble/web/view/FileView]
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.dispatch(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/ServletContext;)V
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getContentLength()J
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getContentType()Ljava/lang/String;
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getServletContext()Ljavax/servlet/ServletContext;
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.prepare()V
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.setServletContext(Ljavax/servlet/ServletContext;)V
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): log
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): net/sourceforge/pebble/web/view/BinaryView.__Descendant_Table[net/sourceforge/pebble/web/view/FileView]
    //#output(net.sourceforge.pebble.web.view.FileView__static_init): net/sourceforge/pebble/web/view/View.__Descendant_Table[net/sourceforge/pebble/web/view/FileView]
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Descendant_Table[net/sourceforge/pebble/web/view/FileView] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): net/sourceforge/pebble/web/view/BinaryView.__Descendant_Table[net/sourceforge/pebble/web/view/FileView] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): net/sourceforge/pebble/web/view/View.__Descendant_Table[net/sourceforge/pebble/web/view/FileView] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.dispatch(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/ServletContext;)V == &dispatch
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getContentLength()J == &getContentLength
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getContentType()Ljava/lang/String; == &getContentType
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model; == &net/sourceforge/pebble/web/view/View.getModel
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.getServletContext()Ljavax/servlet/ServletContext; == &net/sourceforge/pebble/web/view/View.getServletContext
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.prepare()V == &net/sourceforge/pebble/web/view/View.prepare
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V == &net/sourceforge/pebble/web/view/View.setModel
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): __Dispatch_Table.setServletContext(Ljavax/servlet/ServletContext;)V == &net/sourceforge/pebble/web/view/View.setServletContext
    //#post(net.sourceforge.pebble.web.view.FileView__static_init): init'ed(log)
    //#fileview.java:57: end of method: net.sourceforge.pebble.web.view.FileView.net.sourceforge.pebble.web.view.FileView__static_init

  private File file;

  public FileView(File file) {
    //#fileview.java:61: method: void net.sourceforge.pebble.web.view.FileView.net.sourceforge.pebble.web.view.FileView(File)
    //#input(void net.sourceforge.pebble.web.view.FileView(File)): file
    //#input(void net.sourceforge.pebble.web.view.FileView(File)): this
    //#output(void net.sourceforge.pebble.web.view.FileView(File)): this.file
    //#post(void net.sourceforge.pebble.web.view.FileView(File)): this.file == file
    //#post(void net.sourceforge.pebble.web.view.FileView(File)): init'ed(this.file)
    //#unanalyzed(void net.sourceforge.pebble.web.view.FileView(File)): Effects-of-calling:net.sourceforge.pebble.web.view.View
    this.file = file;
  }
    //#fileview.java:63: end of method: void net.sourceforge.pebble.web.view.FileView.net.sourceforge.pebble.web.view.FileView(File)

  /**
   * Gets the title of this view.
   *
   * @return the title as a String
   */
  public String getContentType() {
    String mimeType = null;
    //#fileview.java:71: method: String net.sourceforge.pebble.web.view.FileView.getContentType()
    //#input(String getContentType()): __Descendant_Table[net/sourceforge/pebble/web/view/FileView]
    //#input(String getContentType()): __Descendant_Table[others]
    //#input(String getContentType()): __Dispatch_Table.getServletContext()Ljavax/servlet/ServletContext;
    //#input(String getContentType()): net/sourceforge/pebble/util/FileUtils.localFileNameMap
    //#input(String getContentType()): this
    //#input(String getContentType()): this.__Tag
    //#input(String getContentType()): this.file
    //#input(String getContentType()): this.servletContext
    //#output(String getContentType()): return_value
    //#pre[3] (String getContentType()): this.__Tag == net/sourceforge/pebble/web/view/FileView
    //#pre[5] (String getContentType()): init'ed(this.servletContext)
    //#pre[1] (String getContentType()): (soft) net/sourceforge/pebble/util/FileUtils.localFileNameMap != null
    //#pre[4] (String getContentType()): (soft) this.file != null
    //#presumption(String getContentType()): java.io.File:getName(...)@78 != null
    //#post(String getContentType()): init'ed(return_value)
    //#unanalyzed(String getContentType()): Effects-of-calling:java.net.URLConnection:getFileNameMap
    //#unanalyzed(String getContentType()): Effects-of-calling:java.net.FileNameMap:getContentTypeFor
    //#unanalyzed(String getContentType()): Effects-of-calling:java.lang.String:lastIndexOf
    //#unanalyzed(String getContentType()): Effects-of-calling:java.lang.String:substring
    //#unanalyzed(String getContentType()): Effects-of-calling:java.util.Properties:getProperty
    //#test_vector(String getContentType()): this.servletContext: Addr_Set{null}, Inverse{null}
    ServletContext servletContext = getServletContext();
    if (servletContext != null) {
      mimeType = servletContext.getMimeType(file.getName());
    }

    if (mimeType == null) {
      return FileUtils.getContentType(file.getName());
    }

    return mimeType;
    //#fileview.java:81: end of method: String net.sourceforge.pebble.web.view.FileView.getContentType()
  }

  public long getContentLength() {
    return file.length();
    //#fileview.java:85: method: long net.sourceforge.pebble.web.view.FileView.getContentLength()
    //#input(long getContentLength()): this
    //#input(long getContentLength()): this.file
    //#output(long getContentLength()): return_value
    //#pre[2] (long getContentLength()): this.file != null
    //#post(long getContentLength()): init'ed(return_value)
    //#fileview.java:85: end of method: long net.sourceforge.pebble.web.view.FileView.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 {
    response.setContentLength((int)getContentLength());
    //#fileview.java:96: method: void net.sourceforge.pebble.web.view.FileView.dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): __Descendant_Table[net/sourceforge/pebble/web/view/FileView]
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): __Descendant_Table[others]
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): __Dispatch_Table.getContentLength()J
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): log
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[net/sourceforge/pebble/web/model/Model]
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[others]
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): net/sourceforge/pebble/web/model/Model.__Dispatch_Table.get(Ljava/lang/String;)Ljava/lang/Object;
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): request
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): response
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.__Tag
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.file
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model.__Tag
    //#input(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model.data
    //#pre[3] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): response != null
    //#pre[5] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.__Tag == net/sourceforge/pebble/web/view/FileView
    //#pre[6] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.file != null
    //#pre[7] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model != null
    //#pre[8] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model.__Tag == net/sourceforge/pebble/web/model/Model
    //#pre[9] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): this.model.data != null
    //#pre[1] (void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): (soft) log != null
    //#presumption(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.io.File:length(...)@85 in -2_147_483_648..4_294_967_295
    //#presumption(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.util.HashMap:get(...)@63 != null
    //#presumption(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): java.util.HashMap:get(...)@63 != null
    //#unanalyzed(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): Effects-of-calling:java.io.File:length
    //#unanalyzed(void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)): Effects-of-calling:java.util.HashMap:get
    try {
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
      BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

      byte[] buf = new byte[1024];
      int len = in.read(buf);
      while (len > 0) {
          out.write(buf, 0, len);
          len = in.read(buf);
      }

      in.close();
      out.close();
    } catch (IOException ioe) {
      log.warn(ioe);
    //#fileview.java:111: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:warn(Object)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.FileView
    //#    method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:warn(Object)
    } finally {
      AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
      blog.log(request, HttpServletResponse.SC_OK);
    //#fileview.java:114: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.domain.AbstractBlog:log(HttpServletRequest, int)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.view.FileView
    //#    method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
    //#    unanalyzed callee: void net.sourceforge.pebble.domain.AbstractBlog:log(HttpServletRequest, int)
    }
  }
    //#fileview.java:116: end of method: void net.sourceforge.pebble.web.view.FileView.dispatch(HttpServletRequest, HttpServletResponse, ServletContext)

}
    //#fileview.java:: end of class: net.sourceforge.pebble.web.view.FileView
