Debian Codegraph @

src/main/java/net/sf/antcontrib/antserver/commands/SendFileCommand.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Place class description here.
28 *
29 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
30 * @author <additional author>
31 *
32 * @since
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 try
105 {
106 String realBasePath = project.getBaseDir().getCanonicalPath();
107 String realGetBasePath = file.getCanonicalPath();
108 if (! realGetBasePath.startsWith(realBasePath))
109 throw new SecurityException("Cannot access a file that is not rooted in the project execution directory");
110 }
111 catch (IOException e)
112 {
113 throw new BuildException(e);
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 ; // gulp;
167 }
168 }
169 return false;
170 }
171 }