Skip to content

Commit

Permalink
Made it forward compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Jun 20, 2024
1 parent 97fe9bf commit 5fc8ad7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -111,11 +109,16 @@ public PageUpdate get(@NotNull String url) {
public Result<DeleteResult> 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<String> result = handleResponse(delete, id, STRING, String.class);
DeleteResult entity = fromString(result.getEntity(), delete.getHeaderString("content-type"));
return Result.<DeleteResult>builder()
.status(result.getStatus())
.errors(result.getErrors())
.entity(entity)
.build();

} catch (ProcessingException e) {
return exceptionToResult(e);
}
Expand All @@ -130,23 +133,23 @@ public Result<DeleteResult> deleteWhereStartsWith(@NotNull String prefix) {
try {
DeleteResult result = null;
while (true) {
Result<String> 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.<DeleteResult>builder()
.entity(result)
.status(Result.Status.SUCCESS)
.build();
try(Response delete = pageUpdateApiClient.getPageUpdateRestService()
.delete(prefix, true, batchSize, true, match, null, null)) {
Result<String> 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.<DeleteResult>builder()
.entity(result)
.status(Result.Status.SUCCESS)
.build();
}
}
}
}
Expand All @@ -156,13 +159,22 @@ public Result<DeleteResult> 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<Page> getPublishedPage(String url) {
try {
return Optional.of(getPageUpdateApiClient().getProviderRestService().getPage(url));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}
Expand All @@ -37,7 +38,7 @@ public void testSaveInvalid() {
PageUpdate instance = new PageUpdate(PageType.ARTICLE, "http://vpro.nl/test");
Result<Void> 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
Expand Down

0 comments on commit 5fc8ad7

Please sign in to comment.