Search in sources :

Example 56 with JSONParser

use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method initSpeciesListAdditionalColumns.

private static Map<String, Map<String, List<String>>> initSpeciesListAdditionalColumns() {
    Map<String, Map<String, List<String>>> map = new HashMap<String, Map<String, List<String>>>();
    String slac = settings.getProperty("species.list.additional.columns", "");
    //append dynamic columns
    slac += dynamicSpeciesListColumns();
    String[] columns = slac.split("\\|");
    for (String line : columns) {
        String[] parts = line.split(",");
        if (parts[0].equals("Conservation")) {
            speciesListThreatened = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts[0].equals("Invasive")) {
            speciesListInvasive = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts.length > 1) {
            String columnTitle = parts[0];
            for (int i = 1; i < parts.length; i++) {
                try {
                    JSONParser jp = new JSONParser();
                    InputStream is = new URL(CommonData.getSpeciesListServer() + "/ws/speciesList?druid=" + parts[i]).openStream();
                    String listName = ((JSONObject) jp.parse(IOUtils.toString(is))).get("listName").toString();
                    is.close();
                    Map<String, List<String>> m = map.get(columnTitle);
                    if (m == null)
                        m = new HashMap<String, List<String>>();
                    ArrayList<String> sp = new ArrayList<String>();
                    //fetch species list
                    Collection<SpeciesListItemDTO> list = SpeciesListUtil.getListItems(parts[i]);
                    for (SpeciesListItemDTO item : list) {
                        if (item.getLsid() != null && !item.getLsid().isEmpty())
                            sp.add(item.getLsid());
                    }
                    Collections.sort(sp);
                    m.put(listName, sp);
                    map.put(columnTitle, m);
                } catch (Exception e) {
                    LOGGER.error("error reading list: " + parts[i], e);
                }
            }
        }
    }
    return map;
}
Also used : Point(com.vividsolutions.jts.geom.Point) URL(java.net.URL) SpeciesListItemDTO(au.org.ala.spatial.dto.SpeciesListItemDTO) JSONParser(org.json.simple.parser.JSONParser)

Example 57 with JSONParser

use of org.json.simple.parser.JSONParser in project Glowstone by GlowstoneMC.

the class PlayerDataFetcher method getProfile.

/**
     * Look up the PlayerProfile for a given UUID.
     *
     * @param uuid The UUID to look up.
     * @return The resulting PlayerProfile, or null on failure.
     */
public static PlayerProfile getProfile(UUID uuid) {
    InputStream is;
    try {
        URL url = new URL(PROFILE_URL + UuidUtils.toFlatString(uuid) + PROFILE_URL_SUFFIX);
        URLConnection conn = url.openConnection();
        //potentially blocking
        is = conn.getInputStream();
    } catch (IOException e) {
        GlowServer.logger.log(Level.WARNING, "Failed to look up profile");
        return null;
    }
    JSONObject json;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
        if (br.ready()) {
            json = (JSONObject) new JSONParser().parse(br);
        } else {
            return new PlayerProfile(null, uuid);
        }
    } catch (ParseException e) {
        GlowServer.logger.log(Level.WARNING, "Failed to parse profile response", e);
        return null;
    } catch (IOException e) {
        GlowServer.logger.log(Level.WARNING, "Failed to look up profile", e);
        return null;
    }
    return PlayerProfile.fromJson(json);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection)

Example 58 with JSONParser

use of org.json.simple.parser.JSONParser in project jackrabbit-oak by apache.

the class ConcurrentConflictTest method concurrentUpdates.

private void concurrentUpdates(final boolean useBranch) throws Exception {
    LOG.info("====== Start test =======");
    final AtomicInteger conflicts = new AtomicInteger();
    final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
    List<Thread> writers = new ArrayList<Thread>();
    for (final DocumentMK mk : kernels) {
        writers.add(new Thread(new Runnable() {

            Random random = new Random();

            Map<Integer, JSONObject> nodes = new HashMap<Integer, JSONObject>();

            @Override
            public void run() {
                BitSet conflictSet = new BitSet();
                int numTransfers = 0;
                try {
                    while (numTransfers < NUM_TRANSFERS_PER_THREAD && exceptions.isEmpty()) {
                        try {
                            if (!transfer()) {
                                continue;
                            }
                        } catch (DocumentStoreException e) {
                            log("Failed transfer @" + mk.getHeadRevision());
                            // assume conflict
                            conflicts.incrementAndGet();
                            conflictSet.set(numTransfers);
                        }
                        numTransfers++;
                    }
                } catch (Exception e) {
                    exceptions.add(e);
                }
                log("conflicts (" + conflictSet.cardinality() + "): " + conflictSet);
            }

            private boolean transfer() throws Exception {
                // read 3 random nodes and re-distribute values
                nodes.clear();
                while (nodes.size() < 3) {
                    nodes.put(random.nextInt(NUM_NODES), null);
                }
                String rev;
                if (useBranch) {
                    rev = mk.branch(null);
                } else {
                    rev = mk.getHeadRevision();
                }
                int sum = 0;
                for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
                    String json = mk.getNodes("/node-" + entry.getKey(), rev, 0, 0, 1000, null);
                    JSONParser parser = new JSONParser();
                    JSONObject obj = (JSONObject) parser.parse(json);
                    entry.setValue(obj);
                    sum += (Long) obj.get("value");
                }
                if (sum < 60) {
                    // retry with other nodes
                    return false;
                }
                StringBuilder jsop = new StringBuilder();
                boolean withdrawn = false;
                for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
                    long value = (Long) entry.getValue().get("value");
                    jsop.append("^\"/node-").append(entry.getKey());
                    jsop.append("/value\":");
                    if (value >= 20 && !withdrawn) {
                        jsop.append(value - 20);
                        withdrawn = true;
                    } else {
                        jsop.append(value + 10);
                    }
                }
                String oldRev = rev;
                rev = mk.commit("", jsop.toString(), rev, null);
                if (useBranch) {
                    rev = mk.merge(rev, null);
                }
                log("Successful transfer @" + oldRev + ": " + jsop.toString() + " (new rev: " + rev + ")");
                long s = calculateSum(mk, rev);
                if (s != NUM_NODES * 100) {
                    throw new Exception("Sum mismatch: " + s);
                }
                return true;
            }
        }));
    }
    for (Thread t : writers) {
        t.start();
    }
    for (Thread t : writers) {
        t.join();
    }
    // dispose will flush all pending revisions
    for (DocumentMK mk : kernels) {
        mk.dispose();
    }
    DocumentMK mk = openDocumentMK(1);
    String rev = mk.getHeadRevision();
    long sum = calculateSum(mk, rev);
    log("Conflict rate: " + conflicts.get() + "/" + (NUM_WRITERS * NUM_TRANSFERS_PER_THREAD));
    System.out.print(logBuffer);
    assertEquals(NUM_NODES * 100, sum);
    if (!exceptions.isEmpty()) {
        throw exceptions.get(0);
    }
    mk.dispose();
}
Also used : ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Random(java.util.Random) JSONObject(org.json.simple.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONParser(org.json.simple.parser.JSONParser) HashMap(java.util.HashMap) Map(java.util.Map)

Example 59 with JSONParser

use of org.json.simple.parser.JSONParser in project jackrabbit-oak by apache.

the class ConcurrentConflictTest method calculateSum.

static long calculateSum(DocumentMK mk, String rev) throws Exception {
    long sum = 0;
    for (int i = 0; i < NUM_NODES; i++) {
        String json = mk.getNodes("/node-" + i, rev, 0, 0, 1000, null);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(json);
        sum += (Long) obj.get("value");
    }
    return sum;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser)

Example 60 with JSONParser

use of org.json.simple.parser.JSONParser in project jackrabbit-oak by apache.

the class DocumentMKTestBase method parseJSONArray.

protected JSONArray parseJSONArray(String json) throws AssertionError {
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(json);
        assertTrue(obj instanceof JSONArray);
        return (JSONArray) obj;
    } catch (Exception e) {
        throw new AssertionError("not a valid JSON array: " + e.getMessage());
    }
}
Also used : JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)200 JSONObject (org.json.simple.JSONObject)168 JSONArray (org.json.simple.JSONArray)73 ParseException (org.json.simple.parser.ParseException)49 HttpClient (org.apache.commons.httpclient.HttpClient)44 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)28 HashMap (java.util.HashMap)23 Map (java.util.Map)19 IOException (java.io.IOException)18 File (java.io.File)16 Before (org.junit.Before)15 URL (java.net.URL)14 ArrayList (java.util.ArrayList)13 InputStreamReader (java.io.InputStreamReader)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 InputStream (java.io.InputStream)7