Skip to content

Commit 86568b5

Browse files
authored
Merge pull request #11440 from GlobalDataverseCommunityConsortium/ApacheHTTPUpdate
Switch to new apache client
2 parents 1d03a2f + e5f6d53 commit 86568b5

File tree

14 files changed

+266
-329
lines changed

14 files changed

+266
-329
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@
126126
java.net.HttpUrlConnection is used, sometimes Apache, sometimes Unirest and what not.
127127
A big pile of historically grown mess. Some poor dev should take a Kleenex and call for "payback time". -->
128128
<dependency>
129-
<groupId>commons-httpclient</groupId>
130-
<artifactId>commons-httpclient</artifactId>
131-
<version>3.1</version>
129+
<groupId>org.apache.httpcomponents.client5</groupId>
130+
<artifactId>httpclient5</artifactId>
131+
<version>5.2.1</version>
132132
</dependency>
133133

134134
<!-- BEGIN Data Deposit API v1 (SWORD v2) -->

src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import jakarta.persistence.criteria.*;
4444
import org.apache.commons.codec.digest.DigestUtils;
45-
import org.apache.commons.httpclient.HttpException;
4645
import org.apache.commons.lang3.StringUtils;
4746
import org.apache.http.HttpResponse;
4847
import org.apache.http.HttpResponseInterceptor;
@@ -554,7 +553,7 @@ public void registerExternalTerm(JsonObject cvocEntry, String term, List<Dataset
554553
try (CloseableHttpClient httpClient = HttpClients.custom()
555554
.addInterceptorLast(new HttpResponseInterceptor() {
556555
@Override
557-
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
556+
public void process(HttpResponse response, HttpContext context) throws IOException {
558557
int statusCode = response.getStatusLine().getStatusCode();
559558
if (statusCode == 504) {
560559
//Throwing an exception triggers the retry handler

src/main/java/edu/harvard/iq/dataverse/DatasetPage.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@
109109
import org.primefaces.model.file.UploadedFile;
110110

111111
import jakarta.validation.ConstraintViolation;
112-
import org.apache.commons.httpclient.HttpClient;
113-
//import org.primefaces.context.RequestContext;
114112
import java.util.Arrays;
115113
import java.util.HashSet;
116114
import jakarta.faces.model.SelectItem;
@@ -4234,12 +4232,6 @@ public void cancelCreate() {
42344232
}
42354233
}
42364234

4237-
private HttpClient getClient() {
4238-
// TODO:
4239-
// cache the http client? -- L.A. 4.0 alpha
4240-
return new HttpClient();
4241-
}
4242-
42434235
public void refreshLock() {
42444236
//RequestContext requestContext = RequestContext.getCurrentInstance();
42454237
logger.fine("checking lock");

src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
import jakarta.json.JsonObjectBuilder;
7777
import jakarta.json.JsonArray;
7878
import jakarta.json.JsonReader;
79-
import org.apache.commons.httpclient.HttpClient;
8079
import org.apache.commons.io.IOUtils;
81-
import org.apache.commons.httpclient.methods.GetMethod;
8280
import java.util.Arrays;
8381
import java.util.Collection;
8482
import java.util.Set;
@@ -89,6 +87,11 @@
8987
import jakarta.servlet.http.HttpServletResponse;
9088
import org.apache.commons.lang3.StringUtils;
9189
import org.apache.commons.lang3.mutable.MutableBoolean;
90+
import org.apache.hc.client5.http.classic.methods.HttpGet;
91+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
92+
import org.apache.hc.client5.http.impl.classic.HttpClients;
93+
import org.apache.hc.core5.http.ClassicHttpResponse;
94+
import org.apache.hc.core5.http.HttpEntity;
9295
import org.primefaces.PrimeFaces;
9396

9497
/**
@@ -1336,46 +1339,6 @@ public String cancel() {
13361339
return returnToDatasetOnly();
13371340
}
13381341

1339-
/* deprecated; super inefficient, when called repeatedly on a long list
1340-
of files!
1341-
leaving the code here, commented out, for illustration purposes. -- 4.6
1342-
public boolean isDuplicate(FileMetadata fileMetadata) {
1343-
1344-
Map<String, Integer> MD5Map = new HashMap<String, Integer>();
1345-
1346-
// TODO:
1347-
// think of a way to do this that doesn't involve populating this
1348-
// map for every file on the page?
1349-
// may not be that much of a problem, if we paginate and never display
1350-
// more than a certain number of files... Still, needs to be revisited
1351-
// before the final 4.0.
1352-
// -- L.A. 4.0
1353-
1354-
// make a "defensive copy" to avoid java.util.ConcurrentModificationException from being thrown
1355-
// when uploading 100+ files
1356-
List<FileMetadata> wvCopy = new ArrayList<>(workingVersion.getFileMetadatas());
1357-
Iterator<FileMetadata> fmIt = wvCopy.iterator();
1358-
1359-
while (fmIt.hasNext()) {
1360-
FileMetadata fm = fmIt.next();
1361-
String md5 = fm.getDataFile().getChecksumValue();
1362-
if (md5 != null) {
1363-
if (MD5Map.get(md5) != null) {
1364-
MD5Map.put(md5, MD5Map.get(md5).intValue() + 1);
1365-
} else {
1366-
MD5Map.put(md5, 1);
1367-
}
1368-
}
1369-
}
1370-
1371-
return MD5Map.get(thisMd5) != null && MD5Map.get(thisMd5).intValue() > 1;
1372-
}*/
1373-
private HttpClient getClient() {
1374-
// TODO:
1375-
// cache the http client? -- L.A. 4.0 alpha
1376-
return new HttpClient();
1377-
}
1378-
13791342
/**
13801343
* Is this page in File Replace mode
13811344
*
@@ -1414,30 +1377,35 @@ public boolean showFileUploadComponent() {
14141377
* @param fileLink
14151378
* @return
14161379
*/
1417-
private InputStream getDropBoxInputStream(String fileLink, GetMethod dropBoxMethod) {
1418-
1380+
private InputStream getDropBoxInputStream(String fileLink) {
14191381
if (fileLink == null) {
14201382
return null;
14211383
}
14221384

1423-
// -----------------------------------------------------------
1424-
// Make http call, download the file:
1425-
// -----------------------------------------------------------
1426-
int status = 0;
1427-
//InputStream dropBoxStream = null;
1428-
14291385
try {
1430-
status = getClient().executeMethod(dropBoxMethod);
1386+
CloseableHttpClient httpClient = HttpClients.createDefault();
1387+
HttpGet httpGet = new HttpGet(fileLink);
1388+
1389+
// Use the non-deprecated execute method with ClassicHttpResponse
1390+
ClassicHttpResponse response = httpClient.executeOpen(null, httpGet, null);
1391+
1392+
int status = response.getCode();
1393+
14311394
if (status == 200) {
1432-
return dropBoxMethod.getResponseBodyAsStream();
1395+
HttpEntity entity = response.getEntity();
1396+
if (entity != null) {
1397+
// Return the content as a stream directly
1398+
return entity.getContent();
1399+
}
14331400
}
1401+
1402+
logger.log(Level.WARNING, "Failed to get DropBox InputStream for file: {0}. Status code: {1}", new Object[]{fileLink, status});
1403+
response.close();
1404+
return null;
14341405
} catch (IOException ex) {
14351406
logger.log(Level.WARNING, "Failed to access DropBox url: {0}!", fileLink);
14361407
return null;
14371408
}
1438-
1439-
logger.log(Level.WARNING, "Failed to get DropBox InputStream for file: {0}", fileLink);
1440-
return null;
14411409
} // end: getDropBoxInputStream
14421410

14431411
/**
@@ -1464,7 +1432,6 @@ public void handleDropBoxUpload(ActionEvent event) {
14641432
// Iterate through the Dropbox file information (JSON)
14651433
// -----------------------------------------------------------
14661434
DataFile dFile = null;
1467-
GetMethod dropBoxMethod = null;
14681435
String localWarningMessage = null;
14691436
for (int i = 0; i < dbArray.size(); i++) {
14701437
JsonObject dbObject = dbArray.getJsonObject(i);
@@ -1497,14 +1464,13 @@ public void handleDropBoxUpload(ActionEvent event) {
14971464
}
14981465

14991466
dFile = null;
1500-
dropBoxMethod = new GetMethod(fileLink);
15011467

15021468
// -----------------------------------------------------------
15031469
// Download the file
15041470
// -----------------------------------------------------------
1505-
InputStream dropBoxStream = this.getDropBoxInputStream(fileLink, dropBoxMethod);
1471+
InputStream dropBoxStream = this.getDropBoxInputStream(fileLink);
15061472
if (dropBoxStream == null) {
1507-
logger.severe("Could not retrieve dropgox input stream for: " + fileLink);
1473+
logger.severe("Could not retrieve dropbox input stream for: " + fileLink);
15081474
continue; // Error skip this file
15091475
}
15101476

@@ -1542,14 +1508,6 @@ public void handleDropBoxUpload(ActionEvent event) {
15421508
this.logger.log(Level.SEVERE, "Error during ingest of DropBox file {0} from link {1}: {2}", new Object[]{fileName, fileLink, ex.getMessage()});
15431509
continue;
15441510
}*/ finally {
1545-
// -----------------------------------------------------------
1546-
// release connection for dropBoxMethod
1547-
// -----------------------------------------------------------
1548-
1549-
if (dropBoxMethod != null) {
1550-
dropBoxMethod.releaseConnection();
1551-
}
1552-
15531511
// -----------------------------------------------------------
15541512
// close the dropBoxStream
15551513
// -----------------------------------------------------------

src/main/java/edu/harvard/iq/dataverse/engine/command/impl/DeletePidCommand.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import edu.harvard.iq.dataverse.pidproviders.PidProvider;
1414
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
1515
import edu.harvard.iq.dataverse.util.BundleUtil;
16-
import org.apache.commons.httpclient.HttpException;
1716

1817
import java.util.Arrays;
1918
import java.util.Collections;
@@ -50,13 +49,8 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {
5049
dataset.setGlobalIdCreateTime(null);
5150
dataset.setIdentifierRegistered(false);
5251
ctxt.datasets().merge(dataset);
53-
} catch (HttpException hex) {
54-
String message = BundleUtil.getStringFromBundle("pids.deletePid.failureExpected",
55-
Arrays.asList(dataset.getGlobalId().asString(), Integer.toString(hex.getReasonCode())));
56-
logger.info(message);
57-
throw new IllegalCommandException(message, this);
5852
} catch (Exception ex) {
59-
String message = BundleUtil.getStringFromBundle("pids.deletePid.failureOther",
53+
String message = BundleUtil.getStringFromBundle("pids.deletePid.failure",
6054
Arrays.asList(dataset.getGlobalId().asString(), ex.getLocalizedMessage()));
6155
logger.info(message);
6256
throw new IllegalCommandException(message, this);

src/main/java/edu/harvard/iq/dataverse/engine/command/impl/ImportDatasetCommand.java

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
import java.util.Collections;
1515
import java.util.logging.Level;
1616
import java.util.logging.Logger;
17-
import org.apache.commons.httpclient.HttpClient;
18-
import org.apache.commons.httpclient.methods.GetMethod;
17+
import org.apache.hc.client5.http.classic.methods.HttpGet;
18+
import org.apache.hc.client5.http.config.RequestConfig;
19+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
20+
import org.apache.hc.client5.http.impl.classic.HttpClients;
21+
import org.apache.hc.core5.http.HttpStatus;
22+
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
23+
1924
import static org.apache.commons.lang3.StringUtils.isEmpty;
2025

2126
/**
@@ -63,34 +68,48 @@ protected void additionalParameterTests(CommandContext ctxt) throws CommandExcep
6368
}
6469

6570
String pid = ds.getPersistentURL();
66-
GetMethod httpGet = new GetMethod(pid);
67-
httpGet.setFollowRedirects(false);
68-
69-
HttpClient client = new HttpClient();
71+
HttpGet httpGet = new HttpGet(pid);
72+
73+
RequestConfig requestConfig = RequestConfig.custom()
74+
.setRedirectsEnabled(false)
75+
.build();
7076

71-
try {
72-
int responseStatus = client.executeMethod(httpGet);
77+
try (CloseableHttpClient client = HttpClients.custom()
78+
.setDefaultRequestConfig(requestConfig)
79+
.build()) {
80+
81+
HttpClientResponseHandler<Void> responseHandler = response -> {
82+
int responseStatus = response.getCode();
7383

74-
if (responseStatus == 404) {
75-
/*
76-
* Using test DOIs from DataCite, we'll get a 404 when trying to resolve the DOI
77-
* to a landing page, but the DOI may already exist. An extra check here allows
78-
* use of DataCite test DOIs. It also changes import slightly in allowing PIDs
79-
* that exist (and accessible in the PID provider account configured in
80-
* Dataverse) but aren't findable to be used. That could be the case if, for
81-
* example, someone was importing a draft dataset from elsewhere.
82-
*/
83-
PidProvider pidProvider = PidUtil.getPidProvider(ds.getGlobalId().getProviderId());
84-
if (pidProvider != null) {
85-
if (pidProvider.alreadyRegistered(ds.getGlobalId(), true)) {
86-
return;
84+
if (responseStatus == HttpStatus.SC_NOT_FOUND) {
85+
/*
86+
* Using test DOIs from DataCite, we'll get a 404 when trying to resolve the DOI
87+
* to a landing page, but the DOI may already exist. An extra check here allows
88+
* use of DataCite test DOIs. It also changes import slightly in allowing PIDs
89+
* that exist (and accessible in the PID provider account configured in
90+
* Dataverse) but aren't findable to be used. That could be the case if, for
91+
* example, someone was importing a draft dataset from elsewhere.
92+
*/
93+
PidProvider pidProvider = PidUtil.getPidProvider(ds.getGlobalId().getProviderId());
94+
try {
95+
if (pidProvider != null && pidProvider.alreadyRegistered(ds.getGlobalId(), true)) {
96+
return null;
97+
}
98+
} catch (Exception e) {
99+
throw new IOException("Cannot validate PID due to an error: " + e.getMessage());
87100
}
101+
throw new IOException("Provided PID does not exist. Status code for GET '" + pid + "' is 404.");
88102
}
89-
throw new CommandExecutionException(
90-
"Provided PID does not exist. Status code for GET '" + pid + "' is 404.", this);
103+
return null;
104+
};
105+
try {
106+
client.execute(httpGet, responseHandler);
107+
} catch (IOException ex) {
108+
logger.log(Level.WARNING,
109+
"Error while validating PID at '" + pid + "' for an imported dataset: " + ex.getMessage(), ex);
110+
throw new CommandExecutionException(ex.getMessage(), this);
91111
}
92-
93-
} catch (Exception ex) {
112+
} catch (IOException ex) {
94113
logger.log(Level.WARNING,
95114
"Error while validating PID at '" + pid + "' for an imported dataset: " + ex.getMessage(), ex);
96115
throw new CommandExecutionException("Cannot validate PID due to an error: " + ex.getMessage(), this);

src/main/java/edu/harvard/iq/dataverse/pidproviders/doi/UnmanagedDOIProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.Map;
6-
import org.apache.commons.httpclient.HttpException;
76
import org.apache.commons.lang3.NotImplementedException;
87

98
import edu.harvard.iq.dataverse.DvObject;
@@ -54,7 +53,7 @@ public String modifyIdentifierTargetURL(DvObject dvObject) throws Exception {
5453
}
5554

5655
@Override
57-
public void deleteIdentifier(DvObject dvObject) throws IOException, HttpException {
56+
public void deleteIdentifier(DvObject dvObject) throws IOException {
5857
throw new NotImplementedException();
5958
}
6059

src/main/java/edu/harvard/iq/dataverse/pidproviders/doi/crossref/CrossRefDOIProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public CrossRefDOIProvider(String id, String label, String providerAuthority, St
2626
}
2727

2828
@Override
29-
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) throws Exception {
29+
public boolean alreadyRegistered(GlobalId pid, boolean noProviderDefault) {
3030
logger.info("CrossRef alreadyRegistered");
3131
if (pid == null || pid.asString().isEmpty()) {
3232
logger.fine("No identifier sent.");

0 commit comments

Comments
 (0)