Search in sources :

Example 71 with JsonArray

use of org.json.simple.JsonArray in project Gargoyle by callakrsos.

the class SVNCreateFunction method apply.

/**
	 * @inheritDoc
	 */
@Override
public Boolean apply(Properties t) {
    JSONArray parse = null;
    try {
        String string = ResourceLoader.getInstance().get(SVN_REPOSITORIES);
        if (string == null || string.length() == 0) {
            parse = new JSONArray();
        } else {
            parse = (JSONArray) new JSONParser().parse(string);
        }
    } catch (ParseException e) {
        LOGGER.error(ValueUtil.toString(e));
        return false;
    }
    JSONObject jsonObject = new JSONObject();
    Iterator<Entry<Object, Object>> it = t.entrySet().iterator();
    while (it.hasNext()) {
        Entry<Object, Object> e = it.next();
        Object key = e.getKey();
        Object value = e.getValue();
        if (SVN_USER_PASS.equals(key)) {
            try {
                value = EncrypUtil.encryp(value.toString());
            } catch (Exception e1) {
                LOGGER.error(ValueUtil.toString(e1));
                return false;
            }
        }
        jsonObject.put(key, value);
    }
    parse.add(jsonObject);
    ResourceLoader.getInstance().put(SVN_REPOSITORIES, parse.toJSONString());
    return true;
}
Also used : Entry(java.util.Map.Entry) 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)

Example 72 with JsonArray

use of org.json.simple.JsonArray in project Gargoyle by callakrsos.

the class SVNDiscardLocationFunction method apply.

/**
	 * @inheritDoc
	 */
@Override
public Boolean apply(SVNRepository repo) {
    boolean resultFlag = false;
    if (repo == null)
        return resultFlag;
    JSONArray parse = null;
    try {
        String string = ResourceLoader.getInstance().get(SVN_REPOSITORIES);
        parse = (JSONArray) new JSONParser().parse(string);
    } catch (ParseException e) {
        LOGGER.error(ValueUtil.toString(e));
        return resultFlag;
    }
    //		String simpleName = repo.getSimpleName();
    /* URL정보와 USERID가 정보가 합쳐져 키값으로 구성됨. */
    String savedUrl = repo.getURL();
    //		Object savedUserId = repo.getUserId();
    boolean isFound = false;
    Iterator<Object> it = parse.iterator();
    int idx = -1;
    while (it.hasNext()) {
        JSONObject e = (JSONObject) it.next();
        Object objURL = e.get(SVN_URL);
        //			Object objUserId = e.get(SVN_USER_ID);
        idx++;
        if (objURL == null)
            continue;
        String url = objURL.toString();
        if (savedUrl.equals(url)) {
            isFound = true;
            break;
        }
    }
    if (isFound) {
        parse.remove(idx);
        resultFlag = true;
    } else {
        return resultFlag;
    }
    ResourceLoader.getInstance().put(SVN_REPOSITORIES, parse.toJSONString());
    return resultFlag;
}
Also used : 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)

Example 73 with JsonArray

use of org.json.simple.JsonArray in project Gargoyle by callakrsos.

the class JavaSVNManagerTest method setting.

@Before
public void setting() throws ParseException {
    JSONArray jsonArray = ResourceLoader.getInstance().getJsonArray("svn.repositores");
    if (jsonArray.isEmpty()) {
        Assert.fail("properties is empty.");
    }
    jsonObj = (JSONObject) jsonArray.get(0);
    if (jsonObj.isEmpty()) {
        Assert.fail("properties is empty.");
    }
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    Iterator iterator = jsonObj.keySet().iterator();
    while (iterator.hasNext()) {
        Object key = iterator.next();
        Object val = jsonObj.get(key);
        try {
            String string = key.toString();
            if (JavaSVNManager.SVN_USER_PASS.equals(string))
                hashMap.put(string, EncrypUtil.decryp(val.toString()));
            else
                hashMap.put(string, val.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Properties properties = new Properties();
    properties.put(JavaSVNManager.SVN_URL, hashMap.get(JavaSVNManager.SVN_URL));
    properties.put(JavaSVNManager.SVN_USER_ID, hashMap.get(JavaSVNManager.SVN_USER_ID));
    properties.put(JavaSVNManager.SVN_USER_PASS, hashMap.get(JavaSVNManager.SVN_USER_PASS));
    manager = new JavaSVNManager(properties);
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) Iterator(java.util.Iterator) JSONObject(org.json.simple.JSONObject) Properties(java.util.Properties) ParseException(org.json.simple.parser.ParseException) Before(org.junit.Before)

Example 74 with JsonArray

use of org.json.simple.JsonArray in project tika by apache.

the class DL4JInceptionV3Net method loadClassIndex.

/**
     * Loads the class to
     *
     * @param stream label index stream
     * @return Map of integer -> label name
     * @throws IOException    when the stream breaks unexpectedly
     * @throws ParseException when the input doesn't contain a valid JSON map
     */
public Map<Integer, String> loadClassIndex(InputStream stream) throws IOException, ParseException {
    String content = IOUtils.toString(stream);
    JSONObject jIndex = (JSONObject) new JSONParser().parse(content);
    Map<Integer, String> classMap = new HashMap<>();
    for (Object key : jIndex.keySet()) {
        JSONArray names = (JSONArray) jIndex.get(key);
        classMap.put(Integer.parseInt(key.toString()), names.get(names.size() - 1).toString());
    }
    return classMap;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) RecognisedObject(org.apache.tika.parser.recognition.RecognisedObject) JSONObject(org.json.simple.JSONObject)

Example 75 with JsonArray

use of org.json.simple.JsonArray 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

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6