Skip to content

Commit 2aa06e1

Browse files
authored
Merge pull request DSpace#11261 from JohnnyMendesC/fix/11033-solr-logging-thumbnail-download-stats
Fix/11033 solr logging thumbnail download stats
2 parents ee7cfae + c80359b commit 2aa06e1

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

dspace-api/src/main/java/org/dspace/statistics/SolrLoggerServiceImpl.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.time.format.DateTimeParseException;
3535
import java.time.temporal.ChronoUnit;
3636
import java.util.ArrayList;
37+
import java.util.Arrays;
3738
import java.util.EnumSet;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
@@ -231,6 +232,10 @@ public void postView(DSpaceObject dspaceObject, HttpServletRequest request,
231232
throw new RuntimeException(e);
232233
}
233234

235+
if (dspaceObject instanceof Bitstream && !isBitstreamLoggable((Bitstream) dspaceObject)) {
236+
return;
237+
}
238+
234239
if (solr == null) {
235240
return;
236241
}
@@ -278,6 +283,10 @@ public void postView(DSpaceObject dspaceObject,
278283
@Override
279284
public void postView(DSpaceObject dspaceObject,
280285
String ip, String userAgent, String xforwardedfor, EPerson currentUser, String referrer) {
286+
if (dspaceObject instanceof Bitstream && !isBitstreamLoggable((Bitstream) dspaceObject)) {
287+
return;
288+
}
289+
281290
if (solr == null) {
282291
return;
283292
}
@@ -1617,4 +1626,35 @@ public Object anonymizeIp(String ip) throws UnknownHostException {
16171626

16181627
throw new UnknownHostException("unknown ip format");
16191628
}
1629+
1630+
/**
1631+
* Checks if a given Bitstream's bundles are configured to be logged in Solr statistics.
1632+
*
1633+
* @param bitstream The bitstream to check.
1634+
* @return {@code true} if the bitstream event should be logged, {@code false} otherwise.
1635+
*/
1636+
private boolean isBitstreamLoggable(Bitstream bitstream) {
1637+
String[] allowedBundles = configurationService
1638+
.getArrayProperty("solr-statistics.query.filter.bundles");
1639+
if (allowedBundles == null || allowedBundles.length == 0) {
1640+
return true;
1641+
}
1642+
List<String> allowedBundlesList = Arrays.asList(allowedBundles);
1643+
try {
1644+
List<Bundle> actualBundles = bitstream.getBundles();
1645+
if (actualBundles.isEmpty()) {
1646+
return true;
1647+
}
1648+
for (Bundle bundle : actualBundles) {
1649+
if (allowedBundlesList.contains(bundle.getName())) {
1650+
return true;
1651+
}
1652+
}
1653+
} catch (SQLException e) {
1654+
log.error("Error checking bitstream bundles for logging statistics for bitstream {}",
1655+
bitstream.getID(), e);
1656+
return true;
1657+
}
1658+
return false;
1659+
}
16201660
}

dspace-api/src/test/java/org/dspace/statistics/SolrLoggerServiceImplIT.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.junit.Assert.assertFalse;
1212
import static org.junit.Assert.assertTrue;
1313

14+
import java.io.ByteArrayInputStream;
1415
import java.io.IOException;
1516
import java.io.Writer;
1617
import java.nio.charset.StandardCharsets;
@@ -27,8 +28,14 @@
2728
import org.apache.solr.common.SolrDocument;
2829
import org.apache.solr.common.SolrInputDocument;
2930
import org.dspace.AbstractIntegrationTestWithDatabase;
31+
import org.dspace.builder.BitstreamBuilder;
32+
import org.dspace.builder.CollectionBuilder;
3033
import org.dspace.builder.CommunityBuilder;
34+
import org.dspace.builder.ItemBuilder;
35+
import org.dspace.content.Bitstream;
36+
import org.dspace.content.Collection;
3137
import org.dspace.content.Community;
38+
import org.dspace.content.Item;
3239
import org.dspace.content.factory.ContentServiceFactory;
3340
import org.dspace.core.Constants;
3441
import org.dspace.core.factory.CoreServiceFactory;
@@ -303,4 +310,56 @@ public void testDeleteRobots()
303310
}
304311
assertEquals("Wrong number of documents remaining --", 1, nDocs);
305312
}
313+
314+
@Test
315+
public void testPostViewShouldNotLogIgnoredBundles() throws Exception {
316+
ContentServiceFactory csf = ContentServiceFactory.getInstance();
317+
MockSolrLoggerServiceImpl solrLoggerService = DSpaceServicesFactory.getInstance()
318+
.getServiceManager()
319+
.getServiceByName("solrLoggerService", MockSolrLoggerServiceImpl.class);
320+
solrLoggerService.bitstreamService = csf.getBitstreamService();
321+
solrLoggerService.contentServiceFactory = csf;
322+
solrLoggerService.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
323+
solrLoggerService.clientInfoService = CoreServiceFactory.getInstance().getClientInfoService();
324+
solrLoggerService.afterPropertiesSet();
325+
SolrStatisticsCore solrStatisticsCore = DSpaceServicesFactory.getInstance()
326+
.getServiceManager()
327+
.getServiceByName(SolrStatisticsCore.class.getName(), MockSolrStatisticsCore.class);
328+
329+
solrStatisticsCore.getSolr().deleteByQuery("*:*");
330+
solrStatisticsCore.getSolr().commit();
331+
332+
context.turnOffAuthorisationSystem();
333+
Community community = CommunityBuilder
334+
.createCommunity(context)
335+
.withName("Test Community").build();
336+
Collection collection = CollectionBuilder
337+
.createCollection(context, community)
338+
.withName("Test Collection").build();
339+
Item item = ItemBuilder
340+
.createItem(context, collection)
341+
.withTitle("Test Item for Logging").build();
342+
Bitstream originalBitstream = BitstreamBuilder
343+
.createBitstream(context, item, new ByteArrayInputStream("original content".getBytes()), "ORIGINAL")
344+
.withName("original.txt")
345+
.build();
346+
Bitstream thumbnailBitstream = BitstreamBuilder
347+
.createBitstream(context, item, new ByteArrayInputStream("thumbnail content".getBytes()), "THUMBNAIL")
348+
.withName("thumbnail.jpg")
349+
.build();
350+
351+
context.restoreAuthSystemState();
352+
solrLoggerService.postView(originalBitstream, null, eperson);
353+
solrLoggerService.postView(thumbnailBitstream, null, eperson);
354+
355+
solrStatisticsCore.getSolr().commit();
356+
357+
SolrQuery thumbnailQuery = new SolrQuery("id:" + thumbnailBitstream.getID().toString());
358+
QueryResponse thumbnailResponse = solrStatisticsCore.getSolr().query(thumbnailQuery);
359+
assertEquals("Thumbnail bundle should NOT be logged", 0, thumbnailResponse.getResults().getNumFound());
360+
361+
SolrQuery originalQuery = new SolrQuery("id:" + originalBitstream.getID().toString());
362+
QueryResponse originalResponse = solrStatisticsCore.getSolr().query(originalQuery);
363+
assertEquals("ORIGINAL bundle SHOULD be logged", 1, originalResponse.getResults().getNumFound());
364+
}
306365
}

0 commit comments

Comments
 (0)