mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-17 15:03:18 +00:00
[DS-2463] Tidy up some suggestions.
This commit is contained in:
@@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class SpiderDetector {
|
public class SpiderDetector {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(SpiderDetector.class);
|
private static final Logger log = LoggerFactory.getLogger(SpiderDetector.class);
|
||||||
|
|
||||||
private static Boolean useProxies;
|
private static Boolean useProxies;
|
||||||
|
|
||||||
@@ -43,10 +43,12 @@ public class SpiderDetector {
|
|||||||
private static IPTable table = null;
|
private static IPTable table = null;
|
||||||
|
|
||||||
/** Collection of regular expressions to match known spiders' agents. */
|
/** Collection of regular expressions to match known spiders' agents. */
|
||||||
private static List<Pattern> agents = Collections.synchronizedList(new ArrayList<Pattern>());
|
private static final List<Pattern> agents
|
||||||
|
= Collections.synchronizedList(new ArrayList<Pattern>());
|
||||||
|
|
||||||
/** Collection of regular expressions to match known spiders' domain names. */
|
/** Collection of regular expressions to match known spiders' domain names. */
|
||||||
private static List<Pattern> domains = Collections.synchronizedList(new ArrayList<Pattern>());
|
private static final List<Pattern> domains
|
||||||
|
= Collections.synchronizedList(new ArrayList<Pattern>());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility method which reads lines from a file & returns them in a Set.
|
* Utility method which reads lines from a file & returns them in a Set.
|
||||||
@@ -58,7 +60,7 @@ public class SpiderDetector {
|
|||||||
public static Set<String> readPatterns(File patternFile)
|
public static Set<String> readPatterns(File patternFile)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Set<String> patterns = new HashSet<String>();
|
Set<String> patterns = new HashSet<>();
|
||||||
|
|
||||||
if (!patternFile.exists() || !patternFile.isFile())
|
if (!patternFile.exists() || !patternFile.isFile())
|
||||||
{
|
{
|
||||||
@@ -66,21 +68,22 @@ public class SpiderDetector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Read our file & get all them patterns.
|
//Read our file & get all them patterns.
|
||||||
BufferedReader in = new BufferedReader(new FileReader(patternFile));
|
try (BufferedReader in = new BufferedReader(new FileReader(patternFile)))
|
||||||
String line;
|
{
|
||||||
while ((line = in.readLine()) != null) {
|
String line;
|
||||||
if (!line.startsWith("#")) {
|
while ((line = in.readLine()) != null) {
|
||||||
line = line.trim();
|
if (!line.startsWith("#")) {
|
||||||
|
line = line.trim();
|
||||||
|
|
||||||
if (!line.equals("")) {
|
if (!line.equals("")) {
|
||||||
patterns.add(line);
|
patterns.add(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// ua.add(line.replaceFirst("#","").replaceFirst("UA","").trim());
|
||||||
|
// ... add this functionality later
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// ua.add(line.replaceFirst("#","").replaceFirst("UA","").trim());
|
|
||||||
// ... add this functionality later
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
return patterns;
|
return patterns;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +137,7 @@ public class SpiderDetector {
|
|||||||
log.info("No spider file loaded");
|
log.info("No spider file loaded");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (IOException | IPTable.IPFormatException e) {
|
||||||
log.error("Error Loading Spiders:" + e.getMessage(), e);
|
log.error("Error Loading Spiders:" + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +203,7 @@ public class SpiderDetector {
|
|||||||
{
|
{
|
||||||
// See if any agent patterns match
|
// See if any agent patterns match
|
||||||
if (null != agent)
|
if (null != agent)
|
||||||
{
|
{
|
||||||
synchronized(agents)
|
synchronized(agents)
|
||||||
{
|
{
|
||||||
if (agents.isEmpty())
|
if (agents.isEmpty())
|
||||||
@@ -233,7 +236,7 @@ public class SpiderDetector {
|
|||||||
// No. See if any DNS names match
|
// No. See if any DNS names match
|
||||||
if (null != hostname)
|
if (null != hostname)
|
||||||
{
|
{
|
||||||
synchronized(domains)
|
synchronized(domains)
|
||||||
{
|
{
|
||||||
if (domains.isEmpty())
|
if (domains.isEmpty())
|
||||||
loadPatterns("domains", domains);
|
loadPatterns("domains", domains);
|
||||||
@@ -293,14 +296,7 @@ public class SpiderDetector {
|
|||||||
|
|
||||||
private static boolean isUseProxies() {
|
private static boolean isUseProxies() {
|
||||||
if(useProxies == null) {
|
if(useProxies == null) {
|
||||||
if ("true".equals(ConfigurationManager.getProperty("useProxies")))
|
useProxies = "true".equals(ConfigurationManager.getProperty("useProxies"));
|
||||||
{
|
|
||||||
useProxies = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
useProxies = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return useProxies;
|
return useProxies;
|
||||||
|
@@ -76,8 +76,6 @@ public class SpiderDetectorTest
|
|||||||
candidate = "wiki.dspace.org";
|
candidate = "wiki.dspace.org";
|
||||||
req.setRemoteHost(candidate);
|
req.setRemoteHost(candidate);
|
||||||
assertFalse(candidate + " matched DNS patterns", SpiderDetector.isSpider(req));
|
assertFalse(candidate + " matched DNS patterns", SpiderDetector.isSpider(req));
|
||||||
|
|
||||||
req = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,7 +144,7 @@ public class SpiderDetectorTest
|
|||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
public void $clinit() {}
|
public void $clinit() {}
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
public boolean isUseProxies()
|
public boolean isUseProxies()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user