From 2089183bae40d77bf506a2362d0a21521f470fcd Mon Sep 17 00:00:00 2001 From: yichen88 Date: Mon, 23 Mar 2020 13:37:34 +0100 Subject: [PATCH 1/2] Fix response status if not found (#39) Signed-off-by: yichen88 --- .../com/powsybl/afs/ws/client/utils/ClientUtils.java | 10 ++-------- .../com/powsybl/afs/ws/server/AppStorageServer.java | 8 ++++---- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java b/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java index 12bc8800..3fc80f66 100644 --- a/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java +++ b/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java @@ -93,7 +93,6 @@ public static T readEntityIfOk(Response response, Class entityType) { } else { throw createUnexpectedResponseStatus(status); } - } public static T readEntityIfOk(Response response, GenericType entityType) { @@ -114,16 +113,11 @@ public static T readEntityIfOk(Response response, GenericType entityType) public static Optional readOptionalEntityIfOk(Response response, Class entityType) { Response.Status status = Response.Status.fromStatusCode(response.getStatus()); - if (status == Response.Status.OK) { - return Optional.of(readEntityAndLog(response, entityType)); - } else if (status == Response.Status.NO_CONTENT) { + if (status == Response.Status.NO_CONTENT || status == Response.Status.NOT_FOUND) { LOGGER.trace(" --> null"); return Optional.empty(); - } else if (status == Response.Status.INTERNAL_SERVER_ERROR) { - throw createServerErrorException(response); - } else { - throw createUnexpectedResponseStatus(status); } + return Optional.of(readEntityIfOk(response, entityType)); } public static UserSession authenticate(URI baseUri, String login, String password) { diff --git a/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/AppStorageServer.java b/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/AppStorageServer.java index 18df54d6..2d3f2cf1 100644 --- a/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/AppStorageServer.java +++ b/afs-ws/afs-ws-server/src/main/java/com/powsybl/afs/ws/server/AppStorageServer.java @@ -13,8 +13,8 @@ import com.powsybl.afs.storage.NodeGenericMetadata; import com.powsybl.afs.storage.NodeInfo; import com.powsybl.afs.storage.buffer.*; -import com.powsybl.afs.ws.server.utils.JwtTokenNeeded; import com.powsybl.afs.ws.server.utils.AppDataBean; +import com.powsybl.afs.ws.server.utils.JwtTokenNeeded; import com.powsybl.afs.ws.utils.AfsRestApi; import com.powsybl.afs.ws.utils.gzip.Compress; import com.powsybl.timeseries.DoubleDataChunk; @@ -109,7 +109,7 @@ public Response createNode(@ApiParam(value = "File system name") @PathParam("fil @ApiParam(value = "Version") @QueryParam("version") int version, @ApiParam(value = "Node Meta Data") NodeGenericMetadata nodeMetadata) { AppStorage storage = appDataBean.getStorage(fileSystemName); - NodeInfo newNodeInfo = storage.createNode(nodeId, childName, nodePseudoClass, description, version, nodeMetadata); + NodeInfo newNodeInfo = storage.createNode(nodeId, childName, nodePseudoClass, description, version, nodeMetadata); return Response.ok().entity(newNodeInfo).build(); } @@ -138,7 +138,7 @@ public Response getParentNode(@ApiParam(value = "File system name") @PathParam(" if (parentNodeInfo.isPresent()) { return Response.ok().entity(parentNodeInfo.get()).build(); } else { - return Response.noContent().build(); + return Response.status(Status.NOT_FOUND).build(); } } @@ -155,7 +155,7 @@ public Response getChildNode(@ApiParam(value = "File system name") @PathParam("f if (childNodeInfo.isPresent()) { return Response.status(Status.OK).entity(childNodeInfo.get()).build(); } else { - return Response.status(Status.NO_CONTENT).build(); + return Response.status(Status.NOT_FOUND).build(); } } From 62f32037e2cad5bb2d82a9a7af4b93a387b2a130 Mon Sep 17 00:00:00 2001 From: yichen88 Date: Tue, 16 Jun 2020 16:15:20 +0200 Subject: [PATCH 2/2] Add comments Signed-off-by: yichen88 --- .../main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java b/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java index 3fc80f66..e6b25c77 100644 --- a/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java +++ b/afs-ws/afs-ws-client-utils/src/main/java/com/powsybl/afs/ws/client/utils/ClientUtils.java @@ -113,6 +113,8 @@ public static T readEntityIfOk(Response response, GenericType entityType) public static Optional readOptionalEntityIfOk(Response response, Class entityType) { Response.Status status = Response.Status.fromStatusCode(response.getStatus()); + // the NO_CONTENT case is for backwards compatibility. + // The remote AppStorageServer may still runs on an old version which response with 204 if it not found resources. if (status == Response.Status.NO_CONTENT || status == Response.Status.NOT_FOUND) { LOGGER.trace(" --> null"); return Optional.empty();