//# 0 errors, 105 messages
//#
/*
    //#fileaction.java:1:1: class: net.sourceforge.pebble.web.action.FileAction
 * 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.action;

import net.sourceforge.pebble.Constants;
import net.sourceforge.pebble.domain.MultiBlog;
import net.sourceforge.pebble.domain.FileManager;
import net.sourceforge.pebble.domain.Blog;
import net.sourceforge.pebble.util.FileUtils;
import net.sourceforge.pebble.web.view.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Gets a file/image from a blog.
 *
 * @author    Simon Brown
 */
public class FileAction extends Action {
    //#fileaction.java:57: method: void net.sourceforge.pebble.web.action.FileAction.net.sourceforge.pebble.web.action.FileAction()
    //#input(void net.sourceforge.pebble.web.action.FileAction()): this
    //#fileaction.java:57: end of method: void net.sourceforge.pebble.web.action.FileAction.net.sourceforge.pebble.web.action.FileAction()

  /** the log used by this class */
  private static final Log log = LogFactory.getLog(FileAction.class);
    //#fileaction.java:60: method: net.sourceforge.pebble.web.action.FileAction.net.sourceforge.pebble.web.action.FileAction__static_init
    //#fileaction.java:60: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: net.sourceforge.pebble.web.action.FileAction__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): __Descendant_Table[net/sourceforge/pebble/web/action/FileAction]
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.getLocalizedString(Ljava/lang/String;)Ljava/lang/String;
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lnet/sourceforge/pebble/web/view/View;
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): log
    //#output(net.sourceforge.pebble.web.action.FileAction__static_init): net/sourceforge/pebble/web/action/Action.__Descendant_Table[net/sourceforge/pebble/web/action/FileAction]
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): __Descendant_Table[net/sourceforge/pebble/web/action/FileAction] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): net/sourceforge/pebble/web/action/Action.__Descendant_Table[net/sourceforge/pebble/web/action/FileAction] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.getLocalizedString(Ljava/lang/String;)Ljava/lang/String; == &net/sourceforge/pebble/web/action/Action.getLocalizedString
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model; == &net/sourceforge/pebble/web/action/Action.getModel
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lnet/sourceforge/pebble/web/view/View; == &process
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V == &net/sourceforge/pebble/web/action/Action.setModel
    //#post(net.sourceforge.pebble.web.action.FileAction__static_init): init'ed(log)
    //#fileaction.java:60: end of method: net.sourceforge.pebble.web.action.FileAction.net.sourceforge.pebble.web.action.FileAction__static_init

  /**
   * Peforms the processing associated with this action.
   *
   * @param request  the HttpServletRequest instance
   * @param response the HttpServletResponse instance
   * @return the name of the next view
   */
  public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    Object o = request.getAttribute(Constants.BLOG_KEY);
    //#fileaction.java:70: method: View net.sourceforge.pebble.web.action.FileAction.process(HttpServletRequest, HttpServletResponse)
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Descendant_Table[net/sourceforge/pebble/web/action/FileAction]
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Descendant_Table[others]
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[net/sourceforge/pebble/web/model/Model]
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[others]
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Dispatch_Table.get(Ljava/lang/String;)Ljava/lang/Object;
    //#input(View process(HttpServletRequest, HttpServletResponse)): request
    //#input(View process(HttpServletRequest, HttpServletResponse)): response
    //#input(View process(HttpServletRequest, HttpServletResponse)): this
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.__Tag
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model.__Tag
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model.data
    //#output(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15).__Tag
    //#output(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15).file
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2).__Tag
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2).uri
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6).__Tag
    //#output(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6).uri
    //#output(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#1) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#5) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#12) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#14) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): return_value
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#1)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#5)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#12)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#14)
    //#pre[1] (View process(HttpServletRequest, HttpServletResponse)): request != null
    //#pre[2] (View process(HttpServletRequest, HttpServletResponse)): (soft) response != null
    //#pre[4] (View process(HttpServletRequest, HttpServletResponse)): (soft) this.__Tag == net/sourceforge/pebble/web/action/FileAction
    //#pre[5] (View process(HttpServletRequest, HttpServletResponse)): (soft) this.model != null
    //#pre[6] (View process(HttpServletRequest, HttpServletResponse)): (soft) this.model.__Tag == net/sourceforge/pebble/web/model/Model
    //#pre[7] (View process(HttpServletRequest, HttpServletResponse)): (soft) this.model.data != null
    //#presumption(View process(HttpServletRequest, HttpServletResponse)): java.util.HashMap:get(...)@63 != null
    //#presumption(View process(HttpServletRequest, HttpServletResponse)): net.sourceforge.pebble.domain.Blog:getCalendar(...)@108 != null
    //#presumption(View process(HttpServletRequest, HttpServletResponse)): net.sourceforge.pebble.domain.FileManager:getFile(...)@86 != null
    //#presumption(View process(HttpServletRequest, HttpServletResponse)): net.sourceforge.pebble.domain.FileManager:getRootDirectory(...)@85 != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): return_value in Addr_Set{&new NotModifiedView(process#12),&new NotModifiedView(process#14),&new FileView(process#15),&new ForwardView(process#6),&new NotFoundView(process#5),&new ForwardView(process#2),&new NotFoundView(process#1)}
    //#post(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new FileView(process#15).__Tag == net/sourceforge/pebble/web/view/FileView
    //#post(View process(HttpServletRequest, HttpServletResponse)): (soft) new FileView(process#15).file != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2).__Tag == net/sourceforge/pebble/web/view/ForwardView
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#2).uri != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6).__Tag == net/sourceforge/pebble/web/view/ForwardView
    //#post(View process(HttpServletRequest, HttpServletResponse)): new ForwardView(process#6).uri != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#1) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#5) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#12) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new NotModifiedView(process#14) num objects <= 1
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.util.HashMap:get
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:net.sourceforge.pebble.web.view.View
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.io.File:getCanonicalFile
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.io.File:equals
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.io.File:getParentFile
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:net.sourceforge.pebble.web.view.BinaryView
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.io.File:exists(...)@88: {1}, {0}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.io.File:isDirectory(...)@93: {0}, {1}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.lang.String:equals(...)@112: {0}, {1}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.lang.String:equals(...)@114: {0}, {1}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.lang.String:equals(...)@79: {0}, {1}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): java.lang.String:length(...)@79: {0}, {1..4_294_967_295}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getHeader(...)@100: Addr_Set{null}, Inverse{null}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getHeader(...)@101: Addr_Set{null}, Inverse{null}
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getParameter(...)@77: Addr_Set{null}, Inverse{null}
    if (o instanceof MultiBlog) {
      return new NotFoundView();
    //#fileaction.java:72: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.NotFoundView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.NotFoundView()
    }

    Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);

    String name = request.getParameter("name");
    String type = request.getParameter("type");
    if (name == null || name.length() == 0 || name.equals("/")) {
      // forward to secure file browser
      return new ForwardView("/viewFiles.secureaction?type=" + type);
    }

    FileManager fileManager = new FileManager(blog, type);
    //#fileaction.java:84: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.domain.FileManager(Blog, String)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.domain.FileManager(Blog, String)
    File root = fileManager.getRootDirectory();
    //#fileaction.java:85: Warning: method not available
    //#    -- call on File net.sourceforge.pebble.domain.FileManager:getRootDirectory()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: File net.sourceforge.pebble.domain.FileManager:getRootDirectory()
    File file = fileManager.getFile(name);
    //#fileaction.java:86: Warning: method not available
    //#    -- call on File net.sourceforge.pebble.domain.FileManager:getFile(String)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: File net.sourceforge.pebble.domain.FileManager:getFile(String)

    if (!file.exists()) {
      // file doesn't exist so send back a 404
      return new NotFoundView();
    //#fileaction.java:90: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.NotFoundView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.NotFoundView()
    }

    if (!FileUtils.underneathRoot(root, file) || file.isDirectory()) {
      // forward to secure file browser
      return new ForwardView("/viewFiles.secureaction?type=" + type + "&path=" + name);
    }

    SimpleDateFormat httpFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    httpFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String ifModifiedSince = request.getHeader("If-Modified-Since");
    String ifNoneMatch = request.getHeader("If-None-Match");
    Date lastModified = new Date(file.lastModified());
    response.setDateHeader("Last-Modified", lastModified.getTime());
    response.setHeader("ETag", "\"" + httpFormat.format(lastModified) + "\"");

    // and finally the last modified date
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    Calendar expires = blog.getCalendar();
    //#fileaction.java:108: Warning: method not available
    //#    -- call on Calendar net.sourceforge.pebble.domain.Blog:getCalendar()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: Calendar net.sourceforge.pebble.domain.Blog:getCalendar()
    expires.add(Calendar.MONTH, 1);
    response.setHeader("Expires", sdf.format(expires.getTime()));

    if (ifModifiedSince != null && ifModifiedSince.equals(httpFormat.format(lastModified))) {
      return new NotModifiedView();
    //#fileaction.java:113: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.NotModifiedView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.NotModifiedView()
    } else if (ifNoneMatch != null && ifNoneMatch.equals("\"" + httpFormat.format(lastModified) + "\"")) {
      return new NotModifiedView();
    //#fileaction.java:115: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.NotModifiedView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.FileAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.NotModifiedView()
    } else {
      return new FileView(file);
    //#fileaction.java:117: end of method: View net.sourceforge.pebble.web.action.FileAction.process(HttpServletRequest, HttpServletResponse)
    }
  }

}    //#fileaction.java:: end of class: net.sourceforge.pebble.web.action.FileAction
