From 5fc8ad76a1b34aef054ffe766a975c1f97e12a50 Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Thu, 20 Jun 2024 15:14:26 +0200 Subject: [PATCH] Made it forward compatible. --- .../api/client/utils/PageUpdateApiUtil.java | 68 +++++++++++-------- .../utils/PageUpdateApiClientUtilTest.java | 9 +-- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/client-extras/src/main/java/nl/vpro/api/client/utils/PageUpdateApiUtil.java b/client-extras/src/main/java/nl/vpro/api/client/utils/PageUpdateApiUtil.java index fc2ba7b9..43c8922b 100644 --- a/client-extras/src/main/java/nl/vpro/api/client/utils/PageUpdateApiUtil.java +++ b/client-extras/src/main/java/nl/vpro/api/client/utils/PageUpdateApiUtil.java @@ -1,7 +1,6 @@ package nl.vpro.api.client.utils; -import lombok.Getter; -import lombok.Setter; +import lombok.*; import lombok.extern.slf4j.Slf4j; import java.io.*; @@ -15,8 +14,7 @@ import javax.validation.constraints.NotNull; import javax.ws.rs.NotFoundException; import javax.ws.rs.ProcessingException; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.*; import javax.xml.bind.JAXB; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -111,11 +109,16 @@ public PageUpdate get(@NotNull String url) { public Result delete(@NotNull String id) { limiter.acquire(); PageIdMatch match = id.startsWith("crid:") ? PageIdMatch.CRID : PageIdMatch.URL; - try { - return handleResponse( - pageUpdateApiClient.getPageUpdateRestService() - .delete(id, false, 1, false, match, null, null), id, STRING, DeleteResult.class - ); + try (Response delete = pageUpdateApiClient.getPageUpdateRestService() + .delete(id, false, 1, false, match, null, null)) { + Result result = handleResponse(delete, id, STRING, String.class); + DeleteResult entity = fromString(result.getEntity(), delete.getHeaderString("content-type")); + return Result.builder() + .status(result.getStatus()) + .errors(result.getErrors()) + .entity(entity) + .build(); + } catch (ProcessingException e) { return exceptionToResult(e); } @@ -130,23 +133,23 @@ public Result deleteWhereStartsWith(@NotNull String prefix) { try { DeleteResult result = null; while (true) { - Result r = handleResponse( - pageUpdateApiClient.getPageUpdateRestService() - .delete(prefix, true, batchSize, true, match, null,null), prefix, STRING, String.class - ); - log.info("Batch deleted {}: {}", prefix, r); - DeleteResult entity = fromString(r.getEntity()); - if (result == null) { - result = entity; - } else { - result = result.and(entity); - } - if (r.isOk()) { - if (entity.getCount() == 0) { - return Result.builder() - .entity(result) - .status(Result.Status.SUCCESS) - .build(); + try(Response delete = pageUpdateApiClient.getPageUpdateRestService() + .delete(prefix, true, batchSize, true, match, null, null)) { + Result r = handleResponse(delete, prefix, STRING, String.class); + log.info("Batch deleted {}: {}", prefix, r); + DeleteResult entity = fromString(r.getEntity(), delete.getHeaderString("content-type")); + if (result == null) { + result = entity; + } else { + result = result.and(entity); + } + if (r.isOk()) { + if (entity.getCount() == 0) { + return Result.builder() + .entity(result) + .status(Result.Status.SUCCESS) + .build(); + } } } } @@ -156,13 +159,22 @@ public Result deleteWhereStartsWith(@NotNull String prefix) { } } - private DeleteResult fromString(String string) { + @SneakyThrows + private DeleteResult fromString(String string, String contentType) { if (string == null) { return null; } - return JAXB.unmarshal(new StringReader(string.replaceAll("deleteResult", "deleteresult")), DeleteResult.class); + if (MediaType.APPLICATION_XML_TYPE.isCompatible(MediaType.valueOf(contentType))) { + return JAXB.unmarshal(new StringReader( + string.replaceAll("deleteResult", "deleteresult") + ), + DeleteResult.class); + } else { + return Jackson2Mapper.getInstance().readerFor(DeleteResult.class).readValue(string); + } } + public Optional getPublishedPage(String url) { try { return Optional.of(getPageUpdateApiClient().getProviderRestService().getPage(url)); diff --git a/client-extras/src/test/java/nl/vpro/api/client/utils/PageUpdateApiClientUtilTest.java b/client-extras/src/test/java/nl/vpro/api/client/utils/PageUpdateApiClientUtilTest.java index 1367e4d2..c7bb266f 100644 --- a/client-extras/src/test/java/nl/vpro/api/client/utils/PageUpdateApiClientUtilTest.java +++ b/client-extras/src/test/java/nl/vpro/api/client/utils/PageUpdateApiClientUtilTest.java @@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; -@Disabled("This required running server at publish-dev") +@Disabled("This required running server at publish-test") @Slf4j public class PageUpdateApiClientUtilTest { @@ -26,8 +26,9 @@ public class PageUpdateApiClientUtilTest { @BeforeEach public void setUp() { PageUpdateApiClient clients = PageUpdateApiClient - .configured(Env.LOCALHOST) - .accept(MediaType.APPLICATION_JSON_TYPE) + .configured(Env.ACC) + //.accept(MediaType.APPLICATION_JSON_TYPE) + .accept(MediaType.APPLICATION_XML_TYPE) .build(); util = PageUpdateApiUtil.builder().client(clients).build(); } @@ -37,7 +38,7 @@ public void testSaveInvalid() { PageUpdate instance = new PageUpdate(PageType.ARTICLE, "http://vpro.nl/test"); Result result = util.saveAndWait(instance); assertThat(result.getStatus()).isEqualTo(Result.Status.INVALID); - assertThat(result.getErrors()).contains("may not be null"); + assertThat(result.getErrors()).contains("must not be null"); } @Test