Skip to content

Commit

Permalink
[FIXED JENKINS-32566] Return a FileParameterDefinition when JSON requ…
Browse files Browse the repository at this point in the history
…est for a HTML rendered parameter contains a file key
  • Loading branch information
kinow committed Feb 5, 2016
1 parent aa4f3bf commit 10b9b24
Showing 1 changed file with 49 additions and 24 deletions.
73 changes: 49 additions & 24 deletions src/main/java/org/biouno/unochoice/AbstractUnoChoiceParameter.java
Expand Up @@ -24,24 +24,28 @@

package org.biouno.unochoice;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.util.Utils;
import org.kohsuke.stapler.StaplerRequest;

import hudson.DescriptorExtensionList;
import hudson.model.FileParameterValue;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.SimpleParameterDefinition;
import hudson.model.ParameterDefinition;
import hudson.model.StringParameterValue;

import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.util.Utils;
import org.kohsuke.stapler.StaplerRequest;

/**
* Abstract Uno Choice parameter. Provides basic methods common to all Uno Choice parameters.
*
Expand Down Expand Up @@ -144,23 +148,44 @@ public ParameterValue createValue(StaplerRequest request, JSONObject json) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.entering(AbstractUnoChoiceParameter.class.getName(), "createValue", new Object[] {request, json});
}
final JSONObject parameterJsonModel = new JSONObject(false);
final Object value = json.get("value");
final Object name = json.get("name");
final String valueAsText;

if (JSONUtils.isArray(value)) {
valueAsText = ((JSONArray) value).join(",", true);
if (json.containsKey("file")) {
// copied from FileParameterDefinition
FileItem src;
try {
src = request.getFileItem( json.getString("file") );
} catch (ServletException e) {
LOGGER.log(Level.SEVERE, "Fatal error while reading uploaded file: " + e.getMessage(), e);
return null;
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "IO error while reading uploaded file: " + e.getMessage(), e);
return null;
}
if ( src == null ) {
// the requested file parameter wasn't uploaded
return null;
}
FileParameterValue p = new FileParameterValue(getName(), src);
p.setDescription(getDescription());
return p;
} else {
valueAsText = (value == null) ? "" : String.valueOf(value);
final JSONObject parameterJsonModel = new JSONObject(false);
final Object value = json.get("value");
final Object name = json.get("name");
final String valueAsText;

if (JSONUtils.isArray(value)) {
valueAsText = ((JSONArray) value).join(",", true);
} else {
valueAsText = (value == null) ? "" : String.valueOf(value);
}

parameterJsonModel.put("name", name);
parameterJsonModel.put("value", valueAsText);

StringParameterValue parameterValue = request.bindJSON(StringParameterValue.class, parameterJsonModel);
parameterValue.setDescription(getDescription());
return parameterValue;
}

parameterJsonModel.put("name", name);
parameterJsonModel.put("value", valueAsText);

StringParameterValue parameterValue = request.bindJSON(StringParameterValue.class, parameterJsonModel);
parameterValue.setDescription(getDescription());
return parameterValue;
}

/**
Expand Down

0 comments on commit 10b9b24

Please sign in to comment.