Search in sources :

Example 6 with JSONParser

use of org.json.simple.parser.JSONParser in project cassandra by apache.

the class ColumnFamilyStoreCQLHelperTest method testSnapshot.

@Test
public void testSnapshot() throws Throwable {
    String typeA = createType("CREATE TYPE %s (a1 varint, a2 varint, a3 varint);");
    String typeB = createType("CREATE TYPE %s (b1 frozen<" + typeA + ">, b2 frozen<" + typeA + ">, b3 frozen<" + typeA + ">);");
    String typeC = createType("CREATE TYPE %s (c1 frozen<" + typeB + ">, c2 frozen<" + typeB + ">, c3 frozen<" + typeB + ">);");
    String tableName = createTable("CREATE TABLE IF NOT EXISTS %s (" + "pk1 varint," + "pk2 ascii," + "ck1 varint," + "ck2 varint," + "reg1 " + typeC + "," + "reg2 int," + "reg3 int," + "PRIMARY KEY ((pk1, pk2), ck1, ck2)) WITH " + "CLUSTERING ORDER BY (ck1 ASC, ck2 DESC);");
    alterTable("ALTER TABLE %s DROP reg3 USING TIMESTAMP 10000;");
    alterTable("ALTER TABLE %s ADD reg3 int;");
    for (int i = 0; i < 10; i++) execute("INSERT INTO %s (pk1, pk2, ck1, ck2, reg1, reg2) VALUES (?, ?, ?, ?, ?, ?)", i, i + 1, i + 2, i + 3, null, i + 5);
    ColumnFamilyStore cfs = Keyspace.open(keyspace()).getColumnFamilyStore(tableName);
    cfs.snapshot(SNAPSHOT);
    String schema = Files.toString(cfs.getDirectories().getSnapshotSchemaFile(SNAPSHOT), Charset.defaultCharset());
    assertTrue(schema.contains(String.format("CREATE TYPE %s.%s (a1 varint, a2 varint, a3 varint);", keyspace(), typeA)));
    assertTrue(schema.contains(String.format("CREATE TYPE %s.%s (a1 varint, a2 varint, a3 varint);", keyspace(), typeA)));
    assertTrue(schema.contains(String.format("CREATE TYPE %s.%s (b1 frozen<%s>, b2 frozen<%s>, b3 frozen<%s>);", keyspace(), typeB, typeA, typeA, typeA)));
    assertTrue(schema.contains(String.format("CREATE TYPE %s.%s (c1 frozen<%s>, c2 frozen<%s>, c3 frozen<%s>);", keyspace(), typeC, typeB, typeB, typeB)));
    // trim to ensure order
    schema = schema.substring(schema.indexOf("CREATE TABLE"));
    assertTrue(schema.startsWith("CREATE TABLE IF NOT EXISTS " + keyspace() + "." + tableName + " (\n" + "\tpk1 varint,\n" + "\tpk2 ascii,\n" + "\tck1 varint,\n" + "\tck2 varint,\n" + "\treg2 int,\n" + "\treg3 int,\n" + "\treg1 " + typeC + ",\n" + "\tPRIMARY KEY ((pk1, pk2), ck1, ck2))\n" + "\tWITH ID = " + cfs.metadata.id + "\n" + "\tAND CLUSTERING ORDER BY (ck1 ASC, ck2 DESC)"));
    schema = schema.substring(schema.indexOf("ALTER"));
    assertTrue(schema.startsWith(String.format("ALTER TABLE %s.%s DROP reg3 USING TIMESTAMP 10000;", keyspace(), tableName)));
    assertTrue(schema.contains(String.format("ALTER TABLE %s.%s ADD reg3 int;", keyspace(), tableName)));
    JSONObject manifest = (JSONObject) new JSONParser().parse(new FileReader(cfs.getDirectories().getSnapshotManifestFile(SNAPSHOT)));
    JSONArray files = (JSONArray) manifest.get("files");
    Assert.assertEquals(1, files.size());
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) Test(org.junit.Test)

Example 7 with JSONParser

use of org.json.simple.parser.JSONParser in project flink by apache.

the class SimpleTweetInputFormat method open.

@Override
public void open(FileInputSplit split) throws IOException {
    super.open(split);
    this.handler = new TweetHandler();
    this.parser = new JSONParser();
}
Also used : JSONParser(org.json.simple.parser.JSONParser)

Example 8 with JSONParser

use of org.json.simple.parser.JSONParser in project json-android-compare by martinadamek.

the class SimpleJson method parsePublicTimeline.

public List<Map> parsePublicTimeline(InputStream inputStream) {
    List<Map> result = new ArrayList<Map>();
    JSONParser p = new JSONParser();
    try {
        Map map;
        Set keys;
        Set keys2;
        JSONObject user;
        JSONObject jsonObject;
        JSONArray jsonArray = (JSONArray) p.parse(new InputStreamReader(inputStream));
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            map = new HashMap();
            jsonObject = (JSONObject) jsonArray.get(i);
            keys = jsonObject.keySet();
            for (Object key : keys) {
                if ("user".equals(key)) {
                    user = (JSONObject) jsonObject.get(key);
                    keys2 = user.keySet();
                    for (Object key2 : keys2) {
                        map.put("user." + key2, user.get(key2));
                    }
                } else {
                    map.put(key, jsonObject.get(key));
                }
            }
            result.add(map);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : Set(java.util.Set) JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with JSONParser

use of org.json.simple.parser.JSONParser in project jaggery by wso2.

the class JaggeryDeployerManager method readJaggeryConfig.

private static JSONObject readJaggeryConfig(Context context, Path appBase) {
    String content = null;
    String path = null;
    if (context.getDocBase().contains(WAR_EXTENSION)) {
        try {
            if (!appBase.endsWith("/")) {
                path = appBase + File.separator + context.getDocBase();
            } else {
                path = appBase + context.getDocBase();
            }
            ZipFile zip = new ZipFile(path);
            for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                if (entry.getName().toLowerCase().contains(JAGGERY_CONF)) {
                    InputStream inputStream = zip.getInputStream(entry);
                    content = IOUtils.toString(inputStream);
                }
            }
        } catch (IOException e) {
            log.error("Error occuered when the accessing the jaggery.conf file of " + context.getPath().substring(1), e);
        }
    } else {
        File file = new File(appBase + context.getPath() + File.separator + JAGGERY_CONF);
        try {
            content = FileUtils.readFileToString(file);
        } catch (IOException e) {
            log.error("IOException is thrown when accessing the jaggery.conf file of " + context.getPath().substring(1), e);
        }
    }
    JSONObject jaggeryConfig = null;
    try {
        JSONParser jp = new JSONParser();
        jaggeryConfig = (JSONObject) jp.parse(content);
    } catch (ParseException e) {
        log.error("Error in parsing the jaggery.conf file", e);
    }
    return jaggeryConfig;
}
Also used : ZipFile(java.util.zip.ZipFile) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 10 with JSONParser

use of org.json.simple.parser.JSONParser in project opennms by OpenNMS.

the class AlarmChangeNotificationClient method sendDbNotification.

@Override
public void sendDbNotification(DbNotification dbNotification) {
    try {
        String payload = dbNotification.getPayload();
        JSONObject newJsonObject = null;
        JSONObject oldJsonObject = null;
        try {
            JSONParser parser = new JSONParser();
            Object obj;
            obj = parser.parse(payload);
            JSONArray jsonArray = (JSONArray) obj;
            LOG.debug("payload jsonArray.toString():" + jsonArray.toString());
            newJsonObject = (JSONObject) jsonArray.get(0);
            oldJsonObject = (JSONObject) jsonArray.get(1);
            newJsonObject = jsonAlarmTimeNormaliser(newJsonObject);
            oldJsonObject = jsonAlarmTimeNormaliser(oldJsonObject);
        } catch (ParseException e1) {
            throw new RuntimeException("cannot parse notification payload to json object. payload=" + payload, e1);
        }
        if (newJsonObject.isEmpty() && (!oldJsonObject.isEmpty())) {
            // ignore alarm type 2
            if (!"2".equals(oldJsonObject.get("alarmtype").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("alarm deleted alarmid=" + oldJsonObject.get("alarmid"));
                EventBuilder eb = jsonAlarmToEventBuilder(oldJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_DELETED_EVENT, EVENT_SOURCE_NAME));
                //copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                sendEvent(eb.getEvent());
            }
        } else if ((!newJsonObject.isEmpty()) && oldJsonObject.isEmpty()) {
            // ignore alarm type 2
            if (!"2".equals(newJsonObject.get("alarmtype").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("alarm created alarmid=" + newJsonObject.get("alarmid"));
                EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_CREATED_EVENT, EVENT_SOURCE_NAME));
                //copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                // set initial severity to new alarm severity
                if (newJsonObject.get("severity") != null) {
                    try {
                        String newseverity = newJsonObject.get("severity").toString();
                        Integer newsvty = Integer.valueOf(newseverity);
                        eb.addParam(AlarmChangeEventConstants.INITIAL_SEVERITY_PARAM, newsvty.toString());
                    } catch (Exception e) {
                        LOG.error("problem parsing initial severity for new alarm event newJsonObject=" + newJsonObject, e);
                    }
                }
                sendEvent(eb.getEvent());
            }
        } else {
            // ignore alarm type 2
            if (!"2".equals(newJsonObject.get("alarmtype").toString())) {
                // ignore event count and automation changes if these are only change in alarm
                // TODO need database trigger to also ignore these changes
                JSONObject newobj = new JSONObject(newJsonObject);
                JSONObject oldobj = new JSONObject(oldJsonObject);
                newobj.remove("lasteventtime");
                oldobj.remove("lasteventtime");
                newobj.remove("lasteventid");
                oldobj.remove("lasteventid");
                newobj.remove("counter");
                oldobj.remove("counter");
                newobj.remove("firstautomationtime");
                oldobj.remove("firstautomationtime");
                newobj.remove("lastautomationtime");
                oldobj.remove("lastautomationtime");
                if (!newobj.toString().equals(oldobj.toString())) {
                    // changes other than event count
                    // severity changed notification
                    String oldseverity = (oldJsonObject.get("severity") == null) ? null : oldJsonObject.get("severity").toString();
                    String newseverity = (newJsonObject.get("severity") == null) ? null : newJsonObject.get("severity").toString();
                    if (newseverity != null && !newseverity.equals(oldseverity)) {
                        // check if alarm cleared
                        EventBuilder eb = null;
                        if ("2".equals(newseverity)) {
                            if (LOG.isDebugEnabled())
                                LOG.debug("alarm cleared alarmid=" + oldJsonObject.get("alarmid") + " old severity=" + oldseverity + " new severity=" + newseverity);
                            eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_CLEARED_EVENT, EVENT_SOURCE_NAME));
                        } else {
                            // if just severity changed
                            if (LOG.isDebugEnabled())
                                LOG.debug("alarm severity changed alarmid=" + oldJsonObject.get("alarmid") + " old severity=" + oldseverity + " new severity=" + newseverity);
                            eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_SEVERITY_CHANGED_EVENT, EVENT_SOURCE_NAME));
                        }
                        eb.addParam(AlarmChangeEventConstants.OLDSEVERITY_PARAM, oldseverity);
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    }
                    // alarm acknowledged / unacknowledged notifications  
                    String oldalarmacktime = (oldJsonObject.get("alarmacktime") == null) ? null : oldJsonObject.get("alarmacktime").toString();
                    String newalarmacktime = (newJsonObject.get("alarmacktime") == null) ? null : newJsonObject.get("alarmacktime").toString();
                    if (oldalarmacktime == null && newalarmacktime != null) {
                        //alarm acknowledged notification
                        if (LOG.isDebugEnabled())
                            LOG.debug("alarm acknowleged alarmid=" + newJsonObject.get("alarmid"));
                        EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_ACKNOWLEDGED_EVENT, EVENT_SOURCE_NAME));
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    } else {
                        if (oldalarmacktime != null && newalarmacktime == null) {
                            //alarm unacknowledged notification
                            if (LOG.isDebugEnabled())
                                LOG.debug("alarm unacknowleged alarmid=" + newJsonObject.get("alarmid"));
                            //TODO issue unacknowledged doesn't have a user because only user and time in alarm field
                            EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_UNACKNOWLEDGED_EVENT, EVENT_SOURCE_NAME));
                            //copy in all values as json in params
                            eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                            eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                            sendEvent(eb.getEvent());
                        }
                    }
                    // alarm suppressed / unsuppressed notifications 
                    String newsuppresseduntil = (newJsonObject.get("suppresseduntil") == null) ? null : newJsonObject.get("suppresseduntil").toString();
                    String oldsuppresseduntil = (oldJsonObject.get("suppresseduntil") == null) ? null : oldJsonObject.get("suppresseduntil").toString();
                    if (newsuppresseduntil != null && !newsuppresseduntil.equals(oldsuppresseduntil)) {
                        //alarm suppressed notification
                        if (LOG.isDebugEnabled())
                            LOG.debug("alarm suppressed alarmid=" + newJsonObject.get("alarmid"));
                        EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_SUPPRESSED_EVENT, EVENT_SOURCE_NAME));
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    } else {
                        if (oldsuppresseduntil != null && newsuppresseduntil == null) {
                            //alarm unsuppressed notification
                            if (LOG.isDebugEnabled())
                                LOG.debug("alarm unsuppressed alarmid=" + newJsonObject.get("alarmid"));
                            //TODO issue unsuppress doesn't have a user because only user and time in alarm field
                            EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_UNSUPPRESSED_EVENT, EVENT_SOURCE_NAME));
                            //copy in all values as json in params
                            eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                            eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                            sendEvent(eb.getEvent());
                        }
                    }
                    // trouble ticket state changed notification
                    String oldtticketid = (oldJsonObject.get("tticketid") == null) ? null : oldJsonObject.get("tticketid").toString();
                    String newtticketid = (newJsonObject.get("tticketid") == null) ? null : newJsonObject.get("tticketid").toString();
                    String oldtticketstate = (oldJsonObject.get("tticketstate") == null) ? null : oldJsonObject.get("tticketstate").toString();
                    String newtticketstate = (newJsonObject.get("tticketstate") == null) ? null : newJsonObject.get("tticketstate").toString();
                    if ((oldtticketid == null && newtticketid != null) || (oldtticketid != null && !newtticketid.equals(oldtticketid)) || (oldtticketstate == null && newtticketstate != null) || (oldtticketstate != null && !newtticketstate.equals(oldtticketstate))) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("trouble ticket state changed for alarmid=" + oldJsonObject.get("alarmid") + " oldtticketid=" + oldtticketid + " newtticketid=" + newtticketid + " oldtticketstate=" + oldtticketstate + " newtticketstate=" + newtticketstate);
                        EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_TROUBLETICKET_STATE_CHANGE_EVENT, EVENT_SOURCE_NAME));
                        eb.addParam(AlarmChangeEventConstants.OLDTICKETID_PARAM, oldtticketid);
                        eb.addParam(AlarmChangeEventConstants.TTICKETID_PARAM, newtticketid);
                        eb.addParam(AlarmChangeEventConstants.OLDTTICKETSTATE_PARAM, oldtticketstate);
                        eb.addParam(AlarmChangeEventConstants.TTICKETSTATE_PARAM, newtticketstate);
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    }
                    // alarm sticky note changed notification
                    String oldstickymemo = (oldJsonObject.get("stickymemo") == null) ? null : oldJsonObject.get("stickymemo").toString();
                    String newstickymemo = (newJsonObject.get("stickymemo") == null) ? null : newJsonObject.get("stickymemo").toString();
                    if ((newstickymemo != null && !newstickymemo.equals(oldstickymemo))) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("Sticky memo added for alarmid=" + oldJsonObject.get("alarmid") + " newstickymemo=" + newstickymemo);
                        EventBuilder eb = jsonAlarmToEventBuilder(newJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_STICKYMEMO_ADD_EVENT, EVENT_SOURCE_NAME));
                        eb.addParam(AlarmChangeEventConstants.STICKYMEMO_PARAM, newstickymemo);
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    }
                    // send alarm changed event for any other changes not captured above
                    newobj.remove("severity");
                    oldobj.remove("severity");
                    newobj.remove("alarmacktime");
                    oldobj.remove("alarmacktime");
                    newobj.remove("alarmackuser");
                    oldobj.remove("alarmackuser");
                    newobj.remove("suppresseduntil");
                    oldobj.remove("suppresseduntil");
                    newobj.remove("suppresseduser");
                    oldobj.remove("suppresseduser");
                    newobj.remove("tticketid");
                    oldobj.remove("tticketid");
                    newobj.remove("tticketstate");
                    oldobj.remove("tticketstate");
                    newobj.remove("stickymemo");
                    oldobj.remove("stickymemo");
                    if (!newobj.toString().equals(oldobj.toString())) {
                        EventBuilder eb = jsonAlarmToEventBuilder(oldJsonObject, new EventBuilder(AlarmChangeEventConstants.ALARM_CHANGED_EVENT, EVENT_SOURCE_NAME));
                        //copy in all values as json in params
                        eb.addParam(AlarmChangeEventConstants.OLD_ALARM_VALUES_PARAM, oldJsonObject.toString());
                        eb.addParam(AlarmChangeEventConstants.NEW_ALARM_VALUES_PARAM, newJsonObject.toString());
                        sendEvent(eb.getEvent());
                    }
                }
            }
        }
    } catch (Exception e) {
        LOG.error("problem creating opennms alarm change event from database notification", e);
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) ParseException(org.json.simple.parser.ParseException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)146 JSONObject (org.json.simple.JSONObject)136 JSONArray (org.json.simple.JSONArray)61 HttpClient (org.apache.commons.httpclient.HttpClient)44 ParseException (org.json.simple.parser.ParseException)40 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)18 IOException (java.io.IOException)15 File (java.io.File)13 URL (java.net.URL)13 InputStreamReader (java.io.InputStreamReader)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 ArrayList (java.util.ArrayList)9 InputStream (java.io.InputStream)7 Facet (au.org.ala.legend.Facet)6