mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-17 06:53:09 +00:00
refactored live import client and some utils class
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
*/
|
||||
package org.dspace.importer.external.scopus.service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -17,6 +16,6 @@ import java.util.Map;
|
||||
*/
|
||||
public interface LiveImportClient {
|
||||
|
||||
public InputStream executeHttpGetRequest(int timeout, String URL, Map<String, String> requestParams);
|
||||
public String executeHttpGetRequest(int timeout, String URL, Map<String, String> requestParams);
|
||||
|
||||
}
|
@@ -9,20 +9,21 @@ package org.dspace.importer.external.scopus.service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.config.RequestConfig.Builder;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.services.ConfigurationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -36,34 +37,32 @@ public class LiveImportClientImpl implements LiveImportClient {
|
||||
|
||||
private static final Logger log = Logger.getLogger(LiveImportClientImpl.class);
|
||||
|
||||
private CloseableHttpClient httpClient;
|
||||
|
||||
@Autowired
|
||||
private ConfigurationService configurationService;
|
||||
|
||||
@Override
|
||||
public InputStream executeHttpGetRequest(int timeout, String URL, Map<String, String> requestParams) {
|
||||
public String executeHttpGetRequest(int timeout, String URL, Map<String, String> requestParams) {
|
||||
HttpGet method = null;
|
||||
String proxyHost = configurationService.getProperty("http.proxy.host");
|
||||
String proxyPort = configurationService.getProperty("http.proxy.port");
|
||||
try {
|
||||
HttpClientBuilder hcBuilder = HttpClients.custom();
|
||||
try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient)
|
||||
.orElseGet(HttpClients::createDefault)) {
|
||||
|
||||
Builder requestConfigBuilder = RequestConfig.custom();
|
||||
requestConfigBuilder.setConnectionRequestTimeout(timeout);
|
||||
|
||||
if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
|
||||
HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http");
|
||||
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||
hcBuilder.setRoutePlanner(routePlanner);
|
||||
}
|
||||
RequestConfig defaultRequestConfig = requestConfigBuilder.build();
|
||||
|
||||
method = new HttpGet(getSearchUrl(URL, requestParams));
|
||||
method.setConfig(requestConfigBuilder.build());
|
||||
method.setConfig(defaultRequestConfig);
|
||||
|
||||
HttpClient client = hcBuilder.build();
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
configureProxy(method, defaultRequestConfig);
|
||||
|
||||
HttpResponse httpResponse = httpClient.execute(method);
|
||||
if (isNotSuccessfull(httpResponse)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return httpResponse.getEntity().getContent();
|
||||
InputStream inputStream = httpResponse.getEntity().getContent();
|
||||
return IOUtils.toString(inputStream, Charset.defaultCharset());
|
||||
} catch (Exception e1) {
|
||||
log.error(e1.getMessage(), e1);
|
||||
} finally {
|
||||
@@ -71,7 +70,18 @@ public class LiveImportClientImpl implements LiveImportClient {
|
||||
method.releaseConnection();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
private void configureProxy(HttpGet method, RequestConfig defaultRequestConfig) {
|
||||
String proxyHost = configurationService.getProperty("http.proxy.host");
|
||||
String proxyPort = configurationService.getProperty("http.proxy.port");
|
||||
if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
|
||||
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
|
||||
.setProxy(new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http"))
|
||||
.build();
|
||||
method.setConfig(requestConfig);
|
||||
}
|
||||
}
|
||||
|
||||
private String getSearchUrl(String URL, Map<String, String> requestParams) throws URISyntaxException {
|
||||
@@ -91,4 +101,12 @@ public class LiveImportClientImpl implements LiveImportClient {
|
||||
return response.getStatusLine().getStatusCode();
|
||||
}
|
||||
|
||||
public CloseableHttpClient getHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setHttpClient(CloseableHttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
}
|
52
dspace-api/src/main/java/org/dspace/importer/external/service/DoiCheck.java
vendored
Normal file
52
dspace-api/src/main/java/org/dspace/importer/external/service/DoiCheck.java
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.importer.external.service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
/**
|
||||
* Utility class that provides methods to check if a given string is a DOI and exists on CrossRef services
|
||||
*
|
||||
* @author Corrado Lombardi (corrado.lombardi at 4science.it)
|
||||
*/
|
||||
public class DoiCheck {
|
||||
|
||||
private static final List<String> DOI_PREFIXES = Arrays.asList("http://dx.doi.org/", "https://dx.doi.org/");
|
||||
|
||||
private static final Pattern PATTERN = Pattern.compile("10.\\d{4,9}/[-._;()/:A-Z0-9]+" +
|
||||
"|10.1002/[^\\s]+" +
|
||||
"|10.\\d{4}/\\d+-\\d+X?(\\d+)" +
|
||||
"\\d+<[\\d\\w]+:[\\d\\w]*>\\d+.\\d+.\\w+;\\d" +
|
||||
"|10.1021/\\w\\w\\d++" +
|
||||
"|10.1207/[\\w\\d]+\\&\\d+_\\d+",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
|
||||
|
||||
private DoiCheck() {
|
||||
}
|
||||
|
||||
public static boolean isDoi(final String value) {
|
||||
|
||||
Matcher m = PATTERN.matcher(purgeDoiValue(value));
|
||||
return m.matches();
|
||||
}
|
||||
|
||||
|
||||
public static String purgeDoiValue(final String query) {
|
||||
String value = query.replaceAll(",", "");
|
||||
for (final String prefix : DOI_PREFIXES) {
|
||||
value = value.replaceAll(prefix, "");
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
}
|
@@ -6,6 +6,8 @@
|
||||
|
||||
<bean class="org.dspace.external.service.impl.ExternalDataServiceImpl"/>
|
||||
|
||||
<bean class="org.dspace.importer.external.scopus.service.LiveImportClientImpl"/>
|
||||
|
||||
<bean class="org.dspace.external.provider.impl.MockDataProvider" init-method="init">
|
||||
<property name="sourceIdentifier" value="mock"/>
|
||||
</bean>
|
||||
|
Reference in New Issue
Block a user