Example usage for org.json.simple JSONArray addAll

List of usage examples for org.json.simple JSONArray addAll

Introduction

In this page you can find the example usage for org.json.simple JSONArray addAll.

Prototype

public boolean addAll(Collection<? extends E> c) 

Source Link

Document

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

Usage

From source file:com.opensoc.tagging.TelemetryTaggerBolt.java

@SuppressWarnings("unchecked")
public void execute(Tuple tuple) {

    LOG.trace("[OpenSOC] Starting to process message for alerts");
    JSONObject original_message = null;//from w  w  w . j a va  2  s.  c  o m

    try {

        original_message = (JSONObject) tuple.getValue(0);

        if (original_message == null || original_message.isEmpty())
            throw new Exception("Could not parse message from byte stream");

        LOG.trace("[OpenSOC] Received tuple: " + original_message);

        JSONObject alerts_tag = new JSONObject();
        JSONArray alerts_list = _adapter.tag(original_message);

        LOG.trace("[OpenSOC] Tagged message: " + alerts_list);

        if (alerts_list.size() != 0) {
            if (original_message.containsKey("alerts")) {
                JSONObject tag = (JSONObject) original_message.get("alerts");
                JSONArray already_triggered = (JSONArray) tag.get("triggered");
                alerts_list.addAll(already_triggered);
                LOG.trace("[OpenSOC] Created a new string of alerts");
            }

            alerts_tag.put("identifier", _identifier);
            alerts_tag.put("triggered", alerts_list);
            original_message.put("alerts", alerts_tag);

            LOG.debug("[OpenSOC] Detected alerts: " + alerts_tag);
        } else {
            LOG.debug("[OpenSOC] The following messages did not contain alerts: " + original_message);
        }

        _collector.ack(tuple);
        _collector.emit(new Values(original_message));

        /*if (metricConfiguration != null) {
           emitCounter.inc();
           ackCounter.inc();
        }*/

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error("Failed to tag message :" + original_message);
        e.printStackTrace();
        _collector.fail(tuple);

        /*
        if (metricConfiguration != null) {
           failCounter.inc();
        }*/
    }
}

From source file:edu.vt.vbi.patric.portlets.TranscriptomicsGene.java

public JSONObject doCLustering(String filename, String outputfilename, String g, String e, String m, String ge)
        throws IOException {

    boolean remove = true;
    JSONObject output = new JSONObject();

    String exec = "runMicroArrayClustering.sh " + filename + " " + outputfilename + " "
            + ((g.equals("1")) ? ge : "0") + " " + ((e.equals("1")) ? ge : "0") + " " + m;

    LOGGER.debug(exec);//from   ww w.jav a2s  . com

    CommandResults callClustering = ExecUtilities.exec(exec);

    if (callClustering.getStdout()[0].equals("done")) {

        BufferedReader in = new BufferedReader(new FileReader(outputfilename + ".cdt"));
        String strLine;
        int count = 0;
        JSONArray rows = new JSONArray();
        while ((strLine = in.readLine()) != null) {
            String[] tabs = strLine.split("\t");
            if (count == 0) {
                JSONArray columns = new JSONArray();
                // copy from 4th column to all
                columns.addAll(Arrays.asList(tabs).subList(4, tabs.length));
                output.put("columns", columns);
            }
            if (count >= 3) {
                rows.add(tabs[1]);
            }
            count++;
        }
        in.close();
        output.put("rows", rows);
    }

    if (remove) {

        exec = "rm " + filename + " " + outputfilename;

        callClustering = ExecUtilities.exec(exec);
        LOGGER.debug("{}", callClustering);
    }

    return output;
}

From source file:fr.treeptik.cloudunit.docker.DockerContainerJSON.java

public DockerContainer create(DockerContainer dockerContainer, String hostIp) throws DockerJSONException {
    URI uri = null;//from   ww  w  .j  a  va2 s .  c o  m
    try {
        uri = new URIBuilder().setScheme(dockerEndpointMode).setHost(hostIp).setPath("/containers/create")
                .setParameter("name", dockerContainer.getName()).build();

        JSONObject config = new JSONObject();
        config.put("AttachStdin", Boolean.FALSE);
        config.put("AttachStdout", Boolean.TRUE);
        config.put("AttachStderr", Boolean.TRUE);
        config.put("Memory", dockerContainer.getMemory());
        config.put("MemorySwap", dockerContainer.getMemorySwap());
        config.put("Image", dockerContainer.getImage());
        config.put("ExposedPorts", dockerContainer.getPorts());

        try {
            JSONObject hostConfig = new JSONObject();
            JSONArray listVolumesFrom = new JSONArray();
            if (dockerContainer.getVolumesFrom() != null) {
                for (int i = 0, iMax = dockerContainer.getVolumesFrom().size(); i < iMax; i++) {
                    listVolumesFrom.add(dockerContainer.getVolumesFrom().get(i));
                }
                hostConfig.put("VolumesFrom", listVolumesFrom);
                config.put("HostConfig", hostConfig);
            }
        } catch (Exception ex) {
            logger.error("" + dockerContainer.getVolumesFrom(), ex);
        }

        JSONArray listCmd = new JSONArray();
        listCmd.addAll(dockerContainer.getCmd());
        config.put("Cmd", listCmd);
        int statusCode = client.sendPost(uri, config.toJSONString(), "application/json");
        switch (statusCode) {
        case 404:
            throw new ErrorDockerJSONException("Image or container not found");
        case 406:
            throw new ErrorDockerJSONException("impossible to attach (container not running)");
        case 500:
            throw new ErrorDockerJSONException("server error");
        }

    } catch (URISyntaxException | IOException e) {
        StringBuilder msgError = new StringBuilder(256);
        msgError.append(dockerContainer).append(",hostIP=").append(hostIp).append(",uri=").append(uri);
        logger.error("" + msgError, e);
        throw new FatalDockerJSONException("docker : error fatal");
    }

    return dockerContainer;
}

From source file:fr.treeptik.cloudunit.docker.DockerContainerJSON.java

public DockerContainer start(DockerContainer dockerContainer, String hostIp) throws DockerJSONException {
    URI uri = null;//w ww  .jav a 2s  . c  om
    try {
        uri = new URIBuilder().setScheme(dockerEndpointMode).setHost(hostIp)
                .setPath("/containers/" + dockerContainer.getName() + "/start").build();
        JSONObject config = new JSONObject();

        config.put("Privileged", Boolean.FALSE);
        config.put("PublishAllPorts", Boolean.TRUE);

        JSONArray link = new JSONArray();
        if (dockerContainer.getLinks() != null) {
            link.addAll(dockerContainer.getLinks());
            config.put("Links", link);
        }

        if (dockerContainer.getVolumesFrom() != null) {
            JSONArray listVolumesFrom = new JSONArray();
            if (dockerContainer.getVolumesFrom() != null) {
                for (int i = 0, iMax = dockerContainer.getVolumesFrom().size(); i < iMax; i++) {
                    listVolumesFrom.add(dockerContainer.getVolumesFrom().get(i));
                }
            }
            config.put("VolumesFrom", listVolumesFrom);
        }

        if (dockerContainer.getPortsToOpen() != null) {
            JSONObject portsBinding = new JSONObject();
            dockerContainer.getPortsToOpen().stream().map(t -> t.toString() + "/tcp").forEach(
                    t -> portsBinding.put(t, Arrays.asList((new JSONObject(new HashMap<String, String>() {

                        private static final long serialVersionUID = 1L;

                        {
                            put("HostPort", portUtils.getARandomHostPorts(hostIp).toString());
                            put("HostIp", "0.0.0.0");
                        }
                    })))));

            config.put("PortBindings", portsBinding);
        }

        // ajout des volumes provenant de l'hote
        final List volumes = new ArrayList<String>() {
            {
                add("/etc/localtime:/etc/localtime:ro");
                add("/etc/timezone:/etc/timezone:ro");
            }
        };

        BooleanSupplier isRunningIntoKVM = () -> "true".equalsIgnoreCase(System.getenv().get("CU_KVM"));
        if (isRunningIntoKVM.getAsBoolean()) {
            volumes.add("/dev/urandom:/dev/urandom");
        }

        Predicate<String> isContainerGit = s -> s.contains("-git-");
        if (isContainerGit.test(dockerContainer.getName())) {
            volumes.add("/var/log/cloudunit/git/auth-" + dockerContainer.getId() + ":/var/log/cloudunit");
        }

        config.put("Binds", volumes);

        /**
         * Gestion du binding de port
         */
        JSONObject portBindsConfigJSONFinal = new JSONObject();
        if (dockerContainer.getPortBindings() != null) {
            /**
             * pour chaque ports  Binder (ex: 53/udp) on rcupre le
             * tableau avec les deux maps ex :- map1 = HostIp , 172.17.42.1
             * - map2 = HostPort , 53
             */
            for (Map.Entry<String, Map<String, String>[]> portKey : dockerContainer.getPortBindings()
                    .entrySet()) {

                logger.info("port/protocol to configure : " + portKey.getKey());

                // On convertie le tableau en list pour itrer dessus
                List<Map<String, String>> listOfMapsConfig = Arrays.asList(portKey.getValue());

                JSONObject portConfigJSON = new JSONObject();
                for (Map<String, String> portConfigMap : listOfMapsConfig) {
                    JSONArray portConfigJSONArray = new JSONArray();

                    // transfert HostIP and HostPort avec leur valeurs dans
                    // un JSONArray
                    for (Entry<String, String> hostBindingMap : portConfigMap.entrySet()) {
                        logger.info(hostBindingMap.getKey() + " : " + hostBindingMap.getValue());

                        portConfigJSON.put(hostBindingMap.getKey(), hostBindingMap.getValue());
                        portConfigJSONArray.add(portConfigJSON);
                    }
                    portBindsConfigJSONFinal.put(portKey.getKey(), portConfigJSONArray);
                    config.put("PortBindings", portBindsConfigJSONFinal);
                }
            }
        }

        int statusCode = client.sendPostForStart(uri, config.toJSONString(), "application/json");

        switch (statusCode) {
        case 304:
            throw new WarningDockerJSONException("container already started");
        case 404:
            throw new ErrorDockerJSONException("docker : no such container");
        case 500:
            throw new ErrorDockerJSONException("docker : error server");
        }
        dockerContainer = this.findOne(dockerContainer.getName(), hostIp);
    } catch (URISyntaxException | IOException e) {
        StringBuilder msgError = new StringBuilder(256);
        msgError.append(dockerContainer).append(",hostIP=").append(hostIp).append(",uri=").append(uri);
        logger.error("" + msgError, e);
        throw new FatalDockerJSONException("docker : error fatal");
    }
    return dockerContainer;

}

From source file:hoot.services.controllers.ingest.BasemapResource.java

@GET
@Path("/getlist")
@Produces(MediaType.TEXT_PLAIN)/*from w  w  w  .j a v a 2  s . c om*/
public Response getBasemapList() {
    JSONArray retList = new JSONArray();
    Map<String, JSONObject> sortedScripts = new TreeMap<String, JSONObject>();
    JSONArray filesList = new JSONArray();

    try {
        filesList = _getBasemapList();
    } catch (Exception ex) {
        ResourceErrorHandler.handleError("Error getting base map list: " + ex.getMessage(),
                Status.INTERNAL_SERVER_ERROR, log);
    }

    // sort the list
    for (Object o : filesList) {
        JSONObject cO = (JSONObject) o;
        String sName = cO.get("name").toString();
        sortedScripts.put(sName.toUpperCase(), cO);
    }

    retList.addAll(sortedScripts.values());

    return Response.ok(retList.toString(), MediaType.TEXT_PLAIN).build();
}

From source file:hoot.services.controllers.info.ReportsResource.java

protected JSONArray _getReportsList() throws Exception {
    JSONArray res = new JSONArray();
    // sort by name
    Map<String, JSONObject> sorted = new TreeMap<String, JSONObject>();

    String storePath = _homeFolder + "/" + _rptStorePath;
    File f = new File(storePath);
    if (f.exists()) {
        List<File> files = (List<File>) FileUtils.listFilesAndDirs(f,
                new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY);
        for (File file : files) {
            try {
                if (file.isDirectory()) {
                    String id = file.getName();
                    String absPath = file.getAbsolutePath();
                    if (!absPath.equals(storePath)) {
                        JSONObject meta = _getMetaData(id);
                        meta.put("id", id);
                        sorted.put(meta.get("name").toString(), meta);
                    }/*  ww  w.j  a v  a 2s.com*/
                }
            } catch (Exception ee) {
                // we ignore and continue
                log.error(ee.getMessage());
            }
        }
    }

    res.addAll(sorted.values());

    return res;
}

From source file:eu.riscoss.rdc.RDCGithub.java

/**
 * For paginated requests/*from  w  w  w.  jav a2s  . c o m*/
 * @param request
 * @param maxPages max pages in paginated requests
 * @param created_at_years maximum timespan for the "created at" field (used e.g. for issues). 0: no timespan
 * @return
 */
private JSONAware parsePaged(String request, int maxPages, int created_at_years) {

    JSONArray jaComplete = new JSONArray();

    char divider = '?';
    if (request.contains("?"))
        divider = '&';

    Calendar lastyear = Calendar.getInstance();//actual
    lastyear.set(Calendar.YEAR, lastyear.get(Calendar.YEAR) - created_at_years);

    try {
        for (int i = 1; i <= maxPages; i++) {

            String jsonPage = getData(repository + request + divider + "page=" + i, "");

            if (jsonPage.startsWith("WARNING")) {
                System.err.println(jsonPage); //error message - implement different handling if needed
            } else
                try {
                    JSONAware jv = (JSONAware) new JSONParser().parse(jsonPage);
                    if (jv instanceof JSONArray) {
                        JSONArray ja = (JSONArray) jv;
                        if (ja.size() == 0)
                            break;
                        jaComplete.addAll(ja);
                        //do not scan more years
                        if (created_at_years > 0) {
                            Calendar openedDate;
                            String openedAt = (String) ((JSONObject) ja.get(ja.size() - 1)).get("created_at");
                            if (openedAt != null) {
                                openedDate = DatatypeConverter.parseDateTime(openedAt);
                                //System.out.println("scan: opening date: "+openedDate.get(Calendar.YEAR)+" "+openedDate.get(Calendar.MONTH));
                                //System.out.println("scan: last    date: "+lastyear.get(Calendar.YEAR)+" "+lastyear.get(Calendar.MONTH));

                                if (openedDate.compareTo(lastyear) < 0) {
                                    System.out.println("BREAK");
                                    break;
                                }
                            }
                        }

                    }
                } catch (ParseException e) {
                    e.printStackTrace();//TODO
                }
        }

    } catch (org.apache.http.ParseException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return jaComplete;
}

From source file:com.opensoc.alerts.TelemetryAlertsBolt.java

@SuppressWarnings("unchecked")
public void execute(Tuple tuple) {

    LOG.trace("[OpenSOC] Starting to process message for alerts");
    JSONObject original_message = null;//  w  w  w. j a  v a  2s .  c  o  m
    String key = null;

    try {

        key = tuple.getStringByField("key");
        original_message = (JSONObject) tuple.getValueByField("message");

        if (original_message == null || original_message.isEmpty())
            throw new Exception("Could not parse message from byte stream");

        if (key == null)
            throw new Exception("Key is not valid");

        LOG.trace("[OpenSOC] Received tuple: " + original_message);

        JSONObject alerts_tag = new JSONObject();
        Map<String, JSONObject> alerts_list = _adapter.alert(original_message);
        JSONArray uuid_list = new JSONArray();

        if (alerts_list == null || alerts_list.isEmpty()) {
            System.out.println("[OpenSOC] No alerts detected in: " + original_message);
            _collector.ack(tuple);
            _collector.emit("message", new Values(key, original_message));
        } else {
            for (String alert : alerts_list.keySet()) {
                uuid_list.add(alert);

                LOG.trace("[OpenSOC] Checking alerts cache: " + alert);

                if (cache.getIfPresent(alert) == null) {
                    System.out.println("[OpenSOC]: Alert not found in cache: " + alert);

                    JSONObject global_alert = new JSONObject();
                    global_alert.putAll(_identifier);
                    global_alert.putAll(alerts_list.get(alert));
                    global_alert.put("timestamp", System.currentTimeMillis());
                    _collector.emit("alert", new Values(global_alert));

                    cache.put(alert, "");

                } else
                    LOG.trace("Alert located in cache: " + alert);

                LOG.debug("[OpenSOC] Alerts are: " + alerts_list);

                if (original_message.containsKey("alerts")) {
                    JSONArray already_triggered = (JSONArray) original_message.get("alerts");

                    uuid_list.addAll(already_triggered);
                    LOG.trace("[OpenSOC] Messages already had alerts...tagging more");
                }

                original_message.put("alerts", uuid_list);

                LOG.debug("[OpenSOC] Detected alerts: " + alerts_tag);

                _collector.ack(tuple);
                _collector.emit("message", new Values(key, original_message));

            }

            /*
             * if (metricConfiguration != null) { emitCounter.inc();
             * ackCounter.inc(); }
             */
        }

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error("Failed to tag message :" + original_message);
        e.printStackTrace();
        _collector.fail(tuple);

        /*
         * if (metricConfiguration != null) { failCounter.inc(); }
         */

        JSONObject error = ErrorGenerator.generateErrorMessage("Alerts problem: " + original_message, e);
        _collector.emit("error", new Values(error));
    }
}

From source file:hoot.services.controllers.ingest.CustomScriptResource.java

/**
 * <NAME>Custom Script Service Get Scripts List</NAME>
 * <DESCRIPTION>Gets the list of available scripts.</DESCRIPTION>
 * <PARAMETERS>/*from w  ww.j  ava 2  s . co m*/
 * </PARAMETERS>
 * <OUTPUT>
 *    JSON Array containing JSON of name and description of all available scripts
 * </OUTPUT>
 * <EXAMPLE>
 *    <URL>http://localhost:8080/hoot-services/ingest/customscript/getlist</URL>
 *    <REQUEST_TYPE>GET</REQUEST_TYPE>
 *    <INPUT>
 * </INPUT>
 * <OUTPUT>[{"NAME":"MyTest","DESCRIPTION":"my description"}]</OUTPUT>
 * </EXAMPLE>
* @return
*/
@GET
@Path("/getlist")
@Produces(MediaType.TEXT_PLAIN)
public Response getScriptsList() {

    JSONArray retList = new JSONArray();
    Map<String, JSONObject> sortedScripts = new TreeMap<String, JSONObject>();
    JSONArray filesList = new JSONArray();

    try {
        File scriptsDir = new File(scriptFolder);
        if (scriptsDir.exists()) {
            String[] exts = new String[1];
            exts[0] = "js";
            List<File> files = (List<File>) FileUtils.listFiles(scriptsDir, exts, false);

            for (int i = 0; i < files.size(); i++) {
                File f = files.get(i);
                String content = FileUtils.readFileToString(f, "UTF-8");
                JSONObject oScript = getScriptObject(content);

                if (oScript != null) {
                    JSONObject header = (JSONObject) oScript.get("HEADER");
                    if (header.get("CANEXPORT") == null) {
                        boolean canExport = validateExport(oScript.get("BODY").toString());
                        header.put("CANEXPORT", canExport);
                    }
                    filesList.add(header);
                }
            }
        }
        filesList.addAll(_getDefaultList());

        // sort the list
        for (Object o : filesList) {
            JSONObject cO = (JSONObject) o;
            String sName = cO.get("NAME").toString();
            sortedScripts.put(sName.toUpperCase(), cO);
        }

        retList.addAll(sortedScripts.values());

    } catch (Exception ex) {
        ResourceErrorHandler.handleError("Error getting scripts list: " + ex.getMessage(),
                Status.INTERNAL_SERVER_ERROR, log);
    }

    return Response.ok(retList.toString(), MediaType.TEXT_PLAIN).build();
}

From source file:nl.b3p.geotools.data.arcgis.ArcGISFeatureReader.java

private JSONArray getJSONFeaturesDirect() throws IOException {
    initBeforeRequest();/* ww  w .  j a  v  a  2 s . c  o  m*/

    Map<String, String> params = new HashMap<String, String>();

    try {
        params.putAll(createQueryParams());
    } catch (FilterToSQLException ex) {
        throw new IOException(ex);
    }

    params.put("f", "json");
    params.put("outFields", getOutFields());
    params.put("returnGeometry", returnGeometry + "");

    try {
        JSONObject response = fs.getArcGISDataStore().getServerJSONResponse(typeName + "/query", params);

        batch = (JSONArray) response.get("features");

        int originalSize = batch.size();

        // XXX do not copy when startIndex = 0
        // XXX do only one subList() call for startIndex and maxFeatures

        if (query.getStartIndex() != null) {
            JSONArray subBatch = new JSONArray();
            subBatch.addAll(batch.subList(Math.min(query.getStartIndex(), batch.size()), batch.size()));
            batch = subBatch;
        }
        if (batch.size() > query.getMaxFeatures()) {
            JSONArray subBatch = new JSONArray();
            subBatch.addAll(batch.subList(0, query.getMaxFeatures()));
            batch = subBatch;
        }

        if (log.isDebugEnabled()) {
            log.debug(String.format(
                    "Features received for layer %s: %d; when adjusted for startIndex %s and maxFeatures %s the count is %d",
                    typeName, originalSize, query.getStartIndex() + "",
                    query.isMaxFeaturesUnlimited() ? "unlimited" : query.getMaxFeatures() + "", batch.size()));
        }
        return batch;
    } catch (Exception e) {
        throw new IOException("Error retrieving features from ArcGIS: " + e.toString(), e);
    }
}