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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe, Tom Huybrechts
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 net.sf.json.JSONObject;
27 import org.kohsuke.stapler.DataBoundConstructor;
28 import org.kohsuke.stapler.StaplerRequest;
29 import hudson.Extension;
30 import hudson.FilePath;
31 import hudson.cli.CLICommand;
32 import org.apache.commons.fileupload.FileItem;
33
34 import java.io.IOException;
35 import java.io.File;
36 import javax.servlet.ServletException;
37
38 /**
39 * {@link ParameterDefinition} for doing file upload.
40 *
41 * <p>
42 * The file will be then placed into the workspace at the beginning of a build.
43 *
44 * @author Kohsuke Kawaguchi
45 */
46 public class FileParameterDefinition extends ParameterDefinition {
47 @DataBoundConstructor
48 public FileParameterDefinition(String name, String description) {
49 super(name, description);
50 }
51
52 public FileParameterValue createValue(StaplerRequest req, JSONObject jo) {
53 FileParameterValue p = req.bindJSON(FileParameterValue.class, jo);
54 p.setLocation(getName());
55 p.setDescription(getDescription());
56 return p;
57 }
58
59 @Extension
60 public static class DescriptorImpl extends ParameterDescriptor {
61 @Override
62 public String getDisplayName() {
63 return Messages.FileParameterDefinition_DisplayName();
64 }
65
66 @Override
67 public String getHelpFile() {
68 return "/help/parameter/file.html";
69 }
70 }
71
72 @Override
73 public ParameterValue createValue(StaplerRequest req) {
74 FileItem src;
75 try {
76 src = req.getFileItem( getName() );
77 } catch (ServletException e) {
78 // Not sure what to do here. We might want to raise this
79 // but that would involve changing the throws for this call
80 return null;
81 } catch (IOException e) {
82 // ditto above
83 return null;
84 }
85 if ( src == null ) {
86 // the requested file parameter wasn't uploaded
87 return null;
88 }
89 FileParameterValue p = new FileParameterValue(getName(), src);
90 p.setDescription(getDescription());
91 p.setLocation(getName());
92 return p;
93 }
94
95 @Override
96 public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException {
97 // capture the file to the server
98 FilePath src = new FilePath(command.checkChannel(),value);
99 File local = File.createTempFile("jenkins","parameter");
100 src.copyTo(new FilePath(local));
101
102 FileParameterValue p = new FileParameterValue(getName(), local, src.getName());
103 p.setDescription(getDescription());
104 p.setLocation(getName());
105 return p;
106 }
107 }
108