|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This report was generated with an evaluation server license. Purchase Clover or configure your license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FileAction | Line # 57 | 31 | 0% | 12 | 25 | 43,2% |
0.4318182
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (6) | |||
| Result | |||
|
0.38636363
|
net.sourceforge.pebble.web.action.FileActionTest.testAccessToParentDirectoriesIsDeniedToAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testAccessToParentDirectoriesIsDeniedToAnonymousUsers
|
1 PASS | |
|
0.38636363
|
net.sourceforge.pebble.web.action.FileActionTest.testAccessToParentDirectoriesIsDeniedToAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testAccessToParentDirectoriesIsDeniedToAnonymousUsers
|
1 PASS | |
|
0.22727273
|
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenWhenRootRequestedByAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenWhenRootRequestedByAnonymousUsers
|
1 PASS | |
|
0.22727273
|
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenByAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenByAnonymousUsers
|
1 PASS | |
|
0.22727273
|
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenByAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenByAnonymousUsers
|
1 PASS | |
|
0.22727273
|
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenWhenRootRequestedByAnonymousUsers
net.sourceforge.pebble.web.action.FileActionTest.testBrowsingForbiddenWhenRootRequestedByAnonymousUsers
|
1 PASS | |
| 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 | ||
| 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 | 6 |
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
| 70 | 6 | Object o = request.getAttribute(Constants.BLOG_KEY); |
| 71 | 6 | if (o instanceof MultiBlog) { |
| 72 | 0 | return new NotFoundView(); |
| 73 | } | |
| 74 | ||
| 75 | 6 | Blog blog = (Blog)getModel().get(Constants.BLOG_KEY); |
| 76 | ||
| 77 | 6 | String name = request.getParameter("name"); |
| 78 | 6 | String type = request.getParameter("type"); |
| 79 | 6 | if (name == null || name.length() == 0 || name.equals("/")) { |
| 80 | // forward to secure file browser | |
| 81 | 4 | return new ForwardView("/viewFiles.secureaction?type=" + type); |
| 82 | } | |
| 83 | ||
| 84 | 2 | FileManager fileManager = new FileManager(blog, type); |
| 85 | 2 | File root = fileManager.getRootDirectory(); |
| 86 | 2 | File file = fileManager.getFile(name); |
| 87 | ||
| 88 | 2 | if (!file.exists()) { |
| 89 | // file doesn't exist so send back a 404 | |
| 90 | 0 | return new NotFoundView(); |
| 91 | } | |
| 92 | ||
| 93 | 2 | if (!FileUtils.underneathRoot(root, file) || file.isDirectory()) { |
| 94 | // forward to secure file browser | |
| 95 | 2 | return new ForwardView("/viewFiles.secureaction?type=" + type + "&path=" + name); |
| 96 | } | |
| 97 | ||
| 98 | 0 | SimpleDateFormat httpFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); |
| 99 | 0 | httpFormat.setTimeZone(TimeZone.getTimeZone("GMT")); |
| 100 | 0 | String ifModifiedSince = request.getHeader("If-Modified-Since"); |
| 101 | 0 | String ifNoneMatch = request.getHeader("If-None-Match"); |
| 102 | 0 | Date lastModified = new Date(file.lastModified()); |
| 103 | 0 | response.setDateHeader("Last-Modified", lastModified.getTime()); |
| 104 | 0 | response.setHeader("ETag", "\"" + httpFormat.format(lastModified) + "\""); |
| 105 | ||
| 106 | // and finally the last modified date | |
| 107 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); |
| 108 | 0 | Calendar expires = blog.getCalendar(); |
| 109 | 0 | expires.add(Calendar.MONTH, 1); |
| 110 | 0 | response.setHeader("Expires", sdf.format(expires.getTime())); |
| 111 | ||
| 112 | 0 | if (ifModifiedSince != null && ifModifiedSince.equals(httpFormat.format(lastModified))) { |
| 113 | 0 | return new NotModifiedView(); |
| 114 | 0 | } else if (ifNoneMatch != null && ifNoneMatch.equals("\"" + httpFormat.format(lastModified) + "\"")) { |
| 115 | 0 | return new NotModifiedView(); |
| 116 | } else { | |
| 117 | 0 | return new FileView(file); |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | } | |
|
||||||||||||