| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.antserver.commands; |
| 17 |
|
| 18 |
import java.io.*; |
| 19 |
|
| 20 |
import org.apache.tools.ant.BuildException; |
| 21 |
import org.apache.tools.ant.Project; |
| 22 |
|
| 23 |
import net.sf.antcontrib.antserver.Command; |
| 24 |
import net.sf.antcontrib.antserver.Util; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SendFileCommand |
| 38 |
extends AbstractCommand |
| 39 |
implements Command |
| 40 |
{ |
| 41 |
private long contentLength; |
| 42 |
private String todir; |
| 43 |
private String tofile; |
| 44 |
private String fileBaseName; |
| 45 |
private transient File file; |
| 46 |
|
| 47 |
public File getFile() |
| 48 |
{ |
| 49 |
return file; |
| 50 |
} |
| 51 |
|
| 52 |
public long getContentLength() |
| 53 |
{ |
| 54 |
return contentLength; |
| 55 |
} |
| 56 |
|
| 57 |
public InputStream getContentStream() |
| 58 |
throws IOException |
| 59 |
{ |
| 60 |
return new FileInputStream(file); |
| 61 |
} |
| 62 |
|
| 63 |
public void setFile(File file) |
| 64 |
{ |
| 65 |
this.file = file; |
| 66 |
this.fileBaseName = file.getName(); |
| 67 |
this.contentLength = file.length(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public String getTofile() |
| 72 |
{ |
| 73 |
return tofile; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
public void setTofile(String tofile) |
| 78 |
{ |
| 79 |
this.tofile = tofile; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public String getTodir() |
| 84 |
{ |
| 85 |
return todir; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public void setTodir(String todir) |
| 90 |
{ |
| 91 |
this.todir = todir; |
| 92 |
} |
| 93 |
|
| 94 |
public void validate(Project project) |
| 95 |
{ |
| 96 |
if (file == null) |
| 97 |
throw new BuildException("Missing required attribute 'file'"); |
| 98 |
|
| 99 |
if (tofile == null && todir == null) |
| 100 |
throw new BuildException("Missing both attributes 'tofile' and 'todir'" |
| 101 |
+ " at least one must be supplied"); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
public boolean execute(Project project, |
| 121 |
long contentLength, |
| 122 |
InputStream content) |
| 123 |
throws Throwable |
| 124 |
{ |
| 125 |
File dest = null; |
| 126 |
|
| 127 |
if (tofile != null) |
| 128 |
{ |
| 129 |
dest = new File(project.getBaseDir(), tofile); |
| 130 |
if (! new File(tofile).getCanonicalPath().startsWith(project.getBaseDir().getCanonicalPath())) { |
| 131 |
System.out.println("throwing an exception"); |
| 132 |
throw new SecurityException("The requested filename must be a relative path."); |
| 133 |
} |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
dest = new File(project.getBaseDir(), todir); |
| 138 |
dest = new File(dest, fileBaseName); |
| 139 |
|
| 140 |
if (! new File(todir, tofile).getCanonicalPath().startsWith(project.getBaseDir().getCanonicalPath())) { |
| 141 |
throw new SecurityException("The requested filename must be a relative path."); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
FileOutputStream fos = null; |
| 147 |
|
| 148 |
try |
| 149 |
{ |
| 150 |
fos = new FileOutputStream(dest); |
| 151 |
|
| 152 |
Util.transferBytes(content, |
| 153 |
contentLength, |
| 154 |
fos, |
| 155 |
false); |
| 156 |
} |
| 157 |
finally |
| 158 |
{ |
| 159 |
try |
| 160 |
{ |
| 161 |
if (fos != null) |
| 162 |
fos.close(); |
| 163 |
} |
| 164 |
catch (IOException e) |
| 165 |
{ |
| 166 |
; |
| 167 |
} |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |