Search in sources :

Example 21 with JsonArray

use of org.json.simple.JsonArray in project jaggery by wso2.

the class JaggeryDeployerManager method addFilters.

private static void addFilters(Context ctx, JSONObject jaggeryConfig) {
    if (jaggeryConfig != null) {
        JSONArray arrFilters = (JSONArray) jaggeryConfig.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS);
        JSONArray arrFilterMappings = (JSONArray) jaggeryConfig.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS);
        if (arrFilters != null) {
            for (Object filterObj : arrFilters) {
                JSONObject filter = (JSONObject) filterObj;
                String name = (String) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_NAME);
                String clazz = (String) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_CLASS);
                FilterDef filterDef = new FilterDef();
                filterDef.setFilterName(name);
                filterDef.setFilterClass(clazz);
                JSONArray arrParams = (JSONArray) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS);
                if (arrParams != null) {
                    for (Object paramObj : arrParams) {
                        JSONObject param = (JSONObject) paramObj;
                        String paramName = (String) param.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS_NAME);
                        String paramValue = (String) param.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS_VALUE);
                        filterDef.addInitParameter(paramName, paramValue);
                    }
                }
                ctx.addFilterDef(filterDef);
            }
        }
        if (arrFilterMappings != null) {
            for (Object filterMappingObj : arrFilterMappings) {
                JSONObject mapping = (JSONObject) filterMappingObj;
                String name = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS_NAME);
                String url = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS_URL);
                FilterMap filterMapping = new FilterMap();
                filterMapping.setFilterName(name);
                filterMapping.addURLPattern(url);
                ctx.addFilterMap(filterMapping);
            }
        }
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject)

Example 22 with JsonArray

use of org.json.simple.JsonArray in project jaggery by wso2.

the class JaggeryDeployerManager method addUrlMappings.

private static void addUrlMappings(Context context, JSONObject obj) {
    Object test = context.getServletContext().getAttribute("org.jaggeryjs.serveFunction");
    JSONArray arr = null;
    if (test != null) {
        // URL mapping for progamticaly
        arr = new JSONArray();
        JSONObject urlJSONObj = new JSONObject();
        JSONObject pathJSONObj = new JSONObject();
        urlJSONObj.put("url", "/*");
        pathJSONObj.put("path", File.separator + INDEX_JAG);
        arr.add(urlJSONObj);
    } else {
        arr = (JSONArray) obj.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS);
    }
    if (arr != null) {
        Map<String, Object> urlMappings = new HashMap<String, Object>();
        for (Object mapObj : arr) {
            JSONObject mapping = (JSONObject) mapObj;
            String url = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS_URL);
            String path = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS_PATH);
            if (url != null && path != null) {
                path = path.startsWith("/") ? path : "/" + path;
                FilterMap filterMap = new FilterMap();
                filterMap.setFilterName(JaggeryCoreConstants.JAGGERY_FILTER_NAME);
                filterMap.addURLPattern(url);
                context.addFilterMap(filterMap);
                if (url.equals("/")) {
                    urlMappings.put("/", path);
                    continue;
                }
                url = url.startsWith("/") ? url.substring(1) : url;
                List<String> parts = new ArrayList<String>(Arrays.asList(url.split("/", -1)));
                addMappings(urlMappings, parts, path);
            } else {
                log.error("Invalid url mapping in jaggery.conf url : " + url + ", path : " + path);
            }
        }
        context.getServletContext().setAttribute(CommonManager.JAGGERY_URLS_MAP, urlMappings);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject)

Example 23 with JsonArray

use of org.json.simple.JsonArray in project jaggery by wso2.

the class WebAppManager method exec.

private static void exec(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream sourceIn;
    Context cx;
    JaggeryContext context;
    RhinoEngine engine = null;
    ServletContext servletContext = request.getServletContext();
    try {
        engine = CommonManager.getInstance().getEngine();
        cx = engine.enterContext();
        String scriptPath = getScriptPath(request);
        OutputStream out = response.getOutputStream();
        context = createJaggeryContext(cx, out, scriptPath, request, response);
        context.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext()));
        if (ModuleManager.isModuleRefreshEnabled()) {
            //reload init scripts
            refreshServletContext(servletContext);
            InputStream jaggeryConf = servletContext.getResourceAsStream(JaggeryCoreConstants.JAGGERY_CONF_FILE);
            if (jaggeryConf != null) {
                StringWriter writer = new StringWriter();
                IOUtils.copy(jaggeryConf, writer, null);
                String jsonString = writer.toString();
                JSONObject conf = (JSONObject) JSONValue.parse(jsonString);
                JSONArray initScripts = (JSONArray) conf.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS);
                if (initScripts != null) {
                    JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(servletContext);
                    ScriptableObject sharedScope = sharedContext.getScope();
                    Object[] scripts = initScripts.toArray();
                    for (Object script : scripts) {
                        if (!(script instanceof String)) {
                            log.error("Invalid value for initScripts in jaggery.conf : " + script);
                            continue;
                        }
                        String path = (String) script;
                        path = path.startsWith("/") ? path : "/" + path;
                        Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                        callstack.push(path);
                        engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {

                            @Override
                            protected void build() throws IOException {
                                try {
                                    sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                                } catch (ScriptException e) {
                                    throw new IOException(e);
                                }
                            }
                        }, sharedScope, null);
                        callstack.pop();
                    }
                }
            }
        }
        Object serveFunction = request.getServletContext().getAttribute(SERVE_FUNCTION_JAGGERY);
        if (serveFunction != null) {
            Function function = (Function) serveFunction;
            ScriptableObject scope = context.getScope();
            function.call(cx, scope, scope, new Object[] { scope.get("request", scope), scope.get("response", scope), scope.get("session", scope) });
        } else {
            //resource rendering model proceeding
            sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
            if (sourceIn == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
                return;
            }
            CommonManager.getInstance().getEngine().exec(new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath));
        }
    } catch (ScriptException e) {
        String msg = e.getMessage();
        log.error(msg, e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } catch (Error e) {
        //Rhino doesn't catch Error instances and it is been used to stop the script execution
        //from any specific place. Hence, Error exception propagates up to Java and we silently
        //ignore it, assuming it was initiated by an exit() method call.
        log.debug("Script has called exit() method", e);
    } finally {
        // Exiting from the context
        if (engine != null) {
            RhinoEngine.exitContext();
        }
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ServletContext(javax.servlet.ServletContext) WebAppFileManager(org.jaggeryjs.jaggery.core.plugins.WebAppFileManager) JSONArray(org.json.simple.JSONArray) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JSONObject(org.json.simple.JSONObject) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 24 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)

Example 25 with JsonArray

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

Aggregations

JSONArray (org.json.simple.JSONArray)148 JSONObject (org.json.simple.JSONObject)129 JSONParser (org.json.simple.parser.JSONParser)61 HttpClient (org.apache.commons.httpclient.HttpClient)21 ParseException (org.json.simple.parser.ParseException)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)15 File (java.io.File)11 IOException (java.io.IOException)11 MapLayer (au.org.emii.portal.menu.MapLayer)8 FileReader (java.io.FileReader)6 HashMap (java.util.HashMap)6 List (java.util.List)6 LegendObject (au.org.ala.legend.LegendObject)5 Point (com.vividsolutions.jts.geom.Point)5 PrintStream (java.io.PrintStream)5 Event (org.zkoss.zk.ui.event.Event)5 BuildResult (com.sonar.orchestrator.build.BuildResult)4 SonarScanner (com.sonar.orchestrator.build.SonarScanner)4