vulnerabilityfactory.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 660 行 · 第 1/2 页

JAVA
660
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program 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 General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///package org.opennms.web.vulnerability;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Date;import org.apache.log4j.Category;import org.opennms.core.resource.Vault;import org.opennms.core.utils.ThreadCategory;import org.opennms.web.vulnerability.filter.Filter;import org.opennms.web.vulnerability.filter.InterfaceFilter;import org.opennms.web.vulnerability.filter.NodeFilter;/** * Encapsulates all querying functionality for vulnerabilities. *  * @author <A HREF="mailto:larry@opennms.org">Lawrence Karnowski </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public class VulnerabilityFactory extends Object {    /** Convenience class to determine sort style of a query. */    public static class SortStyle extends Object {        /* CORBA-style enumeration */        public static final int _ID = 1;        public static final int _SEVERITY = 2;        public static final int _NODE = 3;        public static final int _INTERFACE = 4;        public static final int _SERVICE = 5;        public static final int _CREATE_TIME = 6;        public static final int _RESOLVED_TIME = 7;        public static final int _PORT = 8;        public static final int _PROTOCOL = 9;        public static final SortStyle ID = new SortStyle("ID", _ID);        public static final SortStyle SEVERITY = new SortStyle("SEVERITY", _SEVERITY);        public static final SortStyle NODE = new SortStyle("NODE", _NODE);        public static final SortStyle INTERFACE = new SortStyle("INTERFACE", _INTERFACE);        public static final SortStyle SERVICE = new SortStyle("SERVICE", _SERVICE);        public static final SortStyle CREATE_TIME = new SortStyle("CREATE_TIME", _CREATE_TIME);        public static final SortStyle RESOLVED_TIME = new SortStyle("RESOLVED_TIME", _RESOLVED_TIME);        public static final SortStyle PORT = new SortStyle("PORT", _PORT);        public static final SortStyle PROTOCOL = new SortStyle("PROTOCOL", _PROTOCOL);        public static final int _REVERSE_ID = 101;        public static final int _REVERSE_SEVERITY = 102;        public static final int _REVERSE_NODE = 103;        public static final int _REVERSE_INTERFACE = 104;        public static final int _REVERSE_SERVICE = 105;        public static final int _REVERSE_CREATE_TIME = 106;        public static final int _REVERSE_RESOLVED_TIME = 107;        public static final int _REVERSE_PORT = 108;        public static final int _REVERSE_PROTOCOL = 109;        public static final SortStyle REVERSE_ID = new SortStyle("REVERSE_ID", _REVERSE_ID);        public static final SortStyle REVERSE_SEVERITY = new SortStyle("REVERSE_SEVERITY", _REVERSE_SEVERITY);        public static final SortStyle REVERSE_NODE = new SortStyle("REVERSE_NODE", _REVERSE_NODE);        public static final SortStyle REVERSE_INTERFACE = new SortStyle("REVERSE_INTERFACE", _REVERSE_INTERFACE);        public static final SortStyle REVERSE_SERVICE = new SortStyle("REVERSE_SERVICE", _REVERSE_SERVICE);        public static final SortStyle REVERSE_CREATE_TIME = new SortStyle("REVERSE_CREATE_TIME", _REVERSE_CREATE_TIME);        public static final SortStyle REVERSE_RESOLVED_TIME = new SortStyle("REVERSE_RESOLVED_TIME", _REVERSE_RESOLVED_TIME);        public static final SortStyle REVERSE_PORT = new SortStyle("REVERSE_PORT", _REVERSE_PORT);        public static final SortStyle REVERSE_PROTOCOL = new SortStyle("REVERSE_PROTOCOL", _REVERSE_PROTOCOL);        protected String name;        protected int id;        private SortStyle(String name, int id) {            this.name = name;            this.id = id;        }        public String toString() {            return "Event.SortStyle." + this.name;        }        public String getName() {            return this.name;        }        public int getId() {            return this.id;        }    }    /** Convenience class to determine what sort of events to include in a query. */    public static class ResolutionType extends Object {        /* CORBA-style enumeration */        public static final int _OPEN = 1;        public static final int _RESOLVED = 2;        public static final int _BOTH = 3;        public static final ResolutionType OPEN = new ResolutionType("OPEN", _OPEN);        public static final ResolutionType RESOLVED = new ResolutionType("RESOLVED", _RESOLVED);        public static final ResolutionType BOTH = new ResolutionType("BOTH", _BOTH);        protected String name;        protected int id;        private ResolutionType(String name, int id) {            this.name = name;            this.id = id;        }        public String toString() {            return "Vulnerability.ResolutionType." + this.name;        }        public String getName() {            return this.name;        }        public int getId() {            return this.id;        }    }    protected static final Category log = ThreadCategory.getInstance("OutageFactory");    /** Private constructor so this class cannot be instantiated. */    private VulnerabilityFactory() {    }    /**     * Count all open vulnerabilities.     */    public static int getVulnerabilityCount() throws SQLException {        return getVulnerabilityCount(ResolutionType.OPEN, new Filter[0]);    }    /**     * Count the number of vulnerabilities for a given resolution type and given     * filters..     */    public static int getVulnerabilityCount(ResolutionType resType, Filter[] filters) throws SQLException {        if (resType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        int vulCount = 0;        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT COUNT(*) AS VULCOUNT FROM VULNERABILITIES LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE ");            select.append(getResolutionTypeClause(resType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND");                select.append(filters[i].getSql());            }            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());            if (rs.next()) {                vulCount = rs.getInt("VULCOUNT");            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return vulCount;    }    /** Return a specific vulnerability. */    public static Vulnerability getVulnerability(int vulId) throws SQLException {        Vulnerability vul = null;        Connection conn = Vault.getDbConnection();        try {            PreparedStatement stmt = conn.prepareStatement("SELECT VULNERABILITIES.*, NODE.NODELABEL, SERVICE.SERVICENAME FROM VULNERABILITIES LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE VULNERABILITYID=? ");            stmt.setInt(1, vulId);            ResultSet rs = stmt.executeQuery();            Vulnerability[] vuls = rs2Vulnerabilities(rs);            // what do I do if this actually returns more than one            // vulnerability?            if (vuls.length > 0) {                vul = vuls[0];            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return vul;    }    /*     * ****************************************************************** N O D     * E M E T H O D S     * ******************************************************************     */    /**     * Return some maximum number of vulnerabilities or less sorted by the given     * sort style for the given node.     *      * @param throttle     *            a value less than one means no throttling     */    public static Vulnerability[] getVulnerabilitiesForNode(int nodeId, SortStyle sortStyle, ResolutionType resType, int throttle, int offset) throws SQLException {        if (sortStyle == null || resType == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Filter[] filters = new Filter[] { new NodeFilter(nodeId) };        return (VulnerabilityFactory.getVulnerabilities(sortStyle, resType, filters, throttle, offset));    }    /**     * Return the number of vulnerabilities for this node and the given     * resolution type.     */    public static int getVulnerabilityCountForNode(int nodeId, ResolutionType resType) throws SQLException {        if (resType == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Filter[] filters = new Filter[] { new NodeFilter(nodeId) };        return (getVulnerabilityCount(resType, filters));    }    /*     * ****************************************************************** I N T     * E R F A C E M E T H O D S     * ******************************************************************     */    /**     * Return some maximum number of events or less (optionally only     * unacknowledged events) sorted by the given sort style for the given IP     * address.     *      * @param throttle     *            a value less than one means no throttling     * @param offset     *            which row to start on in the result list     */    public static Vulnerability[] getVulnerablilitiesForInterface(int nodeId, String ipAddress, SortStyle sortStyle, ResolutionType resType, int throttle, int offset) throws SQLException {        if (ipAddress == null || sortStyle == null || resType == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        Filter[] filters = new Filter[] { new NodeFilter(nodeId), new InterfaceFilter(ipAddress) };        return (VulnerabilityFactory.getVulnerabilities(sortStyle, resType, filters, throttle, offset));    }    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?