File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\FileParameterValue.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.EnvVars;
27 import hudson.FilePath;
28 import hudson.Launcher;
29 import hudson.tasks.BuildWrapper;
30 import hudson.util.VariableResolver;
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.io.UnsupportedEncodingException;
39 import javax.servlet.ServletException;
40
41 import org.apache.commons.fileupload.FileItem;
42 import org.apache.commons.fileupload.disk.DiskFileItem;
43 import org.apache.commons.io.FilenameUtils;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.kohsuke.stapler.DataBoundConstructor;
47 import org.kohsuke.stapler.StaplerRequest;
48 import org.kohsuke.stapler.StaplerResponse;
49
50 /**
51 * {@link ParameterValue} for {@link FileParameterDefinition}.
52 *
53 * <h2>Persistence</h2>
54 * <p>
55 * {@link DiskFileItem} is persistable via serialization,
56 * (although the data may get very large in XML) so this object
57 * as a whole is persistable.
58 *
59 * @author Kohsuke Kawaguchi
60 */
61 public class FileParameterValue extends ParameterValue {
62 private transient final FileItem file;
63
64 /**
65 * The name of the originally uploaded file.
66 */
67 private final String originalFileName;
68
69 private String location;
70
71 @DataBoundConstructor
72 public FileParameterValue(String name, FileItem file) {
73 this(name, file, FilenameUtils.getName(file.getName()));
74 }
75
76 public FileParameterValue(String name, File file, String originalFileName) {
77 this(name, new FileItemImpl(file), originalFileName);
78 }
79
80 protected FileParameterValue(String name, FileItem file, String originalFileName) {
81 super(name);
82 this.file = file;
83 this.originalFileName = originalFileName;
84 }
85
86 // post initialization hook
87 protected void setLocation(String location) {
88 this.location = location;
89 }
90
91 public String getLocation() {
92 return location;
93 }
94
95 /**
96 * Exposes the originalFileName as an environment variable.
97 */
98 @Override
99 public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {
100 env.put(name,originalFileName);
101 }
102
103 @Override
104 public VariableResolver<String> createVariableResolver(AbstractBuild<?, ?> build) {
105 return new VariableResolver<String>() {
106 public String resolve(String name) {
107 return FileParameterValue.this.name.equals(name) ? originalFileName : null;
108 }
109 };
110 }
111
112 /**
113 * Get the name of the originally uploaded file. If this
114 * {@link FileParameterValue} was created prior to 1.362, this method will
115 * return {@code null}.
116 *
117 * @return the name of the originally uploaded file
118 */
119 public String getOriginalFileName() {
120 return originalFileName;
121 }
122
123 public FileItem getFile() {
124 return file;
125 }
126
127 @Override
128 public BuildWrapper createBuildWrapper(AbstractBuild<?,?> build) {
129 return new BuildWrapper() {
130 @Override
131 public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
132 if (!StringUtils.isEmpty(file.getName())) {
133 listener.getLogger().println("Copying file to "+location);
134 FilePath locationFilePath = build.getWorkspace().child(location);
135 locationFilePath.getParent().mkdirs();
136 locationFilePath.copyFrom(file);
137 locationFilePath.copyTo(new FilePath(getLocationUnderBuild(build)));
138 }
139 return new Environment() {};
140 }
141 };
142 }
143
144 @Override
145 public int hashCode() {
146 final int prime = 31;
147 int result = super.hashCode();
148 result = prime * result
149 + ((location == null) ? 0 : location.hashCode());
150 return result;
151 }
152
153 /**
154 * In practice this will always be false, since location should be unique.
155 */
156 @Override
157 public boolean equals(Object obj) {
158 if (this == obj)
159 return true;
160 if (!super.equals(obj))
161 return false;
162 if (getClass() != obj.getClass())
163 return false;
164 FileParameterValue other = (FileParameterValue) obj;
165 if (location == null) {
166 if (other.location != null)
167 return false;
168 } else if (!location.equals(other.location))
169 return false;
170 return true;
171 }
172
173 @Override
174 public String getShortDescription() {
175 return "(FileParameterValue) " + getName() + "='" + originalFileName + "'";
176 }
177
178 /**
179 * Serve this file parameter in response to a {@link StaplerRequest}.
180 *
181 * @param request
182 * @param response
183 * @throws ServletException
184 * @throws IOException
185 */
186 public void doDynamic(StaplerRequest request, StaplerResponse response) throws ServletException, IOException {
187 if (("/" + originalFileName).equals(request.getRestOfPath())) {
188 AbstractBuild build = (AbstractBuild)request.findAncestor(AbstractBuild.class).getObject();
189 File fileParameter = getLocationUnderBuild(build);
190 if (fileParameter.isFile()) {
191 InputStream data = new FileInputStream(fileParameter);
192 long lastModified = fileParameter.lastModified();
193 long contentLength = fileParameter.length();
194 if (request.hasParameter("view")) {
195 response.serveFile(request, data, lastModified, contentLength, "plain.txt");
196 } else {
197 response.serveFile(request, data, lastModified, contentLength, originalFileName);
198 }
199 }
200 }
201 }
202
203 /**
204 * Get the location under the build directory to store the file parameter.
205 *
206 * @param build the build
207 * @return the location to store the file parameter
208 */
209 private File getLocationUnderBuild(AbstractBuild build) {
210 return new File(build.getRootDir(), "fileParameters/" + location);
211 }
212
213 /**
214 * Default implementation from {@link File}.
215 */
216 public static final class FileItemImpl implements FileItem {
217 private final File file;
218
219 public FileItemImpl(File file) {
220 if (file == null) {
221 throw new NullPointerException("file");
222 }
223 this.file = file;
224 }
225
226 public InputStream getInputStream() throws IOException {
227 return new FileInputStream(file);
228 }
229
230 public String getContentType() {
231 return null;
232 }
233
234 public String getName() {
235 return file.getName();
236 }
237
238 public boolean isInMemory() {
239 return false;
240 }
241
242 public long getSize() {
243 return file.length();
244 }
245
246 public byte[] get() {
247 try {
248 return IOUtils.toByteArray(new FileInputStream(file));
249 } catch (IOException e) {
250 throw new Error(e);
251 }
252 }
253
254 public String getString(String encoding) throws UnsupportedEncodingException {
255 return new String(get(), encoding);
256 }
257
258 public String getString() {
259 return new String(get());
260 }
261
262 public void write(File to) throws Exception {
263 new FilePath(file).copyTo(new FilePath(to));
264 }
265
266 public void delete() {
267 file.delete();
268 }
269
270 public String getFieldName() {
271 return null;
272 }
273
274 public void setFieldName(String name) {
275 }
276
277 public boolean isFormField() {
278 return false;
279 }
280
281 public void setFormField(boolean state) {
282 }
283
284 public OutputStream getOutputStream() throws IOException {
285 return new FileOutputStream(file);
286 }
287 }
288 }
289