diff --git a/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java b/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java index a1d018b..c693e9c 100644 --- a/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java +++ b/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java @@ -36,6 +36,7 @@ import jenkins.model.Jenkins; import net.sf.json.JSONObject; import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadBase; import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.StaplerRequest; @@ -78,9 +79,18 @@ protected AbstractFileParameterDefinition(String name) { @Override public ParameterValue createValue(StaplerRequest req) { try { - FileItem src = req.getFileItem(getName()); + FileItem src; + try { + src = req.getFileItem(getName()); + } catch (ServletException x) { + if (x.getCause() instanceof FileUploadBase.InvalidContentTypeException) { + src = null; + } else { + throw x; + } + } if (src == null) { - throw new RuntimeException("No file was uploaded"); + return null; } AbstractFileParameterValue p; try (InputStream in = src.getInputStream()) { diff --git a/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java b/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java index b94092e..cd48523 100644 --- a/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java +++ b/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java @@ -48,6 +48,7 @@ import org.junit.Rule; import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.BuildWatcher; +import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.MockAuthorizationStrategy; @@ -95,4 +96,21 @@ public class AbstractFileParameterDefinitionTest { r.assertLogContains("received myfile.txt: dXBsb2FkZWQgY29udGVudCBoZXJl", b); } + @Issue("https://github.com/jenkinsci/file-parameters-plugin/issues/26") + @Test public void restMissingValue() throws Exception { + r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); + r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin")); + WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); + p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE"))); + p.setDefinition(new CpsFlowDefinition("echo(/received $env.FILE_FILENAME: $env.FILE/)", true)); + // Like: curl -u $auth $jenkins/job/myjob/buildWithParameters + WebRequest req = new WebRequest(new URL(r.getURL() + "job/myjob/buildWithParameters"), HttpMethod.POST); + User.getById("admin", true); // TODO workaround for https://github.com/jenkinsci/jenkins-test-harness/pull/273 + r.createWebClient().withBasicApiToken("admin").getPage(req); + r.waitUntilNoActivity(); + WorkflowRun b = p.getBuildByNumber(1); + assertNotNull(b); + r.assertLogContains("received null: null", b); + } + }