use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.
the class AlarmQueryServlet method getDateFromRequest.
/**
* <p>getDateFromRequest</p>
*
* @param request a {@link javax.servlet.http.HttpServletRequest} object.
* @param prefix a {@link java.lang.String} object.
* @return a {@link 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 = 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 = 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 = 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 = 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 = 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 = 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 AlarmQueryServlet 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 {@link AlarmFilterController AlarmFilterController}.
*/
@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 ServiceFilter
String service = request.getParameter("service");
if (service != null && !service.equals(AlarmUtil.ANY_SERVICES_OPTION)) {
filterArray.add(new ServiceFilter(WebSecurityUtils.safeParseInt(service), 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 = request.getParameter("severity");
if (severity != null && !severity.equalsIgnoreCase(AlarmUtil.ANY_SEVERITIES_OPTION)) {
filterArray.add(new SeverityFilter(OnmsSeverity.get(WebSecurityUtils.safeParseInt(severity))));
}
// convenient syntax for AfterDateFilter as relative to current time
String relativeTime = request.getParameter("relativetime");
if (relativeTime != null && !relativeTime.equalsIgnoreCase(AlarmUtil.ANY_RELATIVE_TIMES_OPTION)) {
try {
filterArray.add(AlarmUtil.getRelativeTimeFilter(WebSecurityUtils.safeParseInt(relativeTime)));
} catch (IllegalArgumentException e) {
// ignore the relative time if it is an illegal value
this.log("Illegal relativetime value", e);
}
}
String useBeforeLastEventTime = WebSecurityUtils.sanitizeString(request.getParameter("usebeforelasteventtime"));
if (useBeforeLastEventTime != null && useBeforeLastEventTime.equals("1")) {
try {
filterArray.add(this.getBeforeLastEventTimeFilter(request));
} catch (IllegalArgumentException e) {
// ignore the before time if it is an illegal value
this.log("Illegal before last event time value", e);
} catch (MissingParameterException e) {
throw new ServletException(e);
}
}
String useAfterLastEventTime = WebSecurityUtils.sanitizeString(request.getParameter("useafterlasteventtime"));
if (useAfterLastEventTime != null && useAfterLastEventTime.equals("1")) {
try {
filterArray.add(this.getAfterLastEventTimeFilter(request));
} catch (IllegalArgumentException e) {
// ignore the after time if it is an illegal value
this.log("Illegal after last event time value", e);
} catch (MissingParameterException e) {
throw new ServletException(e);
}
}
String useBeforeFirstEventTime = WebSecurityUtils.sanitizeString(request.getParameter("usebeforefirsteventtime"));
if (useBeforeFirstEventTime != null && useBeforeFirstEventTime.equals("1")) {
try {
filterArray.add(this.getBeforeFirstEventTimeFilter(request));
} catch (IllegalArgumentException e) {
// ignore the before time if it is an illegal value
this.log("Illegal before first event time value", e);
} catch (MissingParameterException e) {
throw new ServletException(e);
}
}
String useAfterFirstEventTime = WebSecurityUtils.sanitizeString(request.getParameter("useafterfirsteventtime"));
if (useAfterFirstEventTime != null && useAfterFirstEventTime.equals("1")) {
try {
filterArray.add(this.getAfterFirstEventTimeFilter(request));
} catch (IllegalArgumentException e) {
// ignore the after time if it is an illegal value
this.log("Illegal after first event time value", e);
} catch (MissingParameterException e) {
throw new ServletException(e);
}
}
String location = WebSecurityUtils.sanitizeString(request.getParameter("location"));
if (location != null && !location.equalsIgnoreCase("any")) {
filterArray.add(new LocationFilter(WebSecurityUtils.sanitizeString(location)));
}
String nodelocation = WebSecurityUtils.sanitizeString(request.getParameter("nodelocation"));
if (nodelocation != null && !nodelocation.equalsIgnoreCase("any")) {
filterArray.add(new NodeLocationFilter(WebSecurityUtils.sanitizeString(nodelocation)));
}
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] = AlarmUtil.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 ImportAssetsServlet method doPost.
/**
* {@inheritDoc}
*
* Acknowledge the events specified in the POST and then redirect the client
* to an appropriate URL for display.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String assetsText = request.getParameter("assetsText");
if (assetsText == null) {
logger.error("assetsText was null");
throw new MissingParameterException("assetsText");
}
try {
List<Asset> assets = this.decodeAssetsText(assetsText);
List<Integer> nodesWithAssets = this.getCurrentAssetNodesList();
for (Asset asset : assets) {
// update with the current information
asset.setUserLastModified(request.getRemoteUser());
asset.setLastModifiedDate(new Date());
if (nodesWithAssets.contains(Integer.valueOf(asset.getNodeId()))) {
logger.debug("modifyAsset call for asset:'{}'", asset);
this.model.modifyAsset(asset);
} else {
logger.debug("createAsset:'{}'", asset);
this.model.createAsset(asset);
}
}
final StringBuilder messageText = new StringBuilder();
messageText.append("Successfully imported ").append(assets.size()).append(" asset");
if (assets.size() > 1) {
messageText.append("s");
}
messageText.append(".");
if (errors.size() > 0) {
messageText.append(" ").append(errors.size()).append(" non-fatal errors occurred:");
for (String error : errors) {
messageText.append("<br />").append(error);
}
}
request.getSession().setAttribute("message", messageText.toString());
response.sendRedirect(response.encodeRedirectURL(this.redirectSuccess + "&showMessage=true"));
} catch (AssetException e) {
String message = "Error importing assets: " + e.getMessage();
redirectWithErrorMessage(request, response, e, message);
} catch (SQLException e) {
String message = "Database exception importing assets: " + e.getMessage();
redirectWithErrorMessage(request, response, e, message);
}
}
use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.
the class ModifyAssetServlet method doPost.
/**
* {@inheritDoc}
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nodeIdString = request.getParameter("node");
String isNewString = request.getParameter("isnew");
if (nodeIdString == null) {
throw new MissingParameterException("node", new String[] { "node", "isnew" });
}
if (isNewString == null) {
throw new MissingParameterException("isnew", new String[] { "node", "isnew" });
}
int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
boolean isNew = Boolean.valueOf(isNewString).booleanValue();
Asset asset = this.parms2Asset(request, nodeId);
Event evnt = EventUtils.createAssetInfoChangedEvent("OpenNMS.WebUI", nodeId, -1L);
sendEvent(evnt);
try {
if (isNew) {
this.model.createAsset(asset);
} else {
this.model.modifyAsset(asset);
}
response.sendRedirect("modify.jsp?node=" + nodeId);
} catch (SQLException e) {
throw new ServletException("database error", e);
}
}
use of org.opennms.web.servlet.MissingParameterException in project opennms by OpenNMS.
the class AcknowledgeAlarmByFilterController method handleRequestInternal.
/**
* {@inheritDoc}
*
* Acknowledge the events specified in the POST and then redirect the client
* to an appropriate URL for display.
*/
@Override
public ModelAndView handleRequestInternal(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 = AlarmUtil.getFilter(filterString, getServletContext());
if (filter != null) {
filterArray.add(filter);
}
}
Filter[] filters = filterArray.toArray(new Filter[filterArray.size()]);
OnmsCriteria criteria = AlarmUtil.getOnmsCriteria(new AlarmCriteria(filters));
if (action.equals(AcknowledgeType.ACKNOWLEDGED.getShortName())) {
m_webAlarmRepository.acknowledgeMatchingAlarms(request.getRemoteUser(), new Date(), criteria);
} else if (action.equals(AcknowledgeType.UNACKNOWLEDGED.getShortName())) {
m_webAlarmRepository.unacknowledgeMatchingAlarms(criteria, 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);
}
Aggregations