Skip to content

Commit

Permalink
refactoring response storage to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmelton committed Sep 5, 2015
1 parent f82968e commit 1e17389
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -30,13 +34,49 @@ public abstract class ResponseStore {
*/
public abstract void addResponse(Response response);


/**
* 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 abstract Collection<Response> 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<Response> findResponses(SearchCriteria criteria, Collection<Response> responses) {
if (criteria == null) {
throw new IllegalArgumentException("criteria must be non-null");
}

Collection<Response> matches = new ArrayList<Response>();

User user = criteria.getUser();
Collection<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,34 +76,7 @@ public void addResponse(Response response) {
*/
@Override
public Collection<Response> findResponses(SearchCriteria criteria) {
if (criteria == null) {
throw new IllegalArgumentException("criteria must be non-null");
}

Collection<Response> matches = new ArrayList<Response>();

User user = criteria.getUser();
Collection<String> detectionSystemIds = criteria.getDetectionSystemIds();
DateTime earliest = DateUtils.fromString(criteria.getEarliest());

Collection<Response> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -53,32 +49,7 @@ public void addResponse(Response response) {
*/
@Override
public Collection<Response> findResponses(SearchCriteria criteria) {
if (criteria == null) {
throw new IllegalArgumentException("criteria must be non-null");
}

Collection<Response> matches = new ArrayList<Response>();

User user = criteria.getUser();
Collection<String> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,26 @@ public void addResponse(Response response) {
*/
@Override
public Collection<Response> findResponses(SearchCriteria criteria) {
if (criteria == null) {
throw new IllegalArgumentException("criteria must be non-null");
}
Collection<Response> matches = new ArrayList<Response>();
return findResponses(criteria, loadResponses());
}

private Collection<Response> loadResponses() {
Collection<Response> responsesCollection = new ArrayList<Response>();

User user = criteria.getUser();
Collection<String> detectionSystemIds = criteria.getDetectionSystemIds();
DateTime earliest = DateUtils.fromString(criteria.getEarliest());

DBCursor cursor = responses.find();

try {
while (cursor.hasNext()) {
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
Expand Down

0 comments on commit 1e17389

Please sign in to comment.