Search in sources :

Example 6 with ParseException

use of org.json.simple.parser.ParseException 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 7 with ParseException

use of org.json.simple.parser.ParseException 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 8 with ParseException

use of org.json.simple.parser.ParseException 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)

Example 9 with ParseException

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

the class MemosChangeNotificationClient method sendDbNotification.

@Override
public void sendDbNotification(DbNotification dbNotification) {
    try {
        String payload = dbNotification.getPayload();
        JSONObject memoJsonObject = null;
        JSONObject alarmIdJsonObject = null;
        String alarmId = null;
        String body = null;
        String author = null;
        String reductionkey = null;
        try {
            JSONParser parser = new JSONParser();
            Object obj;
            obj = parser.parse(payload);
            JSONArray jsonArray = (JSONArray) obj;
            if (LOG.isDebugEnabled())
                LOG.debug("payload memo jsonArray.toString():" + jsonArray.toString());
            memoJsonObject = (JSONObject) jsonArray.get(0);
            memoJsonObject = jsonMemoTimeNormaliser(memoJsonObject);
            alarmIdJsonObject = (JSONObject) jsonArray.get(1);
            alarmId = (alarmIdJsonObject.get("alarmid") == null) ? null : alarmIdJsonObject.get("alarmid").toString();
            body = (memoJsonObject.get("body") == null) ? null : memoJsonObject.get("body").toString();
            author = (memoJsonObject.get("author") == null) ? null : memoJsonObject.get("author").toString();
            reductionkey = (memoJsonObject.get("reductionkey") == null) ? null : memoJsonObject.get("reductionkey").toString();
        } catch (ParseException e1) {
            throw new RuntimeException("cannot parse notification payload to json object. payload=" + payload, e1);
        }
        if (!memoJsonObject.isEmpty()) {
            // sticky note event
            if ("Memo".equals(memoJsonObject.get("type").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("sticky memo updated=" + memoJsonObject.get("id"));
                EventBuilder eb = new EventBuilder(AlarmChangeEventConstants.STICKY_MEMO_EVENT, EVENT_SOURCE_NAME);
                // copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.MEMO_VALUES_PARAM, memoJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.MEMO_ALARMID_PARAM, alarmId);
                eb.addParam(AlarmChangeEventConstants.MEMO_BODY_PARAM, body);
                eb.addParam(AlarmChangeEventConstants.MEMO_AUTHOR_PARAM, author);
                sendEvent(eb.getEvent());
            } else if ("ReductionKeyMemo".equals(memoJsonObject.get("type").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("reduction key memo updated=" + memoJsonObject.get("id"));
                EventBuilder eb = new EventBuilder(AlarmChangeEventConstants.JOURNAL_MEMO_EVENT, EVENT_SOURCE_NAME);
                // copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.MEMO_VALUES_PARAM, memoJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.MEMO_ALARMID_PARAM, alarmId);
                eb.addParam(AlarmChangeEventConstants.MEMO_BODY_PARAM, body);
                eb.addParam(AlarmChangeEventConstants.MEMO_AUTHOR_PARAM, author);
                eb.addParam(AlarmChangeEventConstants.MEMO_REDUCTIONKEY_PARAM, reductionkey);
                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)

Example 10 with ParseException

use of org.json.simple.parser.ParseException in project metron by apache.

the class EnrichmentSplitterBolt method generateMessage.

@Override
public JSONObject generateMessage(Tuple tuple) {
    JSONObject message = null;
    if (messageFieldName == null) {
        byte[] data = tuple.getBinary(0);
        try {
            message = (JSONObject) parser.parse(new String(data, "UTF8"));
            message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis());
        } catch (ParseException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else {
        message = (JSONObject) tuple.getValueByField(messageFieldName);
        message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis());
    }
    return message;
}
Also used : JSONObject(org.json.simple.JSONObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException)

Aggregations

ParseException (org.json.simple.parser.ParseException)258 JSONObject (org.json.simple.JSONObject)191 JSONParser (org.json.simple.parser.JSONParser)185 JSONArray (org.json.simple.JSONArray)84 IOException (java.io.IOException)71 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)34 Map (java.util.Map)23 HashSet (java.util.HashSet)18 API (org.wso2.carbon.apimgt.api.model.API)18 BufferedReader (java.io.BufferedReader)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)13 List (java.util.List)12 File (java.io.File)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 InputStreamReader (java.io.InputStreamReader)10 URL (java.net.URL)10