From 1e17389b10b19bb4fe0af6f2a2cb6c8f74a7da37 Mon Sep 17 00:00:00 2001 From: jtmelton Date: Fri, 4 Sep 2015 22:02:49 -0400 Subject: [PATCH] refactoring response storage to reduce code duplication --- .../appsensor/core/storage/ResponseStore.java | 40 +++++++++++++++++++ .../storage/file/FileBasedResponseStore.java | 30 +------------- .../storage/memory/InMemoryResponseStore.java | 31 +------------- .../storage/mongo/MongoResponseStore.java | 31 ++++---------- 4 files changed, 50 insertions(+), 82 deletions(-) diff --git a/appsensor-core/src/main/java/org/owasp/appsensor/core/storage/ResponseStore.java b/appsensor-core/src/main/java/org/owasp/appsensor/core/storage/ResponseStore.java index 4b5f13c0..1c70aa0e 100644 --- a/appsensor-core/src/main/java/org/owasp/appsensor/core/storage/ResponseStore.java +++ b/appsensor-core/src/main/java/org/owasp/appsensor/core/storage/ResponseStore.java @@ -1,13 +1,17 @@ package org.owasp.appsensor.core.storage; +import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.CopyOnWriteArrayList; import javax.inject.Inject; +import org.joda.time.DateTime; import org.owasp.appsensor.core.Response; +import org.owasp.appsensor.core.User; import org.owasp.appsensor.core.criteria.SearchCriteria; import org.owasp.appsensor.core.listener.ResponseListener; +import org.owasp.appsensor.core.util.DateUtils; /** * A store is an observable object. @@ -30,6 +34,7 @@ public abstract class ResponseStore { */ public abstract void addResponse(Response response); + /** * Finder for responses in the ResponseStore * @@ -37,6 +42,41 @@ public abstract class ResponseStore { * @return a {@link java.util.Collection} of {@link org.owasp.appsensor.core.Response} objects matching the search criteria. */ public abstract Collection findResponses(SearchCriteria criteria); + + /** + * Finder for responses in the ResponseStore + * + * @param criteria the {@link org.owasp.appsensor.core.criteria.SearchCriteria} object to search by + * @return a {@link java.util.Collection} of {@link org.owasp.appsensor.core.Response} objects matching the search criteria. + */ + public Collection findResponses(SearchCriteria criteria, Collection responses) { + if (criteria == null) { + throw new IllegalArgumentException("criteria must be non-null"); + } + + Collection matches = new ArrayList(); + + User user = criteria.getUser(); + Collection detectionSystemIds = criteria.getDetectionSystemIds(); + DateTime earliest = DateUtils.fromString(criteria.getEarliest()); + + for (Response response : responses) { + //check user match if user specified + boolean userMatch = (user != null) ? user.equals(response.getUser()) : true; + + //check detection system match if detection systems specified + boolean detectionSystemMatch = (detectionSystemIds != null && detectionSystemIds.size() > 0) ? + detectionSystemIds.contains(response.getDetectionSystem().getDetectionSystemId()) : true; + + boolean earliestMatch = (earliest != null) ? earliest.isBefore(DateUtils.fromString(response.getTimestamp())) : true; + + if (userMatch && detectionSystemMatch && earliestMatch) { + matches.add(response); + } + } + + return matches; + } /** * Register an {@link ResponseListener} to notify when {@link Response}s are added diff --git a/storage-providers/appsensor-storage-file-based/src/main/java/org/owasp/appsensor/storage/file/FileBasedResponseStore.java b/storage-providers/appsensor-storage-file-based/src/main/java/org/owasp/appsensor/storage/file/FileBasedResponseStore.java index 5aa30c55..19acf898 100644 --- a/storage-providers/appsensor-storage-file-based/src/main/java/org/owasp/appsensor/storage/file/FileBasedResponseStore.java +++ b/storage-providers/appsensor-storage-file-based/src/main/java/org/owasp/appsensor/storage/file/FileBasedResponseStore.java @@ -12,6 +12,7 @@ import javax.inject.Named; import org.joda.time.DateTime; +import org.owasp.appsensor.core.Attack; import org.owasp.appsensor.core.Response; import org.owasp.appsensor.core.User; import org.owasp.appsensor.core.criteria.SearchCriteria; @@ -75,34 +76,7 @@ public void addResponse(Response response) { */ @Override public Collection findResponses(SearchCriteria criteria) { - if (criteria == null) { - throw new IllegalArgumentException("criteria must be non-null"); - } - - Collection matches = new ArrayList(); - - User user = criteria.getUser(); - Collection detectionSystemIds = criteria.getDetectionSystemIds(); - DateTime earliest = DateUtils.fromString(criteria.getEarliest()); - - Collection responses = loadResponses(); - - for (Response response : responses) { - //check user match if user specified - boolean userMatch = (user != null) ? user.equals(response.getUser()) : true; - - //check detection system match if detection systems specified - boolean detectionSystemMatch = (detectionSystemIds != null && detectionSystemIds.size() > 0) ? - detectionSystemIds.contains(response.getDetectionSystem().getDetectionSystemId()) : true; - - boolean earliestMatch = (earliest != null) ? earliest.isBefore(DateUtils.fromString(response.getTimestamp())) : true; - - if (userMatch && detectionSystemMatch && earliestMatch) { - matches.add(response); - } - } - - return matches; + return findResponses(criteria, loadResponses()); } protected void writeResponse(Response response) { diff --git a/storage-providers/appsensor-storage-in-memory/src/main/java/org/owasp/appsensor/storage/memory/InMemoryResponseStore.java b/storage-providers/appsensor-storage-in-memory/src/main/java/org/owasp/appsensor/storage/memory/InMemoryResponseStore.java index 21ada5ce..5072c7f5 100644 --- a/storage-providers/appsensor-storage-in-memory/src/main/java/org/owasp/appsensor/storage/memory/InMemoryResponseStore.java +++ b/storage-providers/appsensor-storage-in-memory/src/main/java/org/owasp/appsensor/storage/memory/InMemoryResponseStore.java @@ -1,19 +1,15 @@ package org.owasp.appsensor.storage.memory; -import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.CopyOnWriteArrayList; import javax.inject.Named; -import org.joda.time.DateTime; import org.owasp.appsensor.core.Response; -import org.owasp.appsensor.core.User; import org.owasp.appsensor.core.criteria.SearchCriteria; import org.owasp.appsensor.core.listener.ResponseListener; import org.owasp.appsensor.core.logging.Loggable; import org.owasp.appsensor.core.storage.ResponseStore; -import org.owasp.appsensor.core.util.DateUtils; import org.slf4j.Logger; /** @@ -53,32 +49,7 @@ public void addResponse(Response response) { */ @Override public Collection findResponses(SearchCriteria criteria) { - if (criteria == null) { - throw new IllegalArgumentException("criteria must be non-null"); - } - - Collection matches = new ArrayList(); - - User user = criteria.getUser(); - Collection detectionSystemIds = criteria.getDetectionSystemIds(); - DateTime earliest = DateUtils.fromString(criteria.getEarliest()); - - for (Response response : responses) { - //check user match if user specified - boolean userMatch = (user != null) ? user.equals(response.getUser()) : true; - - //check detection system match if detection systems specified - boolean detectionSystemMatch = (detectionSystemIds != null && detectionSystemIds.size() > 0) ? - detectionSystemIds.contains(response.getDetectionSystem().getDetectionSystemId()) : true; - - boolean earliestMatch = (earliest != null) ? earliest.isBefore(DateUtils.fromString(response.getTimestamp())) : true; - - if (userMatch && detectionSystemMatch && earliestMatch) { - matches.add(response); - } - } - - return matches; + return findResponses(criteria, responses); } } diff --git a/storage-providers/appsensor-storage-nosql-mongodb/src/main/java/org/owasp/appsensor/storage/mongo/MongoResponseStore.java b/storage-providers/appsensor-storage-nosql-mongodb/src/main/java/org/owasp/appsensor/storage/mongo/MongoResponseStore.java index f578c18c..e1faa8c6 100644 --- a/storage-providers/appsensor-storage-nosql-mongodb/src/main/java/org/owasp/appsensor/storage/mongo/MongoResponseStore.java +++ b/storage-providers/appsensor-storage-nosql-mongodb/src/main/java/org/owasp/appsensor/storage/mongo/MongoResponseStore.java @@ -62,16 +62,12 @@ public void addResponse(Response response) { */ @Override public Collection findResponses(SearchCriteria criteria) { - if (criteria == null) { - throw new IllegalArgumentException("criteria must be non-null"); - } - - Collection matches = new ArrayList(); + return findResponses(criteria, loadResponses()); + } + + private Collection loadResponses() { + Collection responsesCollection = new ArrayList(); - User user = criteria.getUser(); - Collection detectionSystemIds = criteria.getDetectionSystemIds(); - DateTime earliest = DateUtils.fromString(criteria.getEarliest()); - DBCursor cursor = responses.find(); try { @@ -79,26 +75,13 @@ public Collection findResponses(SearchCriteria criteria) { DBObject object = cursor.next(); String json = JSON.serialize(object); Response response = gson.fromJson(json, Response.class); - - //check user match if user specified - boolean userMatch = (user != null) ? user.equals(response.getUser()) : true; - - //check detection system match if detection systems specified - boolean detectionSystemMatch = (detectionSystemIds != null && detectionSystemIds.size() > 0) ? - detectionSystemIds.contains(response.getDetectionSystem().getDetectionSystemId()) : true; - - boolean earliestMatch = (earliest != null) ? earliest.isBefore(DateUtils.fromString(response.getTimestamp())) : true; - - if (userMatch && detectionSystemMatch && earliestMatch) { - matches.add(response); - } + responsesCollection.add(response); } } finally { cursor.close(); } - - return matches; + return responsesCollection; } @PostConstruct