Search in sources :

Example 16 with MissingParameterException

use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.

the class AcknowledgeAlarmController method handleRequestInternal.

/**
 * {@inheritDoc}
 *
 * Acknowledge the alarms specified in the POST and then redirect the client
 * to an appropriate URL for display.
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // required parameter
    String[] alarmIdStrings = request.getParameterValues("alarm");
    String action = request.getParameter("actionCode");
    if (alarmIdStrings == null) {
        throw new MissingParameterException("alarm", new String[] { "alarm", "actionCode" });
    }
    if (action == null) {
        throw new MissingParameterException("actionCode", new String[] { "alarm", "actionCode" });
    }
    // convert the alarm id strings to ints
    int[] alarmIds = new int[alarmIdStrings.length];
    for (int i = 0; i < alarmIds.length; i++) {
        alarmIds[i] = WebSecurityUtils.safeParseInt(alarmIdStrings[i]);
    }
    if (action.equals(AcknowledgeType.ACKNOWLEDGED.getShortName())) {
        m_webAlarmRepository.acknowledgeAlarms(alarmIds, request.getRemoteUser(), new Date());
    } else if (action.equals(AcknowledgeType.UNACKNOWLEDGED.getShortName())) {
        m_webAlarmRepository.unacknowledgeAlarms(alarmIds, request.getRemoteUser());
    } else {
        throw new ServletException("Unknown acknowledge action: " + action);
    }
    String redirectParms = request.getParameter("redirectParms");
    String redirect = request.getParameter("redirect");
    String viewName;
    if (redirect != null) {
        viewName = redirect;
    } else {
        viewName = (redirectParms == null || redirectParms == "" || redirectParms == "null" ? m_redirectView : m_redirectView + "?" + redirectParms);
    }
    RedirectView view = new RedirectView(viewName, true);
    return new ModelAndView(view);
}
Also used : ServletException(javax.servlet.ServletException) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) MissingParameterException(org.opennms.web.servlet.MissingParameterException) Date(java.util.Date)

Example 17 with MissingParameterException

use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.

the class AlarmSeverityChangeController method handleRequestInternal.

/**
 * {@inheritDoc}
 *
 * Adjust the severity of the alarms specified in the POST and then redirect the client
 * to an appropriate URL for display.
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // required parameter
    String[] alarmIdStrings = request.getParameterValues("alarm");
    String action = request.getParameter("actionCode");
    if (alarmIdStrings == null) {
        throw new MissingParameterException("alarm", new String[] { "alarm", "actionCode" });
    }
    if (action == null) {
        throw new MissingParameterException("actionCode", new String[] { "alarm", "actionCode" });
    }
    // convert the alarm id strings to ints
    int[] alarmIds = new int[alarmIdStrings.length];
    for (int i = 0; i < alarmIds.length; i++) {
        alarmIds[i] = WebSecurityUtils.safeParseInt(alarmIdStrings[i]);
    }
    if (action.equals(ESCALATE_ACTION)) {
        m_webAlarmRepository.escalateAlarms(alarmIds, request.getRemoteUser(), new Date());
    } else if (action.equals(CLEAR_ACTION)) {
        m_webAlarmRepository.clearAlarms(alarmIds, request.getRemoteUser(), new Date());
    } else {
        throw new ServletException("Unknown alarm severity action: " + action);
    }
    String redirectParms = request.getParameter("redirectParms");
    String redirect = request.getParameter("redirect");
    String viewName;
    if (redirect != null) {
        viewName = redirect;
    } else {
        viewName = (redirectParms == null || redirectParms == "" || redirectParms == "null" ? m_redirectView : m_redirectView + "?" + redirectParms);
    }
    RedirectView view = new RedirectView(viewName, true);
    return new ModelAndView(view);
}
Also used : ServletException(javax.servlet.ServletException) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) MissingParameterException(org.opennms.web.servlet.MissingParameterException) Date(java.util.Date)

Example 18 with MissingParameterException

use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.

the class EventController method acknowledgeByFilter.

/**
 * Acknowledge the events specified in the POST and then redirect the client
 * to an appropriate URL for display.
 */
public ModelAndView acknowledgeByFilter(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // required parameter
    String[] filterStrings = request.getParameterValues("filter");
    String action = request.getParameter("actionCode");
    if (filterStrings == null) {
        filterStrings = new String[0];
    }
    if (action == null) {
        throw new MissingParameterException("actionCode", new String[] { "filter", "actionCode" });
    }
    // handle the filter parameters
    ArrayList<Filter> filterArray = new ArrayList<>();
    for (String filterString : filterStrings) {
        Filter filter = EventUtil.getFilter(filterString, getServletContext());
        if (filter != null) {
            filterArray.add(filter);
        }
    }
    Filter[] filters = filterArray.toArray(new Filter[filterArray.size()]);
    EventCriteria criteria = new EventCriteria(filters);
    if (action.equals(AcknowledgeType.ACKNOWLEDGED.getShortName())) {
        m_webEventRepository.acknowledgeMatchingEvents(request.getRemoteUser(), new Date(), criteria);
    } else if (action.equals(AcknowledgeType.UNACKNOWLEDGED.getShortName())) {
        m_webEventRepository.unacknowledgeMatchingEvents(criteria);
    } else {
        throw new ServletException("Unknown acknowledge action: " + action);
    }
    return getRedirectView(request);
}
Also used : ServletException(javax.servlet.ServletException) EventIdFilter(org.opennms.web.event.filter.EventIdFilter) Filter(org.opennms.web.filter.Filter) EventIdListFilter(org.opennms.web.event.filter.EventIdListFilter) ArrayList(java.util.ArrayList) MissingParameterException(org.opennms.web.servlet.MissingParameterException) EventCriteria(org.opennms.web.event.filter.EventCriteria) Date(java.util.Date)

Example 19 with MissingParameterException

use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.

the class CustomViewController method handleRequestInternal.

/**
 * {@inheritDoc}
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String[] requiredParameters = new String[] { "report", "type" };
    // Get Form Variable
    int reportId = -1;
    String reportType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.type.toString()));
    String reportIdString = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.report.toString()));
    if (reportType == null) {
        throw new MissingParameterException(Parameters.type.toString(), requiredParameters);
    }
    if (reportIdString == null) {
        throw new MissingParameterException(Parameters.report.toString(), requiredParameters);
    }
    if (reportType.equals("node") || reportType.equals("custom")) {
        reportId = WebSecurityUtils.safeParseInt(reportIdString);
    }
    String overrideTimespan = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.timespan.toString()));
    if ("null".equals(overrideTimespan) || "none".equals(overrideTimespan)) {
        overrideTimespan = null;
    }
    String overrideGraphType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.graphtype.toString()));
    if ("null".equals(overrideGraphType) || "none".equals(overrideGraphType)) {
        overrideGraphType = null;
    }
    // Load report to view
    Report report = null;
    if ("node".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildNodeReport(reportId) {}", reportId);
        report = getKscReportService().buildNodeReport(reportId);
    } else if ("nodeSource".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildNodeSourceReport(nodeSource) {}", reportIdString);
        report = getKscReportService().buildNodeSourceReport(reportIdString);
    } else if ("domain".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildDomainReport(reportIdString) {}", reportIdString);
        report = getKscReportService().buildDomainReport(reportIdString);
    } else if ("custom".equals(reportType)) {
        LOG.debug("handleRequestInternal: getReportByIndex(reportId) {}", reportId);
        report = m_kscReportFactory.getReportByIndex(reportId);
        if (report == null) {
            throw new ServletException("Report could not be found in config file for index '" + reportId + "'");
        }
    } else {
        throw new IllegalArgumentException("value to 'type' parameter of '" + reportType + "' is not supported.  Must be one of: node, nodeSource, domain, or custom");
    }
    // Get the list of available prefabricated graph options
    Map<String, OnmsResource> resourceMap = new HashMap<String, OnmsResource>();
    Set<PrefabGraph> prefabGraphs = new TreeSet<>();
    if (removeBrokenGraphsFromReport(report) && reportId > -1) {
        m_kscReportFactory.setReport(reportId, report);
        m_kscReportFactory.saveCurrent();
        EventBuilder eb = new EventBuilder(EventConstants.KSC_REPORT_UPDATED_UEI, "Web UI");
        eb.addParam(EventConstants.PARAM_REPORT_TITLE, report.getTitle() == null ? "Report #" + report.getId() : report.getTitle());
        eb.addParam(EventConstants.PARAM_REPORT_GRAPH_COUNT, report.getGraphs().size());
        try {
            Util.createEventProxy().send(eb.getEvent());
        } catch (Throwable e) {
            LOG.error("Can't send event " + eb.getEvent(), e);
        }
    }
    List<Graph> graphCollection = report.getGraphs();
    if (!graphCollection.isEmpty()) {
        for (Graph graph : graphCollection) {
            final OnmsResource resource = getKscReportService().getResourceFromGraph(graph);
            resourceMap.put(graph.toString(), resource);
            if (resource == null) {
                LOG.debug("Could not get resource for graph {} in report {}", graph, report.getTitle());
            } else {
                prefabGraphs.addAll(Arrays.asList(getResourceService().findPrefabGraphsForResource(resource)));
            }
        }
    }
    List<KscResultSet> resultSets = new ArrayList<KscResultSet>(report.getGraphs().size());
    for (Graph graph : graphCollection) {
        OnmsResource resource = resourceMap.get(graph.toString());
        if (resource != null) {
            promoteResourceAttributesIfNecessary(resource);
        }
        String displayGraphType;
        if (overrideGraphType == null) {
            displayGraphType = graph.getGraphtype();
        } else {
            displayGraphType = overrideGraphType;
        }
        PrefabGraph displayGraph;
        try {
            displayGraph = getResourceService().getPrefabGraph(displayGraphType);
        } catch (ObjectRetrievalFailureException e) {
            LOG.debug("The prefabricated graph '{}' does not exist: {}", displayGraphType, e, e);
            displayGraph = null;
        }
        boolean foundGraph = false;
        if (resource != null) {
            for (PrefabGraph availableGraph : getResourceService().findPrefabGraphsForResource(resource)) {
                if (availableGraph.equals(displayGraph)) {
                    foundGraph = true;
                    break;
                }
            }
        }
        if (!foundGraph) {
            displayGraph = null;
        }
        // gather start/stop time information
        String displayTimespan = null;
        if (overrideTimespan == null) {
            displayTimespan = graph.getTimespan();
        } else {
            displayTimespan = overrideTimespan;
        }
        Calendar beginTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        KSC_PerformanceReportFactory.getBeginEndTime(displayTimespan, beginTime, endTime);
        KscResultSet resultSet = new KscResultSet(graph.getTitle(), beginTime.getTime(), endTime.getTime(), resource, displayGraph);
        resultSets.add(resultSet);
    }
    ModelAndView modelAndView = new ModelAndView("KSC/customView");
    modelAndView.addObject("loggedIn", request.getRemoteUser() != null);
    modelAndView.addObject("reportType", reportType);
    if (report != null) {
        modelAndView.addObject("report", reportIdString);
    }
    modelAndView.addObject("title", report.getTitle());
    modelAndView.addObject("resultSets", resultSets);
    if (report.getShowTimespanButton().orElse(false)) {
        if (overrideTimespan == null || !getKscReportService().getTimeSpans(true).containsKey(overrideTimespan)) {
            modelAndView.addObject("timeSpan", "none");
        } else {
            modelAndView.addObject("timeSpan", overrideTimespan);
        }
        modelAndView.addObject("timeSpans", getKscReportService().getTimeSpans(true));
    } else {
        // Make sure it's null so the pulldown list isn't shown
        modelAndView.addObject("timeSpan", null);
    }
    if (report.getShowGraphtypeButton().orElse(false)) {
        LinkedHashMap<String, String> graphTypes = new LinkedHashMap<String, String>();
        graphTypes.put("none", "none");
        for (PrefabGraph graphOption : prefabGraphs) {
            graphTypes.put(graphOption.getName(), graphOption.getName());
        }
        if (overrideGraphType == null || !graphTypes.containsKey(overrideGraphType)) {
            modelAndView.addObject("graphType", "none");
        } else {
            modelAndView.addObject("graphType", overrideGraphType);
        }
        modelAndView.addObject("graphTypes", graphTypes);
    } else {
        // Make sure it's null so the pulldown list isn't shown
        modelAndView.addObject("graphType", null);
    }
    modelAndView.addObject("showCustomizeButton", (request.isUserInRole(Authentication.ROLE_ADMIN) || !request.isUserInRole(Authentication.ROLE_READONLY)) && (request.getRemoteUser() != null));
    if (report.getGraphsPerLine().orElse(0) > 0) {
        modelAndView.addObject("graphsPerLine", report.getGraphsPerLine().get());
    } else {
        modelAndView.addObject("graphsPerLine", getDefaultGraphsPerLine());
    }
    return modelAndView;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KscResultSet(org.opennms.web.graph.KscResultSet) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) LinkedHashMap(java.util.LinkedHashMap) ServletException(javax.servlet.ServletException) TreeSet(java.util.TreeSet) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) Report(org.opennms.netmgt.config.kscReports.Report) Calendar(java.util.Calendar) OnmsResource(org.opennms.netmgt.model.OnmsResource) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Graph(org.opennms.netmgt.config.kscReports.Graph) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Example 20 with MissingParameterException

use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.

the class FormProcViewController method handleRequestInternal.

/**
 * {@inheritDoc}
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Get Form Variables
    int reportId = -1;
    String overrideTimespan = null;
    String overrideGraphType = null;
    String reportAction = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.action.toString()));
    String reportIdString = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.report.toString()));
    String reportType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.type.toString()));
    if (reportAction == null) {
        throw new MissingParameterException("action", new String[] { "action", "report", "type" });
    }
    if (reportType == null) {
        throw new MissingParameterException("type", new String[] { "action", "report", "type" });
    }
    if (reportIdString == null) {
        throw new MissingParameterException("report", new String[] { "action", "report", "type" });
    }
    if (Actions.Customize.toString().equals(reportAction) || Actions.Update.toString().equals(reportAction)) {
        if (reportType.equals("node") || reportType.equals("custom")) {
            reportId = WebSecurityUtils.safeParseInt(reportIdString);
        }
        overrideTimespan = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.timespan.toString()));
        if ((overrideTimespan == null) || overrideTimespan.equals("null")) {
            overrideTimespan = "none";
        }
        overrideGraphType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.graphtype.toString()));
        if (overrideGraphType == null || overrideGraphType.equals("null")) {
            overrideGraphType = "none";
        }
        if (Actions.Customize.toString().equals(reportAction)) {
            // Fetch the KscReportEditor or create one if there isn't one already
            KscReportEditor editor = KscReportEditor.getFromSession(request.getSession(), false);
            LOG.debug("handleRequestInternal: build report for reportType {}", reportType);
            if (reportType.equals("node")) {
                editor.loadWorkingReport(m_kscReportService.buildNodeReport(reportId));
            } else if (reportType.equals("nodeSource")) {
                editor.loadWorkingReport(m_kscReportService.buildNodeSourceReport(reportIdString));
            } else if (reportType.equals("domain")) {
                editor.loadWorkingReport(m_kscReportService.buildDomainReport(reportIdString));
            } else {
                editor.loadWorkingReport(getKscReportFactory(), reportId);
            }
            // Now inject any override characteristics into the working report model
            Report working_report = editor.getWorkingReport();
            for (int i = 0; i < working_report.getGraphs().size(); i++) {
                final int index = i;
                Graph working_graph = working_report.getGraphs().get(index);
                if (!overrideTimespan.equals("none")) {
                    working_graph.setTimespan(overrideTimespan);
                }
                if (!overrideGraphType.equals("none")) {
                    working_graph.setGraphtype(overrideGraphType);
                }
            }
        }
    } else {
        if (!Actions.Exit.toString().equals(reportAction)) {
            throw new ServletException("Invalid Parameter contents for report_action");
        }
    }
    if (Actions.Update.toString().equals(reportAction)) {
        ModelAndView modelAndView = new ModelAndView("redirect:/KSC/customView.htm");
        modelAndView.addObject("type", reportType);
        if (reportIdString != null) {
            modelAndView.addObject("report", reportIdString);
        }
        if (overrideTimespan != null) {
            modelAndView.addObject("timespan", overrideTimespan);
        }
        if (overrideGraphType != null) {
            modelAndView.addObject("graphtype", overrideGraphType);
        }
        return modelAndView;
    } else if (Actions.Customize.toString().equals(reportAction)) {
        return new ModelAndView("redirect:/KSC/customReport.htm");
    } else if (Actions.Exit.toString().equals(reportAction)) {
        return new ModelAndView("redirect:/KSC/index.jsp");
    } else {
        throw new IllegalArgumentException("Parameter action of '" + reportAction + "' is not supported.  Must be one of: Update, Customize, or Exit");
    }
}
Also used : ServletException(javax.servlet.ServletException) Graph(org.opennms.netmgt.config.kscReports.Graph) Report(org.opennms.netmgt.config.kscReports.Report) ModelAndView(org.springframework.web.servlet.ModelAndView) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Aggregations

MissingParameterException (org.opennms.web.servlet.MissingParameterException)25 ServletException (javax.servlet.ServletException)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 Filter (org.opennms.web.filter.Filter)6 Calendar (java.util.Calendar)5 RedirectView (org.springframework.web.servlet.view.RedirectView)4 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 Report (org.opennms.netmgt.config.kscReports.Report)3 EventIdFilter (org.opennms.web.event.filter.EventIdFilter)3 Graph (org.opennms.netmgt.config.kscReports.Graph)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 OnmsResource (org.opennms.netmgt.model.OnmsResource)2 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)2 ResourceId (org.opennms.netmgt.model.ResourceId)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1