From 0c4a1395ea8db14488fecbcd3e5b452281c729c4 Mon Sep 17 00:00:00 2001 From: Saurabh Parkhi Date: Wed, 10 Apr 2024 15:08:25 +0100 Subject: [PATCH] Migrated the signature file download client from obsolete version to current (#1086) --- droid-container/pom.xml | 11 ++- .../ContainerSignatureHttpService.java | 75 +++++++++++-------- .../httpservice/DateParseException.java | 44 +++++++++++ 3 files changed, 94 insertions(+), 36 deletions(-) create mode 100644 droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/DateParseException.java diff --git a/droid-container/pom.xml b/droid-container/pom.xml index 14b94add7..96c67ba96 100644 --- a/droid-container/pom.xml +++ b/droid-container/pom.xml @@ -79,9 +79,14 @@ commons-configuration - commons-httpclient - commons-httpclient - 3.1 + org.apache.httpcomponents + httpclient + 4.5.14 + + + org.apache.httpcomponents + httpcore + 4.4.16 net.java.truevfs diff --git a/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/ContainerSignatureHttpService.java b/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/ContainerSignatureHttpService.java index 1a132b5fa..40aab21c7 100644 --- a/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/ContainerSignatureHttpService.java +++ b/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/ContainerSignatureHttpService.java @@ -31,23 +31,18 @@ */ package uk.gov.nationalarchives.droid.container.httpservice; -import java.io.IOException; -import java.net.UnknownHostException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - import org.apache.commons.configuration.event.ConfigurationEvent; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.util.DateParseException; -import org.apache.commons.httpclient.util.DateUtil; - +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.DateUtils; +import org.apache.http.conn.routing.HttpRoutePlanner; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.DefaultProxyRoutePlanner; import uk.gov.nationalarchives.droid.core.interfaces.config.DroidGlobalConfig; import uk.gov.nationalarchives.droid.core.interfaces.config.DroidGlobalProperty; import uk.gov.nationalarchives.droid.core.interfaces.signature.ProxySettings; @@ -56,6 +51,14 @@ import uk.gov.nationalarchives.droid.core.interfaces.signature.SignatureType; import uk.gov.nationalarchives.droid.core.interfaces.signature.SignatureUpdateService; +import java.io.IOException; +import java.net.UnknownHostException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * @author rflitcroft * @@ -72,7 +75,7 @@ public class ContainerSignatureHttpService implements SignatureUpdateService { + "server at\n[%s]"; private String endpointUrl; - private HttpClient client = new HttpClient(); + private CloseableHttpClient client = HttpClientBuilder.create().build(); /** * Empty bean constructor. @@ -90,13 +93,14 @@ public ContainerSignatureHttpService(String endpointUrl) { @Override public SignatureFileInfo getLatestVersion(int currentVersion) throws SignatureServiceException { - GetMethod get = new GetMethod(endpointUrl); + HttpGet get = new HttpGet(endpointUrl); try { Date versionDate = getDateFromVersion(currentVersion); - String dateString = DateUtil.formatDate(versionDate); - get.addRequestHeader("If-Modified-Since", dateString); - - int statusCode = client.executeMethod(get); + String dateString = DateUtils.formatDate(versionDate); + get.addHeader("If-Modified-Since", dateString); + + HttpResponse response = client.execute(get); + int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { throw new SignatureServiceException( String.format(FILE_NOT_FOUND_404, endpointUrl)); @@ -104,7 +108,7 @@ public SignatureFileInfo getLatestVersion(int currentVersion) throws SignatureSe throw new SignatureServiceException( String.format(ERROR_MESSAGE_PATTERN, endpointUrl, statusCode)); } - int version = getVersion(get); + int version = getVersion(response); return new SignatureFileInfo(version, false, SignatureType.CONTAINER); } catch (UnknownHostException e) { throw new SignatureServiceException( @@ -116,12 +120,15 @@ public SignatureFileInfo getLatestVersion(int currentVersion) throws SignatureSe } } - private static int getVersion(HttpMethod httpMethod) throws DateParseException { + private static int getVersion(HttpResponse httpResponse) throws DateParseException { int version = 0; - Header header = httpMethod.getResponseHeader(LAST_MODIFIED_HEADER); + Header header = httpResponse.getFirstHeader(LAST_MODIFIED_HEADER); if (header != null) { String lastModified = header.getValue(); - Date lastModifiedDate = DateUtil.parseDate(lastModified); + Date lastModifiedDate = DateUtils.parseDate(lastModified); + if (lastModifiedDate == null) { + throw new DateParseException(lastModified); + } SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); version = Integer.parseInt(sdf.format(lastModifiedDate)); } @@ -136,10 +143,11 @@ private static Date getDateFromVersion(int versionNumber) throws ParseException @Override public SignatureFileInfo importSignatureFile(final Path targetDir) throws SignatureServiceException { - final GetMethod get = new GetMethod(endpointUrl); + final HttpGet get = new HttpGet(endpointUrl); try { - final int statusCode = client.executeMethod(get); + final HttpResponse response = client.execute(get); + final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { throw new SignatureServiceException( String.format(FILE_NOT_FOUND_404, endpointUrl)); @@ -148,13 +156,13 @@ public SignatureFileInfo importSignatureFile(final Path targetDir) throws Signat String.format(ERROR_MESSAGE_PATTERN, endpointUrl, statusCode)); } - final int version = getVersion(get); + final int version = getVersion(response); final SignatureFileInfo signatureFileInfo = new SignatureFileInfo(version, false, SignatureType.CONTAINER); final String fileName = String.format(FILENAME_PATTERN, version); final Path targetFile = targetDir.resolve(fileName); - Files.copy(get.getResponseBodyAsStream(), targetFile); + Files.copy(response.getEntity().getContent(), targetFile); signatureFileInfo.setFile(targetFile); return signatureFileInfo; @@ -189,10 +197,11 @@ public void configurationChanged(ConfigurationEvent evt) { public void onProxyChange(ProxySettings proxySettings) { if (proxySettings.isEnabled()) { - client = new HttpClient(); - client.getHostConfiguration().setProxy(proxySettings.getProxyHost(), proxySettings.getProxyPort()); + HttpRoutePlanner proxyRoutePlanner = new DefaultProxyRoutePlanner( + new HttpHost(proxySettings.getProxyHost(), proxySettings.getProxyPort())); + client = HttpClients.custom().setRoutePlanner(proxyRoutePlanner).build(); } else { - client = new HttpClient(); + client = HttpClientBuilder.create().build(); } } diff --git a/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/DateParseException.java b/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/DateParseException.java new file mode 100644 index 000000000..9dce54a50 --- /dev/null +++ b/droid-container/src/main/java/uk/gov/nationalarchives/droid/container/httpservice/DateParseException.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016, The National Archives + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the The National Archives nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package uk.gov.nationalarchives.droid.container.httpservice; + +public class DateParseException extends RuntimeException { + private static final String COULD_NOT_PARSE_DATE = "Could not parse date string %s"; + + public DateParseException() { + this(""); + } + + public DateParseException(String unparseableString) { + super(String.format(COULD_NOT_PARSE_DATE, unparseableString)); + } + }