diff --git a/src/main/java/org/folio/print/server/main/MainVerticle.java b/src/main/java/org/folio/print/server/main/MainVerticle.java index f98cc54..5cf716c 100644 --- a/src/main/java/org/folio/print/server/main/MainVerticle.java +++ b/src/main/java/org/folio/print/server/main/MainVerticle.java @@ -13,7 +13,6 @@ import org.apache.logging.log4j.Logger; import org.folio.okapi.common.Config; import org.folio.okapi.common.ModuleVersionReporter; -import org.folio.print.server.resources.BatchCreationResource; import org.folio.print.server.service.PrintService; import org.folio.tlib.RouterCreator; import org.folio.tlib.api.HealthApi; @@ -37,8 +36,7 @@ public void start(Promise promise) { RouterCreator[] routerCreators = { printServiceService, new Tenant2Api(printServiceService), - new HealthApi(), - new BatchCreationResource() + new HealthApi() }; RouterCreator.mountAll(vertx, routerCreators) diff --git a/src/main/java/org/folio/print/server/resources/BatchCreationResource.java b/src/main/java/org/folio/print/server/resources/BatchCreationResource.java deleted file mode 100644 index 12722c7..0000000 --- a/src/main/java/org/folio/print/server/resources/BatchCreationResource.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.folio.print.server.resources; - -import io.vertx.core.Future; -import io.vertx.core.Vertx; -import io.vertx.core.http.HttpServerResponse; -import io.vertx.core.json.JsonArray; -import io.vertx.ext.web.Router; -import io.vertx.ext.web.RoutingContext; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.util.List; -import java.util.UUID; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.pdfbox.util.Hex; -import org.folio.okapi.common.XOkapiHeaders; -import org.folio.print.server.data.PrintEntry; -import org.folio.print.server.data.PrintEntryType; -import org.folio.print.server.service.PdfService; -import org.folio.print.server.storage.PrintStorage; -import org.folio.tlib.RouterCreator; - -public class BatchCreationResource implements RouterCreator { - private static final Logger LOGGER = LogManager.getLogger(BatchCreationResource.class); - private static final int MAX_COUNT_IN_BATCH = 1000; - - @Override - public Future createRouter(Vertx vertx) { - Router router = Router.router(vertx); - router.post("/print/batch-creation").handler((ctx) -> process(ctx, vertx)) - .failureHandler(this::failureResponder); - return Future.succeededFuture(router); - } - - private void process(RoutingContext ctx, Vertx vertx) { - String tenant = ctx.request().getHeader(XOkapiHeaders.TENANT); - LOGGER.debug("process:: tenant " + tenant); - PrintStorage printStorage = new PrintStorage(vertx, tenant); - LocalDateTime localDateTime = LocalDateTime.now().with(LocalTime.MIDNIGHT); - - printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime - + " sortby sortingField created", 0, MAX_COUNT_IN_BATCH) - .onSuccess(l -> processListAndSaveResult(l, printStorage)) - .onFailure(e -> LOGGER.error("Failed to create print batch", e)); - ctx.response().setStatusCode(204); - ctx.response().end(); - } - - private void processListAndSaveResult(List entries, PrintStorage storage) { - if (!entries.isEmpty()) { - byte[] merged = PdfService.combinePdfFiles(entries); - PrintEntry batch = new PrintEntry(); - batch.setId(UUID.randomUUID()); - batch.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC)); - batch.setType(PrintEntryType.BATCH); - batch.setContent(Hex.getString(merged)); - storage.createEntry(batch); - } - } - - private void failureResponder(RoutingContext context) { - Throwable failure = context.failure(); - if (failure != null) { - if (StringUtils.isNotBlank(failure.getMessage())) { - internalError(context.response(), failure.getMessage()); - } else { - internalError(context.response(), failure.toString()); - } - } else { - internalError(context.response(), "Unknown failure occurred"); - } - } - - private static void internalError(HttpServerResponse response, String reason) { - response.setStatusCode(500); - response.putHeader("content-type", "text/plain; ISO_8859_1"); - if (reason != null) { - response.end(reason); - } else { - response.end(); - } - } -} diff --git a/src/main/java/org/folio/print/server/service/BatchCreationService.java b/src/main/java/org/folio/print/server/service/BatchCreationService.java new file mode 100644 index 0000000..1a43767 --- /dev/null +++ b/src/main/java/org/folio/print/server/service/BatchCreationService.java @@ -0,0 +1,54 @@ +package org.folio.print.server.service; + +import io.vertx.ext.web.RoutingContext; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.List; +import java.util.UUID; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.pdfbox.util.Hex; +import org.folio.okapi.common.XOkapiHeaders; +import org.folio.print.server.data.PrintEntry; +import org.folio.print.server.data.PrintEntryType; +import org.folio.print.server.storage.PrintStorage; + +public class BatchCreationService { + private static final Logger LOGGER = LogManager.getLogger(BatchCreationService.class); + private static final int MAX_COUNT_IN_BATCH = 1000; + + private BatchCreationService() { + } + + /** + * Process batch creation request. + * @param ctx Batch creation request context + */ + public static void process(RoutingContext ctx) { + String tenant = ctx.request().getHeader(XOkapiHeaders.TENANT); + LOGGER.debug("process:: tenant " + tenant); + PrintStorage printStorage = new PrintStorage(ctx.vertx(), tenant); + LocalDateTime localDateTime = LocalDateTime.now().with(LocalTime.MIDNIGHT); + + printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime + + " sortby sortingField created", 0, MAX_COUNT_IN_BATCH) + .onSuccess(l -> processListAndSaveResult(l, printStorage)) + .onFailure(e -> LOGGER.error("Failed to create print batch", e)); + ctx.response().setStatusCode(204); + ctx.response().end(); + } + + private static void processListAndSaveResult(List entries, PrintStorage storage) { + if (!entries.isEmpty()) { + byte[] merged = PdfService.combinePdfFiles(entries); + PrintEntry batch = new PrintEntry(); + batch.setId(UUID.randomUUID()); + batch.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC)); + batch.setType(PrintEntryType.BATCH); + batch.setContent(Hex.getString(merged)); + storage.createEntry(batch); + } + } +} diff --git a/src/main/java/org/folio/print/server/service/PrintService.java b/src/main/java/org/folio/print/server/service/PrintService.java index f6641a3..f5f69ce 100644 --- a/src/main/java/org/folio/print/server/service/PrintService.java +++ b/src/main/java/org/folio/print/server/service/PrintService.java @@ -3,7 +3,6 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.vertx.core.Future; import io.vertx.core.Vertx; -import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; @@ -113,6 +112,11 @@ private void handlers(RouterBuilder routerBuilder) { .onFailure(cause -> commonError(ctx, cause)) ) .failureHandler(this::failureHandler); + + routerBuilder + .operation("createBatch") + .handler(BatchCreationService::process) + .failureHandler(this::failureHandler); } static PrintStorage createFromParams(Vertx vertx, RequestParameters params) { diff --git a/src/main/resources/openapi/batchPrint.yaml b/src/main/resources/openapi/batchPrint.yaml index 31fdc80..591de1e 100644 --- a/src/main/resources/openapi/batchPrint.yaml +++ b/src/main/resources/openapi/batchPrint.yaml @@ -160,8 +160,9 @@ paths: description: > Send mail to create print entry. X-Okapi-Permissions must include mod-batch-print.print.write + operationId: createBatch responses: - "200": + "204": description: Print entry created "400": $ref: "#/components/responses/trait_400" @@ -169,19 +170,6 @@ paths: $ref: "#/components/responses/trait_403" "500": $ref: "#/components/responses/trait_500" - /print/fake: - parameters: - - $ref: headers/okapi-permissions.yaml - - $ref: headers/okapi-tenant.yaml - - $ref: headers/okapi-token.yaml - - $ref: headers/okapi-url.yaml - - $ref: headers/okapi-user.yaml - post: - description: > - Fake endpoint for service to work - responses: - "200": - description: Fake components: responses: trait_400: diff --git a/src/test/java/org/folio/print/server/main/MainVerticleTest.java b/src/test/java/org/folio/print/server/main/MainVerticleTest.java index c473ae3..f9897c9 100644 --- a/src/test/java/org/folio/print/server/main/MainVerticleTest.java +++ b/src/test/java/org/folio/print/server/main/MainVerticleTest.java @@ -70,7 +70,10 @@ public void testCrudGlobalOk() { .then() .statusCode(200) .contentType(ContentType.JSON) - .body(is(en.encode())); + .body("id", is(entry.getId().toString())) + .body("content", is(entry.getContent())) + .body("type", is(entry.getType().toString())) + .body("sortingField", is(entry.getSortingField())); entry.setContent("BB"); en = JsonObject.mapFrom(entry); diff --git a/src/test/java/org/folio/print/server/resources/BatchCreationResourceTest.java b/src/test/java/org/folio/print/server/service/BatchCreationServiceTest.java similarity index 93% rename from src/test/java/org/folio/print/server/resources/BatchCreationResourceTest.java rename to src/test/java/org/folio/print/server/service/BatchCreationServiceTest.java index 330b305..591bb3f 100644 --- a/src/test/java/org/folio/print/server/resources/BatchCreationResourceTest.java +++ b/src/test/java/org/folio/print/server/service/BatchCreationServiceTest.java @@ -1,4 +1,4 @@ -package org.folio.print.server.resources; +package org.folio.print.server.service; import io.restassured.RestAssured; import io.restassured.http.ContentType; @@ -15,7 +15,7 @@ import static org.hamcrest.Matchers.notNullValue; @RunWith(VertxUnitRunner.class) -public class BatchCreationResourceTest extends TestBase { +public class BatchCreationServiceTest extends TestBase { @Test public void createBatch() throws IOException { @@ -49,6 +49,7 @@ public void createBatch() throws IOException { .baseUri(MODULE_URL) .header(XOkapiHeaders.TENANT, TENANT_1) .header(XOkapiHeaders.PERMISSIONS, perm.encode()) + .contentType(ContentType.JSON) .post("/print/batch-creation") .then() .statusCode(204);