Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated the signature file download client from obsolete version to current #1086

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions droid-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@
<artifactId>commons-configuration</artifactId>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.16</version>
</dependency>
<dependency>
<groupId>net.java.truevfs</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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.
Expand All @@ -90,21 +93,22 @@ 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));
} else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NOT_MODIFIED) {
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(
Expand All @@ -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));
}
Expand All @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, The National Archives <[email protected]>
* 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));
}
}
Loading