use of net.sf.json.JSONArray in project camel by apache.
the class XmlJsonDataFormatTest method testXmlArraysToJson.
@Test
public void testXmlArraysToJson() throws Exception {
MockEndpoint mockJSON = getMockEndpoint("mock:jsonInlineOptionsArray");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
Object json = template.requestBody("direct:marshalInlineOptionsArray", "<ar><el>1</el><el>2</el><el>3</el><el>4</el></ar>");
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONArray array = (JSONArray) JSONSerializer.toJSON(jsonString);
assertTrue("Expected a JSON array with string elements: 1, 2, 3, 4", array.containsAll(Arrays.asList("1", "2", "3", "4")));
mockJSON.assertIsSatisfied();
}
use of net.sf.json.JSONArray in project hudson-2.x by hudson.
the class ParametersDefinitionProperty method _doBuild.
/**
* Interprets the form submission and schedules a build for a parameterized job.
*
* <p>
* This method is supposed to be invoked from {@link AbstractProject#doBuild(StaplerRequest, StaplerResponse)}.
*/
public void _doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
if (!req.getMethod().equals("POST")) {
// show the parameter entry form.
req.getView(this, "index.jelly").forward(req, rsp);
return;
}
List<ParameterValue> values = new ArrayList<ParameterValue>();
JSONObject formData = req.getSubmittedForm();
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
JSONObject jo = (JSONObject) o;
String name = jo.getString("name");
ParameterDefinition d = getParameterDefinition(name);
if (d == null)
throw new IllegalArgumentException("No such parameter definition: " + name);
ParameterValue parameterValue = d.createValue(req, jo);
values.add(parameterValue);
}
Hudson.getInstance().getQueue().schedule(owner, owner.getDelay(req), new ParametersAction(values), new CauseAction(new Cause.UserCause()));
// send the user back to the job top page.
rsp.sendRedirect(".");
}
use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.
the class FavoritesStatePreloader method getFetchData.
@Override
protected FetchData getFetchData(@Nonnull BlueUrlTokenizer blueUrl) {
User jenkinsUser = User.current();
if (jenkinsUser != null) {
UserImpl blueUser = new UserImpl(jenkinsUser);
BlueFavoriteContainer favoritesContainer = blueUser.getFavorites();
if (favoritesContainer != null) {
JSONArray favorites = new JSONArray();
Iterator<BlueFavorite> favoritesIterator = favoritesContainer.iterator();
while (favoritesIterator.hasNext()) {
Reachable favorite = favoritesIterator.next();
try {
favorites.add(JSONObject.fromObject(ModelObjectSerializer.toJson(favorite)));
} catch (IOException e) {
LOGGER.log(Level.FINE, String.format("Unable to preload favorites for User '%s'. Serialization error.", jenkinsUser.getFullName()), e);
return null;
}
}
return new FetchData(favoritesContainer.getLink().getHref(), favorites.toString());
}
}
// Don't preload any data on the page.
return null;
}
use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.
the class PipelineActivityStatePreloader method getFetchData.
@Override
protected FetchData getFetchData(@Nonnull BlueUrlTokenizer blueUrl) {
BluePipeline pipeline = getPipeline(blueUrl);
if (pipeline != null) {
// It's a pipeline page. Let's prefetch the pipeline activity and add them to the page,
// saving the frontend the overhead of requesting them.
Container<Resource> activitiesContainer = pipeline.getActivities();
Iterator<Resource> activitiesIterator = activitiesContainer.iterator(0, DEFAULT_LIMIT);
JSONArray activities = new JSONArray();
while (activitiesIterator.hasNext()) {
Resource blueActivity = activitiesIterator.next();
try {
activities.add(JSONObject.fromObject(ModelObjectSerializer.toJson(blueActivity)));
} catch (IOException e) {
LOGGER.log(Level.FINE, String.format("Unable to preload runs for Job '%s'. Activity serialization error.", pipeline.getFullName()), e);
return null;
}
}
return new FetchData(activitiesContainer.getLink().getHref() + "?start=0&limit=" + DEFAULT_LIMIT, activities.toString());
}
// Don't preload any data on the page.
return null;
}
use of net.sf.json.JSONArray in project blueocean-plugin by jenkinsci.
the class RunContainerImpl method getParameterValue.
private List<ParameterValue> getParameterValue(@Nonnull StaplerRequest request) {
List<ParameterValue> values = new ArrayList<>();
List<ParameterDefinition> pdsInRequest = new ArrayList<>();
ParametersDefinitionProperty pp = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
if (pp == null) {
return values;
}
try {
JSONObject body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
if (body.get("parameters") == null && pp.getParameterDefinitions().size() > 0) {
throw new ServiceException.BadRequestExpception("This is parameterized job, requires parameters");
}
if (body.get("parameters") != null) {
JSONArray pds = JSONArray.fromObject(body.get("parameters"));
for (Object o : pds) {
JSONObject p = (JSONObject) o;
String name = (String) p.get("name");
if (name == null) {
throw new ServiceException.BadRequestExpception("parameters.name is required element");
}
ParameterDefinition pd = pp.getParameterDefinition(name);
if (pd == null) {
throw new ServiceException.BadRequestExpception("No such parameter definition: " + name);
}
ParameterValue parameterValue = pd.createValue(request, p);
if (parameterValue != null) {
values.add(parameterValue);
pdsInRequest.add(pd);
} else {
throw new ServiceException.BadRequestExpception("Invalid value. Cannot retrieve the parameter value: " + name);
}
}
//now check for missing parameters without default values
if (pdsInRequest.size() != pp.getParameterDefinitions().size()) {
for (ParameterDefinition pd : pp.getParameterDefinitions()) {
if (!pdsInRequest.contains(pd)) {
ParameterValue v = pd.getDefaultParameterValue();
if (v == null || v.getValue() == null) {
throw new ServiceException.BadRequestExpception("Missing parameter: " + pd.getName());
}
values.add(v);
}
}
}
}
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
}
return values;
}
Aggregations