Browse Source

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

qdn
CalDescent 3 years ago
parent
commit
5070c4eea9
  1. 8
      src/main/java/org/qortal/api/ApiError.java
  2. 17
      src/main/java/org/qortal/api/resource/DataResource.java
  3. 4
      src/main/resources/i18n/ApiError_en.properties

8
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<Integer, ApiError> map = stream(ApiError.values()).collect(toMap(apiError -> apiError.code, apiError -> apiError));
@ -157,4 +161,4 @@ public enum ApiError {
return this.status;
}
}
}

17
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) {

4
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

Loading…
Cancel
Save