File Source: fileaction.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.web.action.FileAction__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.web.action;
    33  
    34  import net.sourceforge.pebble.Constants;
    35  import net.sourceforge.pebble.domain.MultiBlog;
    36  import net.sourceforge.pebble.domain.FileManager;
    37  import net.sourceforge.pebble.domain.Blog;
    38  import net.sourceforge.pebble.util.FileUtils;
    39  import net.sourceforge.pebble.web.view.*;
    40  import org.apache.commons.logging.Log;
    41  import org.apache.commons.logging.LogFactory;
    42  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.web.action.FileAction()
          */
    43  import javax.servlet.ServletException;
    44  import javax.servlet.http.HttpServletRequest;
    45  import javax.servlet.http.HttpServletResponse;
    46  import java.io.File;
    47  import java.text.SimpleDateFormat;
    48  import java.util.Calendar;
    49  import java.util.Date;
    50  import java.util.TimeZone;
    51  
    52  /**
    53   * Gets a file/image from a blog.
    54   *
    55   * @author    Simon Brown
    56   */
    57  public class FileAction extends Action {
    58  
    59    /** the log used by this class */
    60    private static final Log log = LogFactory.getLog(FileAction.class);
    61  
    62    /**
    63     * Peforms the processing associated with this action.
    64     *
    65     * @param request  the HttpServletRequest instance
    66     * @param response the HttpServletResponse instance
    67     * @return the name of the next view
    68     */
    69    public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
             /* 
    P/P       *  Method: View process(HttpServletRequest, HttpServletResponse)
              * 
              *  Preconditions:
              *    request != null
              *    (soft) response != null
              *    (soft) this.model != null
              *    (soft) this.model.data != null
              * 
              *  Presumptions:
              *    java.util.HashMap:get(...)@63 != null
              *    net.sourceforge.pebble.domain.Blog:getCalendar(...)@108 != null
              *    net.sourceforge.pebble.domain.FileManager:getFile(...)@86 != null
              *    net.sourceforge.pebble.domain.FileManager:getRootDirectory(...)@85 != null
              * 
              *  Postconditions:
              *    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)}
              *    new FileView(process#15) num objects <= 1
              *    (soft) new FileView(process#15).file != null
              *    new ForwardView(process#2) num objects <= 1
              *    new ForwardView(process#2).uri != null
              *    new ForwardView(process#6) num objects <= 1
              *    new ForwardView(process#6).uri != null
              *    new NotFoundView(process#1) num objects <= 1
              *    new NotFoundView(process#5) num objects <= 1
              *    new NotModifiedView(process#12) num objects <= 1
              *    ...
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@88: {1}, {0}
              *    java.io.File:isDirectory(...)@93: {0}, {1}
              *    java.lang.String:equals(...)@112: {0}, {1}
              *    java.lang.String:equals(...)@114: {0}, {1}
              *    java.lang.String:equals(...)@79: {0}, {1}
              *    java.lang.String:length(...)@79: {0}, {1..232-1}
              *    javax.servlet.http.HttpServletRequest:getHeader(...)@100: Addr_Set{null}, Inverse{null}
              *    javax.servlet.http.HttpServletRequest:getHeader(...)@101: Addr_Set{null}, Inverse{null}
              *    javax.servlet.http.HttpServletRequest:getParameter(...)@77: Addr_Set{null}, Inverse{null}
              */
    70      Object o = request.getAttribute(Constants.BLOG_KEY);
    71      if (o instanceof MultiBlog) {
    72        return new NotFoundView();
    73      }
    74  
    75      Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
    76  
    77      String name = request.getParameter("name");
    78      String type = request.getParameter("type");
    79      if (name == null || name.length() == 0 || name.equals("/")) {
    80        // forward to secure file browser
    81        return new ForwardView("/viewFiles.secureaction?type=" + type);
    82      }
    83  
    84      FileManager fileManager = new FileManager(blog, type);
    85      File root = fileManager.getRootDirectory();
    86      File file = fileManager.getFile(name);
    87  
    88      if (!file.exists()) {
    89        // file doesn't exist so send back a 404
    90        return new NotFoundView();
    91      }
    92  
    93      if (!FileUtils.underneathRoot(root, file) || file.isDirectory()) {
    94        // forward to secure file browser
    95        return new ForwardView("/viewFiles.secureaction?type=" + type + "&path=" + name);
    96      }
    97  
    98      SimpleDateFormat httpFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    99      httpFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
   100      String ifModifiedSince = request.getHeader("If-Modified-Since");
   101      String ifNoneMatch = request.getHeader("If-None-Match");
   102      Date lastModified = new Date(file.lastModified());
   103      response.setDateHeader("Last-Modified", lastModified.getTime());
   104      response.setHeader("ETag", "\"" + httpFormat.format(lastModified) + "\"");
   105  
   106      // and finally the last modified date
   107      SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
   108      Calendar expires = blog.getCalendar();
   109      expires.add(Calendar.MONTH, 1);
   110      response.setHeader("Expires", sdf.format(expires.getTime()));
   111  
   112      if (ifModifiedSince != null && ifModifiedSince.equals(httpFormat.format(lastModified))) {
   113        return new NotModifiedView();
   114      } else if (ifNoneMatch != null && ifNoneMatch.equals("\"" + httpFormat.format(lastModified) + "\"")) {
   115        return new NotModifiedView();
   116      } else {
   117        return new FileView(file);
   118      }
   119    }
   120  
   121  }








SofCheck Inspector Build Version : 2.22510
fileaction.java 2010-Jun-25 19:40:34
fileaction.class 2010-Jul-19 20:23:38