X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FIngressRoute.java;h=033b39003a1aece2c0c4c1144ea16cef62f3079f;hb=85b8739b9f1ebdf9731b134ac2b2796c0e2e5178;hp=0de57df2f604113699ba30b0bbfdef3782ec1741;hpb=4261823d84c2b911b68cdf4cb4dc3be429ebe285;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java index 0de57df2..033b3900 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java @@ -52,6 +52,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $ */ public class IngressRoute extends NodeClass implements Comparable { + private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); private final int seq; private final int feedid; @@ -76,7 +77,8 @@ public class IngressRoute extends NodeClass implements Comparable * @return a set of IngressRoutes */ public static Set getIngressRoutesForSeq(int seq) { - return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); + return getAllIngressRoutesForSQL( + "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); } private static SortedSet getAllIngressRoutesForSQL(String sql) { @@ -85,21 +87,21 @@ public class IngressRoute extends NodeClass implements Comparable DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - int seq = rs.getInt("SEQUENCE"); - int feedid = rs.getInt("FEEDID"); - String user = rs.getString("USERID"); - String subnet = rs.getString("SUBNET"); - int nodeset = rs.getInt("NODESET"); - set.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + int seq = rs.getInt("SEQUENCE"); + int feedid = rs.getInt("FEEDID"); + String user = rs.getString("USERID"); + String subnet = rs.getString("SUBNET"); + int nodeset = rs.getInt("NODESET"); + set.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } return set; } @@ -128,16 +130,16 @@ public class IngressRoute extends NodeClass implements Comparable DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - if (rs.next()) { - rv = rs.getInt("MAX"); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + if (rs.next()) { + rv = rs.getInt("MAX"); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } return rv; } @@ -146,7 +148,7 @@ public class IngressRoute extends NodeClass implements Comparable * Get an Ingress Route for a particular feed ID, user, and subnet * * @param feedid the Feed ID to look for - * @param user the user name to look for + * @param user the user name to look for * @param subnet the subnet to look for * @return the Ingress Route, or null of there is none */ @@ -162,22 +164,24 @@ public class IngressRoute extends NodeClass implements Comparable ps.setInt(1, feedid); ps.setString(2, user); ps.setString(3, subnet); - ResultSet rs = ps.executeQuery(); - if (rs.next()) { - int seq = rs.getInt("SEQUENCE"); - int nodeset = rs.getInt("NODESET"); - v = new IngressRoute(seq, feedid, user, subnet, nodeset); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + int seq = rs.getInt("SEQUENCE"); + int nodeset = rs.getInt("NODESET"); + v = new IngressRoute(seq, feedid, user, subnet, nodeset); + } } - rs.close(); ps.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return v; @@ -191,33 +195,26 @@ public class IngressRoute extends NodeClass implements Comparable */ public static Collection getIngressRoute(int seq) { Collection rv = new ArrayList(); - PreparedStatement ps = null; try { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?"; - ps = conn.prepareStatement(sql); - ps.setInt(1, seq); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - int feedid = rs.getInt("FEEDID"); - String user = rs.getString("USERID"); - String subnet = rs.getString("SUBNET"); - int nodeset = rs.getInt("NODESET"); - rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, seq); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + int feedid = rs.getInt("FEEDID"); + String user = rs.getString("USERID"); + String subnet = rs.getString("SUBNET"); + int nodeset = rs.getInt("NODESET"); + rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); + } + } } - rs.close(); - ps.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); - } finally { - try { - ps.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + intlogger.error("SQLException " + e.getMessage()); } return rv; } @@ -244,12 +241,14 @@ public class IngressRoute extends NodeClass implements Comparable this.subnet = (subnet == null) ? "-" : subnet; this.nodelist = -1; this.nodes = null; - if (Feed.getFeedById(feedid) == null) + if (Feed.getFeedById(feedid) == null) { throw new IllegalArgumentException("No such feed: " + feedid); + } if (!this.subnet.equals("-")) { SubnetMatcher sm = new SubnetMatcher(subnet); - if (!sm.isValid()) + if (!sm.isValid()) { throw new IllegalArgumentException("Invalid subnet: " + subnet); + } } } @@ -263,37 +262,41 @@ public class IngressRoute extends NodeClass implements Comparable this.nodelist = -1; this.nodes = new TreeSet(); JSONArray ja = jo.getJSONArray("node"); - for (int i = 0; i < ja.length(); i++) + for (int i = 0; i < ja.length(); i++) { this.nodes.add(ja.getString(i)); + } } /** - * Does this particular IngressRoute match a request, represented by feedid and req? - * To match, feedid must match the feed ID in the route, the user in the route - * (if specified) must match the user in the request, and the subnet in the route (if specified) - * must match the subnet from the request. + * Does this particular IngressRoute match a request, represented by feedid and req? To match, feedid must + * match the feed ID in the route, the user in the route (if specified) must match the user in the request, and the + * subnet in the route (if specified) must match the subnet from the request. * * @param feedid the feedid for this request - * @param req the remainder of the request + * @param req the remainder of the request * @return true if a match, false otherwise */ public boolean matches(int feedid, HttpServletRequest req) { // Check feedid - if (this.feedid != feedid) + if (this.feedid != feedid) { return false; + } // Get user from request and compare // Note: we don't check the password; the node will do that if (userid.length() > 0 && !userid.equals("-")) { String credentials = req.getHeader("Authorization"); - if (credentials == null || !credentials.startsWith("Basic ")) + if (credentials == null || !credentials.startsWith("Basic ")) { return false; + } String t = new String(Base64.decodeBase64(credentials.substring(6))); int ix = t.indexOf(':'); - if (ix >= 0) + if (ix >= 0) { t = t.substring(0, ix); - if (!t.equals(this.userid)) + } + if (!t.equals(this.userid)) { return false; + } } // If this route has a subnet, match it against the requester's IP addr @@ -310,10 +313,11 @@ public class IngressRoute extends NodeClass implements Comparable } /** - * Compare IP addresses as byte arrays to a subnet specified as a CIDR. - * Taken from org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat. + * Compare IP addresses as byte arrays to a subnet specified as a CIDR. Taken from + * org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat. */ public class SubnetMatcher { + private byte[] sn; private int len; private int mask; @@ -386,31 +390,23 @@ public class IngressRoute extends NodeClass implements Comparable private Collection readNodes() { Collection set = new TreeSet(); - PreparedStatement ps = null; try { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); String sql = "select NODEID from NODESETS where SETID = ?"; - ps = conn.prepareStatement(sql); - ps.setInt(1, nodelist); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - int id = rs.getInt("NODEID"); - set.add(lookupNodeID(id)); + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, nodelist); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + int id = rs.getInt("NODEID"); + set.add(lookupNodeID(id)); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); - } finally { - try { - ps.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + intlogger.error("SQLException " + e.getMessage()); } return set; } @@ -438,12 +434,13 @@ public class IngressRoute extends NodeClass implements Comparable } catch (SQLException e) { rv = false; intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -468,7 +465,8 @@ public class IngressRoute extends NodeClass implements Comparable } // Create the INGRESS_ROUTES row - ps = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)"); + ps = c.prepareStatement( + "insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)"); ps.setInt(1, this.seq); ps.setInt(2, this.feedid); ps.setString(3, this.userid); @@ -479,12 +477,13 @@ public class IngressRoute extends NodeClass implements Comparable rv = true; } catch (SQLException e) { intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -500,10 +499,12 @@ public class IngressRoute extends NodeClass implements Comparable JSONObject jo = new JSONObject(); jo.put("feedid", feedid); // Note: for user and subnet, null, "", and "-" are equivalent - if (userid != null && !userid.equals("-") && !userid.equals("")) + if (userid != null && !userid.equals("-") && !userid.equals("")) { jo.put("user", userid); - if (subnet != null && !subnet.equals("-") && !subnet.equals("")) + } + if (subnet != null && !subnet.equals("-") && !subnet.equals("")) { jo.put("subnet", subnet); + } jo.put("seq", seq); jo.put("node", nodes); return jo; @@ -511,7 +512,8 @@ public class IngressRoute extends NodeClass implements Comparable @Override public String getKey() { - return String.format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); + return String + .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); } @Override @@ -522,8 +524,9 @@ public class IngressRoute extends NodeClass implements Comparable @Override public boolean equals(Object obj) { try { - if (!(obj instanceof IngressRoute)) + if (!(obj instanceof IngressRoute)) { return false; + } return this.compareTo((IngressRoute) obj) == 0; } catch (NullPointerException e) { return false; @@ -532,25 +535,31 @@ public class IngressRoute extends NodeClass implements Comparable @Override public int compareTo(IngressRoute in) { - if (in == null) + if (in == null) { throw new NullPointerException(); + } int n = this.feedid - in.feedid; - if (n != 0) + if (n != 0) { return n; + } n = this.seq - in.seq; - if (n != 0) + if (n != 0) { return n; + } n = this.userid.compareTo(in.userid); - if (n != 0) + if (n != 0) { return n; + } n = this.subnet.compareTo(in.subnet); - if (n != 0) + if (n != 0) { return n; + } return this.nodes.equals(in.nodes) ? 0 : 1; } @Override public String toString() { - return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); + return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, + (subnet == null) ? "" : subnet, seq); } }