Better handling of data file responses in the /data/file/frompeer API endpoint.

This commit is contained in:
CalDescent 2021-06-19 20:23:33 +01:00
parent 33d9c51b6f
commit 5070c4eea9
3 changed files with 21 additions and 8 deletions

View File

@ -129,7 +129,11 @@ public enum ApiError {
// Foreign blockchain // Foreign blockchain
FOREIGN_BLOCKCHAIN_NETWORK_ISSUE(1201, 500), FOREIGN_BLOCKCHAIN_NETWORK_ISSUE(1201, 500),
FOREIGN_BLOCKCHAIN_BALANCE_ISSUE(1202, 402), 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<Integer, ApiError> map = stream(ApiError.values()).collect(toMap(apiError -> apiError.code, apiError -> apiError)); private static final Map<Integer, ApiError> map = stream(ApiError.values()).collect(toMap(apiError -> apiError.code, apiError -> apiError));

View File

@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List; import java.util.List;
@ -150,8 +151,8 @@ public class DataResource {
) )
} }
) )
@ApiErrors({ApiError.REPOSITORY_ISSUE}) @ApiErrors({ApiError.REPOSITORY_ISSUE, ApiError.INVALID_DATA, ApiError.INVALID_CRITERIA, ApiError.FILE_NOT_FOUND, ApiError.NO_REPLY})
public String getFileFromPeer(@QueryParam("base58Digest") String base58Digest, public Response getFileFromPeer(@QueryParam("base58Digest") String base58Digest,
@QueryParam("peer") String targetPeerAddress) { @QueryParam("peer") String targetPeerAddress) {
try (final Repository repository = RepositoryManager.getRepository()) { try (final Repository repository = RepositoryManager.getRepository()) {
@ -190,12 +191,16 @@ public class DataResource {
Message getDataFileMessage = new GetDataFileMessage(digest); Message getDataFileMessage = new GetDataFileMessage(digest);
Message message = targetPeer.getResponse(getDataFileMessage); Message message = targetPeer.getResponse(getDataFileMessage);
if (message == null || message.getType() != Message.MessageType.DATA_FILE) if (message == null) {
return "invalid file received"; 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; 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) { } catch (ApiException e) {
throw e; throw e;
} catch (DataException | InterruptedException e) { } catch (DataException | InterruptedException e) {

View File

@ -64,3 +64,7 @@ TRANSACTION_UNKNOWN = transaction unknown
TRANSFORMATION_ERROR = could not transform JSON into transaction TRANSFORMATION_ERROR = could not transform JSON into transaction
UNAUTHORIZED = API call unauthorized UNAUTHORIZED = API call unauthorized
FILE_NOT_FOUND = file not found
NO_REPLY = peer didn't reply within the allowed time