gt:             if (varbind == null) {
context: /*******************************************************************************
   * This file is part of OpenNMS(R).
   *
   * Copyright (C) 2002-2014 The OpenNMS Group, Inc.
   * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License,
   * or (at your option) any later version.
   *
   * OpenNMS(R) is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with OpenNMS(R).  If not, see:
   *      http://www.gnu.org/licenses/
   *
   * For more information contact:
   *     OpenNMS(R) Licensing <license@opennms.org>
   *     http://www.opennms.org/
   *     http://www.opennms.com/
   *******************************************************************************/
  
  package org.opennms.web.admin.notification.noticeWizard;
  
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.SortedMap;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.opennms.core.db.DataSourceFactory;
  import org.opennms.core.utils.DBUtils;
  import org.opennms.core.utils.InetAddressUtils;
  import org.opennms.netmgt.config.NotifdConfigFactory;
  import org.opennms.netmgt.config.NotificationFactory;
  import org.opennms.netmgt.config.notifications.Notification;
  import org.opennms.netmgt.config.notifications.Parameter;
  import org.opennms.netmgt.config.notifications.Varbind;
  import org.opennms.netmgt.filter.FilterDaoFactory;
  import org.opennms.netmgt.filter.api.FilterDao;
  import org.opennms.netmgt.filter.api.FilterParseException;
  import org.opennms.web.api.Util;
  
  /**
   * A servlet that handles the data comming in from the notification wizard jsps.
   *
   * @author <A HREF="mailto:jason@opennms.org">Jason Johns </A>
   * @author <A HREF="http://www.opennms.org/">OpenNMS </A>
   */
  public class NotificationWizardServlet extends HttpServlet {
      private static final long serialVersionUID = -5623373180751511103L;
  
      private static final Pattern SERVICES = Pattern.compile("\\s*\\&\\s*\\(\\s*\\!?is.+");
      private static final Pattern WHITESPACE = Pattern.compile("\\s+");
      private static final Pattern WHITESPACE_BEGINNING = Pattern.compile("^\\s");
      private static final Pattern WHITESPACE_END = Pattern.compile("\\s$");
  
      //SOURCE_PAGE_EVENTS_VIEW is more of a tag than an actual page - can't be used for navigation as is
      /** Constant <code>SOURCE_PAGE_OTHER_WEBUI="eventslist"</code> */
      public static final String SOURCE_PAGE_OTHER_WEBUI = "eventslist";
  
      /** Constant <code>SOURCE_PAGE_NOTICES="eventNotices.htm"</code> */
      public static final String SOURCE_PAGE_NOTICES = "eventNotices.htm";
  
      /** Constant <code>SOURCE_PAGE_NOTIFS_FOR_UEI="notifsForUEI.jsp"</code> */
      public static final String SOURCE_PAGE_NOTIFS_FOR_UEI = "notifsForUEI.jsp";
  
      /** Constant <code>SOURCE_PAGE_UEIS="chooseUeis.htm"</code> */
      public static final String SOURCE_PAGE_UEIS = "chooseUeis.htm";
  
      /** Constant <code>SOURCE_PAGE_RULE="buildRule.jsp"</code> */
      public static final String SOURCE_PAGE_RULE = "buildRule.jsp";
  
      /** Constant <code>SOURCE_PAGE_VALIDATE="validateRule.jsp"</code> */
      public static final String SOURCE_PAGE_VALIDATE = "validateRule.jsp";
  
      /** Constant <code>SOURCE_PAGE_PATH_OUTAGE="buildPathOutage.jsp"</code> */
      public static final String SOURCE_PAGE_PATH_OUTAGE = "buildPathOutage.jsp";
  
      /** Constant <code>SOURCE_PAGE_VALIDATE_PATH_OUTAGE="validatePathOutage.jsp"</code> */
      public static final String SOURCE_PAGE_VALIDATE_PATH_OUTAGE = "validatePathOutage.jsp";
  
      /** Constant <code>SOURCE_PAGE_PATH="choosePath.jsp"</code> */
      public static final String SOURCE_PAGE_PATH = "choosePath.jsp";
  
      /** Constant <code>SOURCE_PAGE_NOTIFICATION_INDEX="../index.jsp"</code> */
      public static final String SOURCE_PAGE_NOTIFICATION_INDEX = "../index.jsp";
  
      private static final String SQL_DELETE_CRITICAL_PATH = "DELETE FROM pathoutage WHERE nodeid=?";
  
      private static final String SQL_SET_CRITICAL_PATH = "INSERT INTO pathoutage (nodeid, criticalpathip, criticalpathservicename) VALUES (?, ?, ?)";
  
      /** {@inheritDoc} */
      @Override
      public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
          final String sourcePage = request.getParameter("sourcePage");
          final HttpSession user = request.getSession(true);
  
          /*
           * FIXME: Why do we do this for every request in doPost instead of
           * once in init?
           */
          try {
              NotifdConfigFactory.init();
          } catch (final Throwable t) {
              throw new ServletException("Failed to initialize NotifdConfigFactory: " + t.getMessage(), t);
          }
          try {
              NotificationFactory.init();
          } catch (final Throwable t) {
              throw new ServletException("Failed to initialize NotificationFactory: " + t.getMessage(), t);
          }
  
          if (SOURCE_PAGE_NOTICES.equals(sourcePage)) {
              response.sendRedirect(processNotices(request, user));
          } else if (SOURCE_PAGE_UEIS.equals(sourcePage)) {
              response.sendRedirect(processUeis(request, user));
          } else if (SOURCE_PAGE_RULE.equals(sourcePage)) {
              response.sendRedirect(processRule(request, user));
          } else if (SOURCE_PAGE_VALIDATE.equals(sourcePage)) {
              response.sendRedirect(processValidate(request, user));
          } else if (SOURCE_PAGE_PATH.equals(sourcePage)) {
              response.sendRedirect(processPath(request, user));
          } else if (SOURCE_PAGE_PATH_OUTAGE.equals(sourcePage)) {
              response.sendRedirect(processPathOutage(request));
          } else if (SOURCE_PAGE_VALIDATE_PATH_OUTAGE.equals(sourcePage)) {
              response.sendRedirect(processValidatePathOutage(request));
          } else if (SOURCE_PAGE_OTHER_WEBUI.equals(sourcePage)) {
              response.sendRedirect(processOtherWebUi(request, user));
          } else if (SOURCE_PAGE_NOTIFS_FOR_UEI.equals(sourcePage)) {
              response.sendRedirect(processNotificationsForUei(request, user));
          } else {
              throw new ServletException("no redirect specified for this wizard!");
          }
      }
  
      private String processNotices(final HttpServletRequest request, final HttpSession user) throws ServletException {
          final String userAction = request.getParameter("userAction");
  
          if ("delete".equals(userAction)) {
              try {
                  getNotificationFactory().removeNotification(request.getParameter("notice"));
              } catch (final Throwable t) {
                  throw new ServletException("Couldn't save/reload notifications configuration file: " + t.getMessage(), t);
              }
              return SOURCE_PAGE_NOTICES;
          } else if ("edit".equals(userAction)) {
              return edit(request, user);
          } else if ("new".equals(userAction)) {
              user.setAttribute("newNotice", buildNewNotification("off"));
              return SOURCE_PAGE_UEIS;
          } else if ("on".equals(userAction) || "off".equals(userAction)) {
              try {
                  getNotificationFactory().updateStatus(request.getParameter("notice"), userAction);
              } catch (final Throwable t) {
                  throw new ServletException("Couldn't save/reload notifications configuration file: " + t.getMessage(), t);
              }
  
              return SOURCE_PAGE_NOTICES;
          } else {
              // FIXME: We should do something if we hit this
              return "";
          }
      }
  
      private String processUeis(final HttpServletRequest request, final HttpSession user) {
          final Notification newNotice = (Notification) user.getAttribute("newNotice");
          newNotice.setUei(request.getParameter("uei"));
  
          final Map<String, Object> params = new HashMap<String, Object>();
          params.put("newRule", toSingleQuote(newNotice.getRule()));
  
          return SOURCE_PAGE_RULE + makeQueryString(params);
      }
  
      private String processValidate(final HttpServletRequest request, final HttpSession user) {
          final String userAction = request.getParameter("userAction");
  
          if ("rebuild".equals(userAction)) {
              final Map<String, Object> params = new HashMap<String, Object>();
              params.put("newRule", request.getParameter("newRule"));
              final String[] services = request.getParameterValues("services");
              if (services != null) {
                  params.put("services", services);
              }
              params.put("mode", "rebuild");
  
              return SOURCE_PAGE_RULE + makeQueryString(params);
          } else {
              final Notification newNotice = (Notification) user.getAttribute("newNotice");
              newNotice.setRule(request.getParameter("newRule"));
              return SOURCE_PAGE_PATH;
          }
      }
  
      private String processRule(final HttpServletRequest request, final HttpSession user) {
          String ruleString = request.getParameter("newRule");
          ruleString = toSingleQuote(ruleString);
          ruleString = stripExtraWhite(ruleString);
          ruleString = stripServices(ruleString);
          ruleString = checkParens(ruleString);
  
          final StringBuilder rule = new StringBuilder(ruleString);
  
          final String[] services = request.getParameterValues("services");
          if (services != null) {
              rule.append(" & ").append(" (");
  
              for (int i = 0; i < services.length; i++) {
                  rule.append("is").append(services[i]);
                  if (i < services.length - 1) {
                      rule.append(" | ");
                  }
              }
  
              rule.append(" )");
          }
  
          final String[] notServices = request.getParameterValues("notServices");
          if (notServices != null) {
              rule.append(" & ").append(" (");
  
              for (int i = 0; i < notServices.length; i++) {
                  rule.append("!is").append(notServices[i]);
                  if (i < notServices.length - 1) {
                      rule.append(" & ");
                  }
              }
  
              rule.append(" )");
          }
  
          final Map<String, Object> params = new HashMap<String, Object>();
          params.put("newRule", rule.toString());
          if (services != null) {
              params.put("services", services);
          }
          if (notServices != null) {
              params.put("notServices", notServices);
          }
  
          // page to redirect to, either validate or skip validation
          String redirectPage = request.getParameter("nextPage");
  
          // now lets see if the rule is syntactically valid
          try {
              getFilterDao().validateRule(rule.toString());
          } catch (final FilterParseException e) {
              // page to redirect to if the rule is invalid
              params.put("mode", "failed");
              redirectPage = SOURCE_PAGE_RULE;
          }
  
          // save the rule if we are bypassing validation
          if (redirectPage.equals(SOURCE_PAGE_PATH)) {
              final Notification newNotice = (Notification) user.getAttribute("newNotice");
              newNotice.setRule(rule.toString());
          }
  
          return redirectPage + makeQueryString(params);
      }
  
      private String processPath(final HttpServletRequest request, final HttpSession user) throws ServletException {
          final Notification newNotice = (Notification) user.getAttribute("newNotice");
          newNotice.setDestinationPath(request.getParameter("path"));
  
          final String description = request.getParameter("description");
          if (description != null && !description.trim().equals("")) {
              newNotice.setDescription(description);
          } else {
              newNotice.setDescription(null);
          }
  
          newNotice.setTextMessage(request.getParameter("textMsg"));
  
          final String subject = request.getParameter("subject");
          if (subject != null && !subject.trim().equals("")) {
              newNotice.setSubject(subject);
          } else {
              newNotice.setSubject(null);
          }
  
          final String numMessage = request.getParameter("numMsg");
          if (numMessage != null && !numMessage.trim().equals("")) {
              newNotice.setNumericMessage(numMessage);
          } else {
              newNotice.setNumericMessage(null);
          }
  
          final String oldName = newNotice.getName();
          newNotice.setName(request.getParameter("name"));
  
          final String varbindName = request.getParameter("varbindName");
          final String varbindValue = request.getParameter("varbindValue");
  
          Varbind varbind=newNotice.getVarbind();           
          if (varbindName != null && !varbindName.trim().equals("") && varbindValue != null && !varbindValue.trim().equals("")) {
  