forked from Qortal/qortal
Better handling of data file responses in the /data/file/frompeer API endpoint.
This commit is contained in:
parent
33d9c51b6f
commit
5070c4eea9
@ -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<Integer, ApiError> map = stream(ApiError.values()).collect(toMap(apiError -> apiError.code, apiError -> apiError));
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user