| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
package net.sourceforge.pebble.web.view; |
| 33 |
|
|
| 34 |
|
import net.sourceforge.pebble.util.FileUtils; |
| 35 |
|
import net.sourceforge.pebble.domain.AbstractBlog; |
| 36 |
|
import net.sourceforge.pebble.domain.Blog; |
| 37 |
|
import net.sourceforge.pebble.domain.FileMetaData; |
| 38 |
|
import net.sourceforge.pebble.Constants; |
| 39 |
|
|
| 40 |
|
import javax.servlet.ServletContext; |
| 41 |
|
import javax.servlet.ServletException; |
| 42 |
|
import javax.servlet.http.HttpServletRequest; |
| 43 |
|
import javax.servlet.http.HttpServletResponse; |
| 44 |
|
import java.io.*; |
| 45 |
|
|
| 46 |
|
import org.apache.commons.logging.LogFactory; |
| 47 |
|
import org.apache.commons.logging.Log; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@author |
| 53 |
|
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 8 |
Complexity Density: 0,35 |
|
| 54 |
|
public class FileView extends BinaryView { |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private static Log log = LogFactory.getLog(FileView.class); |
| 58 |
|
|
| 59 |
|
private File file; |
| 60 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 61 |
0
|
public FileView(File file) {... |
| 62 |
0
|
this.file = file; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@return |
| 69 |
|
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
| 70 |
0
|
public String getContentType() {... |
| 71 |
0
|
String mimeType = null; |
| 72 |
0
|
ServletContext servletContext = getServletContext(); |
| 73 |
0
|
if (servletContext != null) { |
| 74 |
0
|
mimeType = servletContext.getMimeType(file.getName()); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
0
|
if (mimeType == null) { |
| 78 |
0
|
return FileUtils.getContentType(file.getName()); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
0
|
return mimeType; |
| 82 |
|
} |
| 83 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 84 |
0
|
public long getContentLength() {... |
| 85 |
0
|
return file.length(); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
@param |
| 92 |
|
@param |
| 93 |
|
@param |
| 94 |
|
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 3 |
Complexity Density: 0,21 |
|
| 95 |
0
|
public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {... |
| 96 |
0
|
response.setContentLength((int)getContentLength()); |
| 97 |
0
|
try { |
| 98 |
0
|
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); |
| 99 |
0
|
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); |
| 100 |
|
|
| 101 |
0
|
byte[] buf = new byte[1024]; |
| 102 |
0
|
int len = in.read(buf); |
| 103 |
0
|
while (len > 0) { |
| 104 |
0
|
out.write(buf, 0, len); |
| 105 |
0
|
len = in.read(buf); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
0
|
in.close(); |
| 109 |
0
|
out.close(); |
| 110 |
|
} catch (IOException ioe) { |
| 111 |
0
|
log.warn(ioe); |
| 112 |
|
} finally { |
| 113 |
0
|
AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY); |
| 114 |
0
|
blog.log(request, HttpServletResponse.SC_OK); |
| 115 |
|
} |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
} |