diff --git a/.gitignore b/.gitignore index 733bcac..0ae2ec7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ target/ .project .classpath .settings/ +.idea/ +*.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml index b5e95b9..f419fc2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,24 +3,20 @@ 4.0.0 com.redhat.trie PathPacker - 0.0.3 + 0.0.4 bundle - - 3.0 - - junit junit - 4.11 + 4.13 test org.bouncycastle - bcprov-jdk16 - 1.46 + bcprov-jdk15on + 1.64 org.apache.maven.surefire @@ -28,51 +24,44 @@ 2.17 - - log4j - log4j - 1.2.17 - compile - - - - com.sun.jdmk - jmxtools - - - com.sun.jmx - jmxri - - - javax.jms - jms - - - javax.mail - mail - - - - + + org.apache.maven.plugins + maven-enforcer-plugin + 3.0.0-M3 + + + enforce-maven + + enforce + + + + + 3.0 + + + + + + + maven-compiler-plugin - 2.3.2 + 3.8.1 - 1.6 - 1.6 + 1.8 + 1.8 org.apache.maven.plugins maven-javadoc-plugin + 3.1.1 @@ -117,7 +106,7 @@ org.apache.felix maven-bundle-plugin - 2.4.0 + 4.2.1 true diff --git a/src/main/java/com/redhat/trie/HuffNode.java b/src/main/java/com/redhat/trie/HuffNode.java index 9f089d4..dfbd1c8 100644 --- a/src/main/java/com/redhat/trie/HuffNode.java +++ b/src/main/java/com/redhat/trie/HuffNode.java @@ -146,26 +146,22 @@ public class HuffNode { * get a String of the bits, that map to Object need */ private String getBitPath(HuffNode trie, Object need) { - HuffNode left = trie.getLeft(); - HuffNode right = trie.getRight(); - if (left != null && left.getValue() != null) { - if (need.equals(left.getValue())) { - return "0"; - } + HuffNode leftNode = trie.getLeft(); + HuffNode rightNode = trie.getRight(); + if (leftNode != null && leftNode.getValue() != null && need.equals(leftNode.getValue())) { + return "0"; } - if (right != null && right.getValue() != null) { - if (need.equals(right.getValue())) { - return "1"; - } + if (rightNode != null && rightNode.getValue() != null && need.equals(rightNode.getValue())) { + return "1"; } - if (left != null) { - String leftPath = getBitPath(left, need); + if (leftNode != null) { + String leftPath = getBitPath(leftNode, need); if (leftPath.length() > 0) { return "0" + leftPath; } } - if (right != null) { - String rightPath = getBitPath(right, need); + if (rightNode != null) { + String rightPath = getBitPath(rightNode, need); if (rightPath.length() > 0) { return "1" + rightPath; } diff --git a/src/main/java/com/redhat/trie/NodePair.java b/src/main/java/com/redhat/trie/NodePair.java index 93308d4..bfeb40e 100644 --- a/src/main/java/com/redhat/trie/NodePair.java +++ b/src/main/java/com/redhat/trie/NodePair.java @@ -56,7 +56,7 @@ public class NodePair implements Comparable { * @return true if it has at least one connection and false otherwise. */ public boolean hasNoChildren() { - return getConnection().getChildren().size() == 0; + return getConnection().getChildren().isEmpty(); } } diff --git a/src/main/java/com/redhat/trie/PathNode.java b/src/main/java/com/redhat/trie/PathNode.java index fbddf56..676d747 100644 --- a/src/main/java/com/redhat/trie/PathNode.java +++ b/src/main/java/com/redhat/trie/PathNode.java @@ -16,12 +16,12 @@ package com.redhat.trie; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.HashSet; -import java.util.Collections; - -import org.apache.log4j.Logger; +import java.util.logging.Level; +import java.util.logging.Logger; /** * PathNode is the relationship to an item in the path tree. @@ -31,7 +31,7 @@ import org.apache.log4j.Logger; * The Name of a given PathNode, is inferred by the NodePair that regards this PathNode as its "connection" */ public class PathNode { - private static org.apache.log4j.Logger log = Logger.getLogger(PathTree.class); + private static Logger log = Logger.getLogger(PathTree.class.getCanonicalName()); private long id = 0; private List children = new ArrayList(); private List parents = new ArrayList(); @@ -161,14 +161,11 @@ public class PathNode { * Traverse up the tree, and get the highest ancestor PathNode, for node. */ public PathNode getStartNode(PathNode node) { - if (node.getParents().size() == 0) { + if (node.getParents().isEmpty()) { return node; // this is the end! + } else { + return node.getStartNode(node.getParents().get(0)); } - - for (PathNode parent : node.getParents()) { - return node.getStartNode(parent); - } - return node; // when in doubt, return yourself } /** @@ -182,13 +179,11 @@ public class PathNode { * Traverse down the tree, and get the "endMarker" child, for node. */ public PathNode getEndNode(PathNode node) { - if (node.getChildren().size() == 0) { + if (node.getChildren().isEmpty()) { return node; // this is the end! + } else { + return node.getEndNode(node.getChildren().get(0).getConnection()); } - for (NodePair child : node.getChildren()) { - return node.getEndNode(child.getConnection()); - } - return node; // when in doubt, return yourself } /** @@ -233,7 +228,7 @@ public class PathNode { public boolean includes(PathNode that) { // if we are at the end of the tree we're checking against, // then it includes everything up to this point. - if (this.getChildren().size() == 0 || that.getChildren().size() == 0) { + if (this.getChildren().isEmpty() || that.getChildren().isEmpty()) { return true; } @@ -248,8 +243,8 @@ public class PathNode { thisnp.getName().startsWith("$") || thisnp.getName().equals(thatnp.getName()) ) { result = thisnp.getConnection().includes(thatnp.getConnection()); - found.add(new Boolean(result).booleanValue()); - log.debug("includes: this: " + thisnp.getName() + " == that:" + thatnp.getName()); + found.add(result); + log.log(Level.FINE, () -> "includes: this: " + thisnp.getName() + " == that:" + thatnp.getName()); } } } @@ -262,12 +257,11 @@ public class PathNode { * pretty information */ public String toString() { - String parentList = ""; + StringBuilder parentList = new StringBuilder(); for (PathNode parent : parents) { - parentList += ": " + parent.getId(); + parentList.append(": " + parent.getId()); } - parentList += ""; - return "ID: " + id + ", Name: " + this.getName() + ", Parents" + parentList + ", Children: " + children; + return "ID: " + id + ", Name: " + this.getName() + ", Parents" + parentList.toString() + ", Children: " + children; } } diff --git a/src/main/java/com/redhat/trie/PathTree.java b/src/main/java/com/redhat/trie/PathTree.java index 77ce089..99cbc81 100644 --- a/src/main/java/com/redhat/trie/PathTree.java +++ b/src/main/java/com/redhat/trie/PathTree.java @@ -15,26 +15,23 @@ package com.redhat.trie; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.HashSet; -import java.util.Map; -import java.util.HashMap; -import java.util.StringTokenizer; - -import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; - -import java.util.zip.Inflater; -import java.util.zip.InflaterOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; -import java.util.zip.DataFormatException; - -import org.apache.log4j.Logger; +import java.util.zip.Inflater; +import java.util.zip.InflaterOutputStream; /** * PathTree @@ -42,10 +39,10 @@ import org.apache.log4j.Logger; * An efficient means by which to check the content sets. */ public class PathTree { - private static org.apache.log4j.Logger log = Logger.getLogger(PathTree.class); + private static Logger log = Logger.getLogger(PathTree.class.getCanonicalName()); private List nodeDictionary; private List pathDictionary; - private StringBuffer nodeBits; + private StringBuilder nodeBits; private byte[] payload; @@ -95,11 +92,7 @@ public class PathTree { * Constructor using the list of content sets. */ public PathTree(List contentSets) throws PayloadException { - try { - setContentSets(contentSets); - } catch (PayloadException ex) { - throw ex; - } + setContentSets(contentSets); } /** @@ -150,13 +143,13 @@ public class PathTree { /** * the buffer of significant bits, with regard to how many nodes there are. * - * @return StringBuffer of + * @return StringBuilder of */ - private StringBuffer getNodeBits() { + private StringBuilder getNodeBits() { return this.nodeBits; } - private void setNodeBits(StringBuffer nodeBits) { + private void setNodeBits(StringBuilder nodeBits) { this.nodeBits = nodeBits; } @@ -211,11 +204,11 @@ public class PathTree { } if (this.modified || this.pathDictionary == null || this.nodeDictionary == null) { this.nodeDictionary = new ArrayList(); - this.setNodeBits(new StringBuffer()); + this.setNodeBits(new StringBuilder()); ByteArrayInputStream bais = new ByteArrayInputStream(this.getPayload(), - (new Long(this.getDictOffset())).intValue(), - (new Long(this.getPayload().length - this.getDictOffset()).intValue())); + ((int)(this.getDictOffset())), + ((int) (this.getPayload().length - this.getDictOffset()))); int value = bais.read(); // check for size bits this.setNodeCount(value); @@ -261,11 +254,7 @@ public class PathTree { * @throws PayloadException if the newly read PathNode dictionary can not be read from the payload */ private HuffNode getPathTrie() throws PayloadException { - try { - return makeTrie(getPathDictionary()); - } catch (PayloadException ex) { - throw ex; - } + return makeTrie(getPathDictionary()); } /** @@ -275,11 +264,7 @@ public class PathTree { * @throws PayloadException if the newly read Node name dictionary can not be read from the payload */ private HuffNode getNodeTrie() throws PayloadException { - try { - return makeTrie(getNodeDictionary()); - } catch (PayloadException ex) { - throw ex; - } + return makeTrie(getNodeDictionary()); } /** @@ -287,17 +272,12 @@ public class PathTree { */ public PathNode getRootPathNode() throws PayloadException { // populate the PathNodes so we can rebuild the cool url tree - Set pathNodes; - try { - pathNodes = populatePathNodes(getNodeDictionary(), + Set pathNodes = populatePathNodes(getNodeDictionary(), getPathTrie(), getNodeTrie(), getNodeBits()); - } catch (PayloadException ex) { - throw ex; - } // find the root, he has no parents PathNode root = null; for (PathNode pn : pathNodes) { - if (pn.getParents().size() == 0) { + if (pn.getParents().isEmpty()) { root = pn; break; } @@ -316,7 +296,7 @@ public class PathTree { try { rootPathNode = getRootPathNode(); } catch(PayloadException pe) { - log.error(pe); + log.log(Level.SEVERE,"Payload Exception", pe); return false; } return test(contentPath, rootPathNode); @@ -336,7 +316,7 @@ public class PathTree { /* Request is of the form "/content/rc/rhel/7/..." * Grab the next element. */ - log.debug("test(" + request + ")"); + log.log(Level.FINE, () -> "test(" + request + ")"); StringTokenizer tokenizer = new StringTokenizer(request, PATH_DELIMITER); if(tokenizer.countTokens() == 0) { return false; @@ -344,12 +324,11 @@ public class PathTree { String currentToken = tokenizer.nextToken(); for(NodePair nodePair: tree.getChildren()) { String nodePairName = nodePair.getName(); - log.debug("Current token: [" + currentToken + "] =??= NodePair name: [" + nodePairName + "]"); + log.log(Level.FINE, () -> "Current token: [" + currentToken + "] =??= NodePair name: [" + nodePairName + "]"); if(currentToken.equals(nodePairName) || nodePairName.startsWith(CONTENT_PATH_VARIABLE_PREFIX)) { if(nodePair.hasNoChildren()) { return true; } else { - String s = PATH_DELIMITER + currentToken; boolean retval = test(request.substring(currentToken.length()+1), nodePair.getConnection()); if(retval) { return true; @@ -377,7 +356,7 @@ public class PathTree { PathNode treeRoot = PathTree.makePathTree(contentSets, new PathNode()); List nodeStrings = orderStrings(treeRoot); - if (nodeStrings.size() == 0) { + if (nodeStrings.isEmpty()) { this.payload = new byte[0]; return; } @@ -429,14 +408,14 @@ public class PathTree { private List byteArrayToStringList(byte[] ba) { List strings = new ArrayList(); - String str = ""; + StringBuilder str = new StringBuilder(); for (byte b : ba) { if (b == '\0') { - strings.add(str); - str = ""; + strings.add(str.toString()); + str.setLength(0); } else { - str += (char) b; + str.append((char) b); } } return strings; @@ -475,9 +454,9 @@ public class PathTree { * @return the Set of weighted PathNode */ private Set populatePathNodes(List thisNodeDictionary, - HuffNode pathTrie, HuffNode nodeTrie, StringBuffer theseNodeBits) { + HuffNode pathTrie, HuffNode nodeTrie, StringBuilder theseNodeBits) { Set pathNodes = new HashSet(); - StringBuffer myNodeBits = new StringBuffer(theseNodeBits.toString()); + StringBuilder myNodeBits = new StringBuilder(theseNodeBits.toString()); for (HuffNode node : thisNodeDictionary) { pathNodes.add((PathNode) node.getValue()); boolean stillNode = true; @@ -485,7 +464,7 @@ public class PathTree { // get first child name // if its HuffNode.END_NODE we are done String nameValue = null; - StringBuffer nameBits = new StringBuffer(); + StringBuilder nameBits = new StringBuilder(); while (nameValue == null && stillNode) { nameBits.append(myNodeBits.charAt(0)); myNodeBits.deleteCharAt(0); @@ -503,7 +482,7 @@ public class PathTree { } PathNode nodeValue = null; - StringBuffer pathBits = new StringBuffer(); + StringBuilder pathBits = new StringBuilder(); while (nodeValue == null && stillNode) { pathBits.append(myNodeBits.charAt(0)); myNodeBits.deleteCharAt(0); @@ -530,7 +509,7 @@ public class PathTree { */ public List toList() { List urls = new ArrayList(); - StringBuffer aPath = new StringBuffer(); + StringBuilder aPath = new StringBuilder(); try { makeURLs(getRootPathNode(), urls, aPath); } catch (PayloadException ex) { @@ -539,12 +518,12 @@ public class PathTree { return urls; } - private void makeURLs(PathNode root, List urls, StringBuffer aPath) { - if (root.getChildren().size() == 0) { + private void makeURLs(PathNode root, List urls, StringBuilder aPath) { + if (root.getChildren().isEmpty()) { urls.add(aPath.toString()); } for (NodePair child : root.getChildren()) { - StringBuffer childPath = new StringBuffer(aPath.substring(0)); + StringBuilder childPath = new StringBuilder(aPath.substring(0)); childPath.append("/"); childPath.append(child.getName()); makeURLs(child.getConnection(), urls, childPath); @@ -572,9 +551,8 @@ public class PathTree { private HuffNode mergeNodes(HuffNode node1, HuffNode node2) { HuffNode left = node1; HuffNode right = node2; - HuffNode parent = new HuffNode(getHuffNodeContext(), + return new HuffNode(getHuffNodeContext(), null, left.getWeight() + right.getWeight(), left, right); - return parent; } private List getStringNodeList(List pathStrings) { @@ -603,13 +581,13 @@ public class PathTree { * @return deflated byte array */ private byte[] byteProcess(List entries) - throws IOException, UnsupportedEncodingException { + throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION)); for (String segment : entries) { - dos.write(segment.getBytes("UTF-8")); - dos.write("\0".getBytes("UTF-8")); + dos.write(segment.getBytes(StandardCharsets.UTF_8)); + dos.write("\0".getBytes(StandardCharsets.UTF_8)); } dos.finish(); dos.close(); @@ -622,11 +600,11 @@ public class PathTree { Map segments = new HashMap(); Set nodes = new HashSet(); buildSegments(segments, nodes, parent); - for (String part : segments.keySet()) { - if (!part.equals("")) { - int count = segments.get(part); - if (parts.size() == 0) { - parts.add(part); + for (Map.Entry part : segments.entrySet()) { + if (!part.getKey().equals("")) { + int count = part.getValue(); + if (parts.isEmpty()) { + parts.add(part.getKey()); } else { int pos = parts.size(); @@ -636,7 +614,7 @@ public class PathTree { break; } } - parts.add(pos, part); + parts.add(pos, part.getKey()); } } } @@ -654,10 +632,7 @@ public class PathTree { boolean start = false; /* TODO ??? */ for (byte b : toByteArray(nodeSize)) { - if (b == 0 && !start) { - continue; - } - else { + if (!(b == 0 && !start)) { countBaos.write(b); start = true; } @@ -673,7 +648,7 @@ public class PathTree { else { baos.write(nodeSize); } - StringBuffer bits = new StringBuffer(); + StringBuilder bits = new StringBuilder(); String endNodeLocationBitPath = stringParent.getBitPath(HuffNode.END_NODE); for (PathNode pn : pathNodes) { for (NodePair np : pn.getChildren()) { @@ -726,7 +701,7 @@ public class PathTree { Set nodes = treeRoot.getAllNodes(); for (PathNode pn : nodes) { int count = pn.getParents().size(); - if (nodes.size() == 0) { + if (nodes.isEmpty()) { nodes.add(pn); } else { @@ -786,7 +761,7 @@ public class PathTree { for (NodePair np : parent.getChildren()) { Integer count = segments.get(np.getName()); if (count == null) { - count = new Integer(0); + count = 0; } segments.put(np.getName(), ++count); buildSegments(segments, nodes, np.getConnection()); @@ -814,7 +789,6 @@ public class PathTree { // "equivalent" parents are merged List parentResult = new ArrayList(); parentResult.addAll(location.getParents()); - //log.debug(location); for (PathNode parent1 : location.getParents()) { if (!parentResult.contains(parent1)) { continue; diff --git a/src/main/java/com/redhat/trie/PayloadException.java b/src/main/java/com/redhat/trie/PayloadException.java index 7fad097..8488630 100644 --- a/src/main/java/com/redhat/trie/PayloadException.java +++ b/src/main/java/com/redhat/trie/PayloadException.java @@ -15,8 +15,6 @@ package com.redhat.trie; -import java.lang.Exception; - public class PayloadException extends Exception { } diff --git a/src/test/java/com/redhat/trie/TestHelpers.java b/src/test/java/com/redhat/trie/TestHelpers.java index 9b8d051..af70b6b 100644 --- a/src/test/java/com/redhat/trie/TestHelpers.java +++ b/src/test/java/com/redhat/trie/TestHelpers.java @@ -1,35 +1,33 @@ package com.redhat.trie; -import java.io.InputStreamReader; +import org.junit.Test; + import java.io.BufferedReader; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.IOException; - -import java.util.List; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; +import java.util.logging.Logger; -import static org.junit.Assert.fail; import static org.junit.Assert.assertNotNull; - -import org.junit.Test; -import org.apache.log4j.Logger; +import static org.junit.Assert.fail; /** * This class is just to provide helpers for the other tests */ public class TestHelpers { - private static org.apache.log4j.Logger log = Logger.getLogger(TestHelpers.class); + private static Logger log = Logger.getLogger(TestHelpers.class.getCanonicalName()); /** * junit requires at least one runnable test */ @Test public void testDeadVicker() { - assertNotNull(new String("What's its diocese?")); + assertNotNull("What's its diocese?"); } // Helpers @@ -98,7 +96,7 @@ public class TestHelpers { br = new BufferedReader(new InputStreamReader(in)); } catch(NullPointerException npe) { // can happen in the case of an empty content set list - log.warn(">>>>>>> Empty content set <<<<<"); + log.fine(">>>>>>> Empty content set <<<<<"); return contentList; } diff --git a/src/test/java/com/redhat/trie/TestValidater.java b/src/test/java/com/redhat/trie/TestValidater.java index 0fecf76..0765a91 100644 --- a/src/test/java/com/redhat/trie/TestValidater.java +++ b/src/test/java/com/redhat/trie/TestValidater.java @@ -1,19 +1,17 @@ package com.redhat.trie; -import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; import java.util.List; -import org.apache.log4j.Logger; -import org.apache.log4j.Level; - -import org.junit.Test; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Tests of whether a URL should be granted access based on a given content set