Search in sources :

Example 21 with MissingParameterException

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

the class AcknowledgeNotificationController method handleRequestInternal.

/**
 * {@inheritDoc}
 *
 * Acknowledge the notifications 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 {
    String[] required = { "notices" };
    String[] noticeIdStrings = request.getParameterValues("notices");
    if (noticeIdStrings == null) {
        throw new MissingParameterException("notices", required);
    }
    String currentUser = request.getParameter("curUser");
    if (currentUser == null) {
        currentUser = request.getRemoteUser();
    }
    List<Integer> noticeIds = new ArrayList<>();
    for (String noticeIdString : noticeIdStrings) {
        noticeIds.add(WebSecurityUtils.safeParseInt(noticeIdString));
    }
    List<Filter> filters = new ArrayList<>();
    filters.add(new NotificationIdListFilter(noticeIds.toArray(new Integer[0])));
    NotificationCriteria criteria = new NotificationCriteria(filters.toArray(new Filter[0]));
    m_webNotificationRepository.acknowledgeMatchingNotification(currentUser, new Date(), criteria);
    Notification[] notices = m_webNotificationRepository.getMatchingNotifications(criteria);
    request.setAttribute("notices", notices);
    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 : ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) Date(java.util.Date) Notification(org.opennms.web.notification.Notification) NotificationIdListFilter(org.opennms.web.notification.filter.NotificationIdListFilter) Filter(org.opennms.web.filter.Filter) NotificationIdListFilter(org.opennms.web.notification.filter.NotificationIdListFilter) NotificationCriteria(org.opennms.web.notification.filter.NotificationCriteria) RedirectView(org.springframework.web.servlet.view.RedirectView) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Example 22 with MissingParameterException

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

the class ElementUtil method getNodeByParams.

/**
 * <p>getNodeByParams</p>
 *
 * @param request a {@link javax.servlet.http.HttpServletRequest} object.
 * @param nodeLookupParam a {@link java.lang.String} object.
 * @return a {@link OnmsNode} object.
 * @throws javax.servlet.ServletException if any.
 * @throws java.sql.SQLException if any.
 */
public static OnmsNode getNodeByParams(HttpServletRequest request, String nodeLookupParam, ServletContext servletContext) throws ServletException, SQLException {
    if (request.getParameter(nodeLookupParam) == null) {
        throw new MissingParameterException(nodeLookupParam, new String[] { "node" });
    }
    String nodeLookupString = request.getParameter(nodeLookupParam);
    if (!nodeLookupString.contains(":")) {
        try {
            Integer.parseInt(nodeLookupString);
        } catch (NumberFormatException e) {
            throw new ElementIdNotFoundException("Wrong data type for \"" + nodeLookupString + "\", should be integer", nodeLookupString, "node", "element/node.jsp", "node", "element/nodeList.htm");
        }
    }
    OnmsNode node = NetworkElementFactory.getInstance(servletContext).getNode(nodeLookupString);
    if (node == null) {
        throw new ElementNotFoundException("No such node in database", "node", "element/node.jsp", "node", "element/nodeList.htm");
    }
    return node;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Example 23 with MissingParameterException

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

the class EventQueryServlet method getDateFromRequest.

/**
 * <p>getDateFromRequest</p>
 *
 * @param request a {@link javax.servlet.http.HttpServletRequest} object.
 * @param prefix a {@link java.lang.String} object.
 * @return a java$util$Date object.
 * @throws org.opennms.web.servlet.MissingParameterException if any.
 */
protected Date getDateFromRequest(HttpServletRequest request, String prefix) throws MissingParameterException {
    if (request == null || prefix == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    Calendar cal = Calendar.getInstance();
    // be lenient to handle the inputs easier
    // read the java.util.Calendar javadoc for more info
    cal.setLenient(true);
    // hour, from 1-12
    String hourString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "hour"));
    if (hourString == null) {
        throw new MissingParameterException(prefix + "hour", this.getRequiredDateFields(prefix));
    }
    cal.set(Calendar.HOUR, WebSecurityUtils.safeParseInt(hourString));
    // minute, from 0-59
    String minuteString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "minute"));
    if (minuteString == null) {
        throw new MissingParameterException(prefix + "minute", this.getRequiredDateFields(prefix));
    }
    cal.set(Calendar.MINUTE, WebSecurityUtils.safeParseInt(minuteString));
    // AM/PM, either AM or PM
    String amPmString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "ampm"));
    if (amPmString == null) {
        throw new MissingParameterException(prefix + "ampm", this.getRequiredDateFields(prefix));
    }
    if (amPmString.equalsIgnoreCase("am")) {
        cal.set(Calendar.AM_PM, Calendar.AM);
    } else if (amPmString.equalsIgnoreCase("pm")) {
        cal.set(Calendar.AM_PM, Calendar.PM);
    } else {
        throw new IllegalArgumentException("Illegal AM/PM value: " + amPmString);
    }
    // month, 0-11 (Jan-Dec)
    String monthString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "month"));
    if (monthString == null) {
        throw new MissingParameterException(prefix + "month", this.getRequiredDateFields(prefix));
    }
    cal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(monthString));
    // date, 1-31
    String dateString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "date"));
    if (dateString == null) {
        throw new MissingParameterException(prefix + "date", this.getRequiredDateFields(prefix));
    }
    cal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(dateString));
    // year
    String yearString = WebSecurityUtils.sanitizeString(request.getParameter(prefix + "year"));
    if (yearString == null) {
        throw new MissingParameterException(prefix + "year", this.getRequiredDateFields(prefix));
    }
    cal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(yearString));
    return cal.getTime();
}
Also used : Calendar(java.util.Calendar) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Example 24 with MissingParameterException

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

the class EventQueryServlet method doGet.

/**
 * {@inheritDoc}
 *
 * Extracts the key parameters from the parameter set, translates them into
 * filter-based parameters, and then passes the modified parameter set to
 * the event filter.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Filter> filterArray = new ArrayList<>();
    // convenient syntax for LogMessageSubstringFilter
    String msgSubstring = WebSecurityUtils.sanitizeString(request.getParameter("msgsub"));
    if (msgSubstring != null && msgSubstring.length() > 0) {
        filterArray.add(new LogMessageSubstringFilter(msgSubstring));
    }
    // convenient syntax for LogMessageMatchesAnyFilter
    String msgMatchAny = WebSecurityUtils.sanitizeString(request.getParameter("msgmatchany"));
    if (msgMatchAny != null && msgMatchAny.length() > 0) {
        filterArray.add(new LogMessageMatchesAnyFilter(msgMatchAny));
    }
    // convenient syntax for NodeNameContainingFilter
    String nodeNameLike = WebSecurityUtils.sanitizeString(request.getParameter("nodenamelike"));
    if (nodeNameLike != null && nodeNameLike.length() > 0) {
        filterArray.add(new NodeNameLikeFilter(nodeNameLike));
    }
    // convenient syntax for ExactUEIFilter
    String exactUEI = WebSecurityUtils.sanitizeString(request.getParameter("exactuei"));
    if (exactUEI != null && exactUEI.length() > 0) {
        filterArray.add(new ExactUEIFilter(exactUEI));
    }
    // convenient syntax for LocationFilter
    String location = WebSecurityUtils.sanitizeString(request.getParameter("location"));
    if (location != null && !location.equalsIgnoreCase("any")) {
        filterArray.add(new LocationFilter(WebSecurityUtils.sanitizeString(location)));
    }
    // convenient syntax for NodeLocationFilter
    String nodeLocation = WebSecurityUtils.sanitizeString(request.getParameter("nodelocation"));
    if (nodeLocation != null && !nodeLocation.equalsIgnoreCase("any")) {
        filterArray.add(new NodeLocationFilter(WebSecurityUtils.sanitizeString(nodeLocation)));
    }
    // convenient syntax for SystemIdFilter
    String systemId = WebSecurityUtils.sanitizeString(request.getParameter("systemId"));
    if (systemId != null && !systemId.equalsIgnoreCase("any")) {
        filterArray.add(new SystemIdFilter(WebSecurityUtils.sanitizeString(systemId)));
    }
    // convenient syntax for ServiceFilter
    String service = WebSecurityUtils.sanitizeString(request.getParameter("service"));
    if (service != null && !service.equalsIgnoreCase("any")) {
        filterArray.add(new ServiceFilter(WebSecurityUtils.safeParseInt(service), this.getServletContext()));
    }
    // convenient syntax for IPLikeFilter
    String ipLikePattern = WebSecurityUtils.sanitizeString(request.getParameter("iplike"));
    if (ipLikePattern != null && !ipLikePattern.equals("")) {
        filterArray.add(new IPAddrLikeFilter(ipLikePattern));
    }
    // convenient syntax for SeverityFilter
    String severity = WebSecurityUtils.sanitizeString(request.getParameter("severity"));
    if (severity != null && !severity.equalsIgnoreCase("any")) {
        filterArray.add(new SeverityFilter(WebSecurityUtils.safeParseInt(severity)));
    }
    String eventId = WebSecurityUtils.sanitizeString(request.getParameter("eventid"));
    if (eventId != null && !"".equals(eventId)) {
        int eventIdInt = WebSecurityUtils.safeParseInt(eventId);
        if (eventIdInt > 0) {
            filterArray.add(new EventIdFilter(eventIdInt));
        }
    }
    // convenient syntax for AfterDateFilter as relative to current time
    String relativeTime = WebSecurityUtils.sanitizeString(request.getParameter("relativetime"));
    if (relativeTime != null && !relativeTime.equalsIgnoreCase("any")) {
        int timeInt = WebSecurityUtils.safeParseInt(relativeTime);
        if (timeInt > 0) {
            try {
                filterArray.add(EventUtil.getRelativeTimeFilter(timeInt));
            } catch (IllegalArgumentException e) {
                // ignore the relative time if it is an illegal value
                this.log("Illegal relativetime value", e);
            }
        }
    }
    String useBeforeTime = WebSecurityUtils.sanitizeString(request.getParameter("usebeforetime"));
    if (useBeforeTime != null && useBeforeTime.equals("1")) {
        try {
            filterArray.add(this.getBeforeDateFilter(request));
        } catch (IllegalArgumentException e) {
            // ignore the before time if it is an illegal value
            this.log("Illegal before time value", e);
        } catch (MissingParameterException e) {
            throw new ServletException(e);
        }
    }
    String useAfterTime = WebSecurityUtils.sanitizeString(request.getParameter("useaftertime"));
    if (useAfterTime != null && useAfterTime.equals("1")) {
        try {
            filterArray.add(this.getAfterDateFilter(request));
        } catch (IllegalArgumentException e) {
            // ignore the after time if it is an illegal value
            this.log("Illegal after time value", e);
        } catch (MissingParameterException e) {
            throw new ServletException(e);
        }
    }
    String queryString = "";
    if (filterArray.size() > 0) {
        String[] filterStrings = new String[filterArray.size()];
        for (int i = 0; i < filterStrings.length; i++) {
            Filter filter = filterArray.get(i);
            filterStrings[i] = EventUtil.getFilterString(filter);
        }
        Map<String, Object> paramAdditions = new HashMap<String, Object>();
        paramAdditions.put("filter", filterStrings);
        queryString = Util.makeQueryString(request, paramAdditions, IGNORE_LIST);
    } else {
        queryString = Util.makeQueryString(request, IGNORE_LIST);
    }
    response.sendRedirect(redirectUrl + "?" + queryString);
}
Also used : NodeNameLikeFilter(org.opennms.web.event.filter.NodeNameLikeFilter) ServiceFilter(org.opennms.web.event.filter.ServiceFilter) IPAddrLikeFilter(org.opennms.web.event.filter.IPAddrLikeFilter) HashMap(java.util.HashMap) LogMessageSubstringFilter(org.opennms.web.event.filter.LogMessageSubstringFilter) ArrayList(java.util.ArrayList) ExactUEIFilter(org.opennms.web.event.filter.ExactUEIFilter) ServletException(javax.servlet.ServletException) NodeLocationFilter(org.opennms.web.event.filter.NodeLocationFilter) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) LogMessageMatchesAnyFilter(org.opennms.web.event.filter.LogMessageMatchesAnyFilter) EventIdFilter(org.opennms.web.event.filter.EventIdFilter) NodeLocationFilter(org.opennms.web.event.filter.NodeLocationFilter) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) SystemIdFilter(org.opennms.web.event.filter.SystemIdFilter) IPAddrLikeFilter(org.opennms.web.event.filter.IPAddrLikeFilter) ExactUEIFilter(org.opennms.web.event.filter.ExactUEIFilter) NodeNameLikeFilter(org.opennms.web.event.filter.NodeNameLikeFilter) LocationFilter(org.opennms.web.event.filter.LocationFilter) ServiceFilter(org.opennms.web.event.filter.ServiceFilter) BeforeDateFilter(org.opennms.web.event.filter.BeforeDateFilter) AfterDateFilter(org.opennms.web.event.filter.AfterDateFilter) Filter(org.opennms.web.filter.Filter) LogMessageMatchesAnyFilter(org.opennms.web.event.filter.LogMessageMatchesAnyFilter) LogMessageSubstringFilter(org.opennms.web.event.filter.LogMessageSubstringFilter) NodeLocationFilter(org.opennms.web.event.filter.NodeLocationFilter) LocationFilter(org.opennms.web.event.filter.LocationFilter) SystemIdFilter(org.opennms.web.event.filter.SystemIdFilter) MissingParameterException(org.opennms.web.servlet.MissingParameterException) EventIdFilter(org.opennms.web.event.filter.EventIdFilter)

Example 25 with MissingParameterException

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

the class MailerServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String sendto = request.getParameter("sendto");
    String subject = request.getParameter("subject");
    String msg = request.getParameter("msg");
    String username = request.getRemoteUser();
    LOG.debug("To: {}, Subject: {}, message: {}, username: {}", sendto, subject, msg, username);
    if (sendto == null) {
        throw new MissingParameterException("sendto", REQUIRED_FIELDS);
    }
    if (subject == null) {
        throw new MissingParameterException("subject", REQUIRED_FIELDS);
    }
    if (msg == null) {
        throw new MissingParameterException("msg", REQUIRED_FIELDS);
    }
    if (username == null) {
        username = "";
    }
    String[] cmdArgs = { this.mailProgram, "-s", subject, sendto };
    Process process = Runtime.getRuntime().exec(cmdArgs);
    // send the message to the stdin of the mail command
    PrintWriter stdinWriter = new PrintWriter(process.getOutputStream());
    stdinWriter.print(msg);
    stdinWriter.close();
    // get the stderr to see if the command failed
    BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    if (err.ready()) {
        // get the error message
        StringWriter tempErr = new StringWriter();
        StreamUtils.streamToStream(err, tempErr);
        String errorMessage = tempErr.toString();
        // log the error message
        LOG.warn("Read from stderr: {}", errorMessage);
        // send the error message to the client
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        StreamUtils.streamToStream(new StringReader(errorMessage), out);
        out.close();
    } else {
        response.sendRedirect(this.redirectSuccess);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MissingParameterException(org.opennms.web.servlet.MissingParameterException) PrintWriter(java.io.PrintWriter)

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