Search in sources :

Example 16 with JSONArray

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();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) JSONArray(net.sf.json.JSONArray) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 17 with JSONArray

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(".");
}
Also used : JSONObject(net.sf.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) JSONObject(net.sf.json.JSONObject)

Example 18 with JSONArray

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;
}
Also used : BlueFavoriteContainer(io.jenkins.blueocean.rest.model.BlueFavoriteContainer) BlueFavorite(io.jenkins.blueocean.rest.model.BlueFavorite) User(hudson.model.User) UserImpl(io.jenkins.blueocean.service.embedded.rest.UserImpl) JSONArray(net.sf.json.JSONArray) Reachable(io.jenkins.blueocean.rest.Reachable) IOException(java.io.IOException)

Example 19 with JSONArray

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;
}
Also used : Resource(io.jenkins.blueocean.rest.model.Resource) JSONArray(net.sf.json.JSONArray) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) IOException(java.io.IOException)

Example 20 with JSONArray

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;
}
Also used : ParameterValue(hudson.model.ParameterValue) ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) IOException(java.io.IOException) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) JSONObject(net.sf.json.JSONObject) ParameterDefinition(hudson.model.ParameterDefinition)

Aggregations

JSONArray (net.sf.json.JSONArray)31 JSONObject (net.sf.json.JSONObject)19 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Map (java.util.Map)5 CobarAdapterDAO (com.alibaba.cobar.manager.dao.CobarAdapterDAO)4 Test (org.junit.Test)4 CommandStatus (com.alibaba.cobar.manager.dataobject.cobarnode.CommandStatus)3 ProcessorStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ProcessorStatus)3 SimpleChartData (com.sohu.cache.web.chart.model.SimpleChartData)3 ParameterDefinition (hudson.model.ParameterDefinition)3 ParameterValue (hudson.model.ParameterValue)3 OutputStream (java.io.OutputStream)3 CobarDO (com.alibaba.cobar.manager.dataobject.xml.CobarDO)2 Pair (com.alibaba.cobar.manager.util.Pair)2 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 Method (java.lang.reflect.Method)2 ParseException (java.text.ParseException)2 Iterator (java.util.Iterator)2