From f81a0101ef1cdb4d6875e457da6be7886f728ccf Mon Sep 17 00:00:00 2001 From: Leonid Andreev Date: Wed, 13 Jul 2022 12:04:06 -0400 Subject: [PATCH] got rid of an inefficient/unnecessary instance of .replaceAll() (#8372) --- .../harvest/client/HarvesterServiceBean.java | 20 ++++----------- .../harvest/client/oai/OaiHandler.java | 25 +++++++++++++------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvesterServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvesterServiceBean.java index 7f34847cbf3..e7156dfe9aa 100644 --- a/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvesterServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvesterServiceBean.java @@ -84,7 +84,7 @@ public class HarvesterServiceBean { public static final String HARVEST_RESULT_SUCCESS="success"; public static final String HARVEST_RESULT_FAILED="failed"; public static final String DATAVERSE_PROPRIETARY_METADATA_FORMAT="dataverse_json"; - public static final String DATAVERSE_PROPRIETARY_METADATA_API="/api/datasets/export?exporter="+DATAVERSE_PROPRIETARY_METADATA_FORMAT; + public static final String DATAVERSE_PROPRIETARY_METADATA_API="/api/datasets/export?exporter="+DATAVERSE_PROPRIETARY_METADATA_FORMAT+"&persistentId="; public HarvesterServiceBean() { @@ -302,8 +302,9 @@ private Long processRecord(DataverseRequest dataverseRequest, Logger hdLogger, P if (DATAVERSE_PROPRIETARY_METADATA_FORMAT.equals(oaiHandler.getMetadataPrefix())) { // Make direct call to obtain the proprietary Dataverse metadata // in JSON from the remote Dataverse server: - String extendedApiUrl = getProprietaryDataverseMetadataURL(oaiHandler.getBaseOaiUrl(), identifier); - tempFile = retrieveProprietaryDataverseMetadata(httpClient, extendedApiUrl); + String metadataApiUrl = oaiHandler.getProprietaryDataverseMetadataURL(identifier); + logger.info("calling "+metadataApiUrl); + tempFile = retrieveProprietaryDataverseMetadata(httpClient, metadataApiUrl); } else { FastGetRecord record = oaiHandler.runGetRecord(identifier); @@ -409,18 +410,7 @@ private void deleteHarvestedDatasetIfExists(String persistentIdentifier, Dataver } hdLogger.info("No dataset found for " + persistentIdentifier + ", skipping delete. "); } - - private static String getProprietaryDataverseMetadataURL(String baseURL, String identifier) { - - baseURL = baseURL.replaceAll("/oai", ""); - - StringBuilder requestURL = new StringBuilder(baseURL); - requestURL.append(DATAVERSE_PROPRIETARY_METADATA_API); - requestURL.append("&persistentId=").append(identifier); - - return requestURL.toString(); - } - + private void logBeginOaiHarvest(Logger hdLogger, HarvestingClient harvestingClient) { hdLogger.log(Level.INFO, "BEGIN HARVEST, oaiUrl=" +harvestingClient.getHarvestingUrl() diff --git a/src/main/java/edu/harvard/iq/dataverse/harvest/client/oai/OaiHandler.java b/src/main/java/edu/harvard/iq/dataverse/harvest/client/oai/OaiHandler.java index a32feeed5a7..c0a039e2d2b 100644 --- a/src/main/java/edu/harvard/iq/dataverse/harvest/client/oai/OaiHandler.java +++ b/src/main/java/edu/harvard/iq/dataverse/harvest/client/oai/OaiHandler.java @@ -1,12 +1,11 @@ package edu.harvard.iq.dataverse.harvest.client.oai; -import io.gdcc.xoai.model.oaipmh.results.Description; import io.gdcc.xoai.model.oaipmh.Granularity; import io.gdcc.xoai.model.oaipmh.results.record.Header; import io.gdcc.xoai.model.oaipmh.results.MetadataFormat; import io.gdcc.xoai.model.oaipmh.results.Set; import io.gdcc.xoai.serviceprovider.ServiceProvider; -import io.gdcc.xoai.serviceprovider.client.JdkHttpOaiClient; //.HttpOAIClient; +import io.gdcc.xoai.serviceprovider.client.JdkHttpOaiClient; import io.gdcc.xoai.serviceprovider.exceptions.BadArgumentException; import io.gdcc.xoai.serviceprovider.exceptions.InvalidOAIResponse; import io.gdcc.xoai.serviceprovider.exceptions.NoSetHierarchyException; @@ -14,16 +13,15 @@ import io.gdcc.xoai.serviceprovider.model.Context; import io.gdcc.xoai.serviceprovider.parameters.ListIdentifiersParameters; import edu.harvard.iq.dataverse.harvest.client.FastGetRecord; +import static edu.harvard.iq.dataverse.harvest.client.HarvesterServiceBean.DATAVERSE_PROPRIETARY_METADATA_API; import edu.harvard.iq.dataverse.harvest.client.HarvestingClient; import java.io.IOException; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.lang3.StringUtils; import org.xml.sax.SAXException; import javax.xml.transform.TransformerException; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; @@ -70,8 +68,9 @@ public OaiHandler(HarvestingClient harvestingClient) throws OaiHandlerException this.harvestingClient = harvestingClient; } - private String baseOaiUrl; //= harvestingClient.getHarvestingUrl(); - private String metadataPrefix; // = harvestingClient.getMetadataPrefix(); + private String baseOaiUrl; + private String dataverseApiUrl; // if the remote server is a Dataverse and we access its native metadata + private String metadataPrefix; private String setName; private Date fromDate; private Boolean setListTruncated = false; @@ -120,7 +119,7 @@ public boolean isSetListTruncated() { return setListTruncated; } - private ServiceProvider getServiceProvider() throws OaiHandlerException { + public ServiceProvider getServiceProvider() throws OaiHandlerException { if (serviceProvider == null) { if (baseOaiUrl == null) { throw new OaiHandlerException("Could not instantiate Service Provider, missing OAI server URL."); @@ -277,6 +276,18 @@ private ListIdentifiersParameters buildListIdentifiersParams() throws OaiHandler return mip; } + public String getProprietaryDataverseMetadataURL(String identifier) { + + if (dataverseApiUrl == null) { + dataverseApiUrl = baseOaiUrl.replaceFirst("/oai", ""); + } + + StringBuilder requestURL = new StringBuilder(dataverseApiUrl); + requestURL.append(DATAVERSE_PROPRIETARY_METADATA_API).append(identifier); + + return requestURL.toString(); + } + public void runIdentify() { // not implemented yet // (we will need it, both for validating the remote server,