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);
}
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;
}
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();
}
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);
}
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);
}
}
Aggregations