From 5070c4eea9495b2e1593bb31a109bc268e14689d Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 19 Jun 2021 20:23:33 +0100 Subject: [PATCH] Better handling of data file responses in the /data/file/frompeer API endpoint. --- src/main/java/org/qortal/api/ApiError.java | 8 ++++++-- .../org/qortal/api/resource/DataResource.java | 17 +++++++++++------ src/main/resources/i18n/ApiError_en.properties | 4 ++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/qortal/api/ApiError.java b/src/main/java/org/qortal/api/ApiError.java index dd7fc4b0..d43aa197 100644 --- a/src/main/java/org/qortal/api/ApiError.java +++ b/src/main/java/org/qortal/api/ApiError.java @@ -129,7 +129,11 @@ public enum ApiError { // Foreign blockchain FOREIGN_BLOCKCHAIN_NETWORK_ISSUE(1201, 500), FOREIGN_BLOCKCHAIN_BALANCE_ISSUE(1202, 402), - FOREIGN_BLOCKCHAIN_TOO_SOON(1203, 408); + FOREIGN_BLOCKCHAIN_TOO_SOON(1203, 408), + + // Data + FILE_NOT_FOUND(1301, 404), + NO_REPLY(1302, 404); private static final Map map = stream(ApiError.values()).collect(toMap(apiError -> apiError.code, apiError -> apiError)); @@ -157,4 +161,4 @@ public enum ApiError { return this.status; } -} \ No newline at end of file +} diff --git a/src/main/java/org/qortal/api/resource/DataResource.java b/src/main/java/org/qortal/api/resource/DataResource.java index 1ce23f56..e9eae796 100644 --- a/src/main/java/org/qortal/api/resource/DataResource.java +++ b/src/main/java/org/qortal/api/resource/DataResource.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.List; @@ -150,9 +151,9 @@ public class DataResource { ) } ) - @ApiErrors({ApiError.REPOSITORY_ISSUE}) - public String getFileFromPeer(@QueryParam("base58Digest") String base58Digest, - @QueryParam("peer") String targetPeerAddress) { + @ApiErrors({ApiError.REPOSITORY_ISSUE, ApiError.INVALID_DATA, ApiError.INVALID_CRITERIA, ApiError.FILE_NOT_FOUND, ApiError.NO_REPLY}) + public Response getFileFromPeer(@QueryParam("base58Digest") String base58Digest, + @QueryParam("peer") String targetPeerAddress) { try (final Repository repository = RepositoryManager.getRepository()) { @@ -190,12 +191,16 @@ public class DataResource { Message getDataFileMessage = new GetDataFileMessage(digest); Message message = targetPeer.getResponse(getDataFileMessage); - if (message == null || message.getType() != Message.MessageType.DATA_FILE) - return "invalid file received"; + if (message == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NO_REPLY); + } + else if (message.getType() == Message.MessageType.BLOCK_SUMMARIES) { // TODO: use dedicated message type here + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FILE_NOT_FOUND); + } DataFileMessage dataFileMessage = (DataFileMessage) message; - return String.format("Received file %s, size %d bytes", dataFileMessage.getDataFile(), dataFileMessage.getDataFile().size()); + return Response.ok(String.format("Received file %s, size %d bytes", dataFileMessage.getDataFile(), dataFileMessage.getDataFile().size())).build(); } catch (ApiException e) { throw e; } catch (DataException | InterruptedException e) { diff --git a/src/main/resources/i18n/ApiError_en.properties b/src/main/resources/i18n/ApiError_en.properties index 5acf2373..2a6ec002 100644 --- a/src/main/resources/i18n/ApiError_en.properties +++ b/src/main/resources/i18n/ApiError_en.properties @@ -64,3 +64,7 @@ TRANSACTION_UNKNOWN = transaction unknown TRANSFORMATION_ERROR = could not transform JSON into transaction UNAUTHORIZED = API call unauthorized + +FILE_NOT_FOUND = file not found + +NO_REPLY = peer didn't reply within the allowed time