Skip to content

Commit

Permalink
MODOAIPMH-553 - Request has expired error on downloading error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
siarhei-charniak committed Apr 4, 2024
1 parent 3b9e750 commit 90b58ec
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
13 changes: 12 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
"pathPattern": "/oai/request-metadata/{requestId}/suppressed-from-discovery-instances",
"permissionsRequired": ["oai-pmh.request-metadata.suppressed-from-discovery-instances.collection.get"]
},
{
"methods": ["GET"],
"pathPattern": "/oai/request-metadata/{requestId}/logs",
"permissionsRequired": ["oai-pmh.request-metadata.logs.item.get"]
},
{
"methods": [
"POST"
Expand Down Expand Up @@ -273,6 +278,11 @@
"displayName": "OAI-PMH Request metadata - get collection of suppressed from discovery instances UUIDs",
"description": "Retrieves request metadata collection - suppressed from discovery instances UUIDs"
},
{
"permissionName": "oai-pmh.request-metadata.logs.item.get",
"displayName": "OAI-PMH Request metadata - download error log",
"description": "Downloads error log by request id"
},
{
"permissionName": "oai-pmh.all",
"displayName": "OAI-PMH - all permissions",
Expand All @@ -289,7 +299,8 @@
"oai-pmh.request-metadata.failed-to-save-instances.collection.get",
"oai-pmh.request-metadata.failed-instances.collection.get",
"oai-pmh.request-metadata.skipped-instances.collection.get",
"oai-pmh.request-metadata.suppressed-from-discovery-instances.collection.get"
"oai-pmh.request-metadata.suppressed-from-discovery-instances.collection.get",
"oai-pmh.request-metadata.logs.item.get"
]
},
{
Expand Down
18 changes: 18 additions & 0 deletions ramls/request-metadata.raml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ resourceTypes:
get:
description: Get list of suppressed from discovery instances UUIDs
is: [ pageable ]

/{requestId}/logs:
description: Service that allows to retrieve error log by request id
get:
responses:
200:
body:
binary/octet-stream:
404:
description: "Not found"
body:
text/plain:
example: "Not found"
500:
description: "Internal server error, e.g. due to misconfiguration"
body:
text/plain:
example: "Internal server error, contact administrator"
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@
import org.folio.rest.jooq.tables.records.RequestMetadataLbRecord;
import org.folio.rest.jooq.tables.records.SkippedInstancesIdsRecord;
import org.folio.rest.jooq.tables.records.SuppressedFromDiscoveryInstancesIdsRecord;
import org.folio.s3.client.FolioS3Client;
import org.jooq.InsertValuesStep3;
import org.jooq.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import io.github.jklingsporn.vertx.jooq.classic.reactivepg.ReactiveClassicGenericQueryExecutor;
Expand All @@ -69,9 +67,6 @@ public InstancesDaoImpl(PostgresClientFactory postgresClientFactory) {
this.postgresClientFactory = postgresClientFactory;
}

@Autowired
private FolioS3Client folioS3Client;

@Override
public Future<List<String>> getExpiredRequestIds(String tenantId, long expirationPeriodInSeconds) {
OffsetDateTime offsetDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())
Expand Down Expand Up @@ -480,8 +475,7 @@ private RequestMetadata rowToRequestMetadata(Row row) {
of(pojo.getSuppressedInstancesCounter()).ifPresent(requestMetadata::withSuppressedInstancesCounter);
ofNullable(pojo.getPathToErrorFileInS3()).ifPresentOrElse(pathToError -> {
if (!pathToError.isEmpty()) {
var regeneratedLink = folioS3Client.getPresignedUrl(pathToError);
requestMetadata.withLinkToErrorFile(regeneratedLink);
requestMetadata.withLinkToErrorFile(pathToError);
} else {
requestMetadata.setLinkToErrorFile("");
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/folio/rest/impl/RequestMetadataAPIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.util.Map;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dataimport.util.ExceptionHelper;
import org.folio.oaipmh.dao.InstancesDao;
import org.folio.rest.jaxrs.resource.OaiRequestMetadata;
import org.folio.rest.jooq.tables.pojos.RequestMetadataLb;
import org.folio.rest.tools.utils.TenantTool;
import org.folio.s3.client.FolioS3Client;
import org.folio.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -24,11 +29,15 @@ public class RequestMetadataAPIs implements OaiRequestMetadata {
private static final Logger logger = LogManager.getLogger(RequestMetadataAPIs.class);
private static final String REQUEST_METADATA_ERROR_MESSAGE_TEMPLATE = "Error occurred while get request metadata. Message: {}.";
private static final String UUID_COLLECTION_ERROR_MESSAGE_TEMPLATE = "Error occurred while get UUIDs collection. Message: {}.";
private static final String DOWNLOAD_LOG_ERROR_MESSAGE_TEMPLATE = "Error occurred while downloading log. Message: {}.";


@Autowired
InstancesDao instancesDao;

@Autowired
FolioS3Client folioS3Client;

public RequestMetadataAPIs() {
SpringContextUtil.autowireDependencies(this, Vertx.currentContext());
}
Expand Down Expand Up @@ -116,4 +125,31 @@ public void getOaiRequestMetadataSuppressedFromDiscoveryInstancesByRequestId(Str
asyncResultHandler.handle(Future.succeededFuture(ExceptionHelper.mapExceptionToResponse(e)));
}
}

@Override
public void getOaiRequestMetadataLogsByRequestId(String requestId, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
vertxContext.runOnContext(v -> {
try {
var tenantId = TenantTool.tenantId(okapiHeaders);
logger.info("Download error log for tenant: {}, requestId: {}", tenantId, requestId);
instancesDao.getRequestMetadataByRequestId(requestId, tenantId)
.map(RequestMetadataLb::getLinkToErrorFile)
.map(this::toResponse)
.map(Response.class::cast)
.otherwise(ExceptionHelper::mapExceptionToResponse)
.onComplete(asyncResultHandler);
} catch (Exception e) {
logger.error(DOWNLOAD_LOG_ERROR_MESSAGE_TEMPLATE, e.getMessage());
asyncResultHandler.handle(Future.succeededFuture(ExceptionHelper.mapExceptionToResponse(e)));
}
});
}

private GetOaiRequestMetadataLogsByRequestIdResponse toResponse(String fileName) {
logger.info("Filename: {}", fileName);
var response = GetOaiRequestMetadataLogsByRequestIdResponse.respond200WithBinaryOctetStream(folioS3Client.read(fileName));
var headers = response.getHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.utils.ModuleName;
import org.folio.rest.tools.utils.NetworkUtils;
import org.folio.s3.client.FolioS3Client;
import org.folio.spring.SpringContextUtil;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class ErrorsServiceImplTest extends AbstractErrorsTest {
public static final String BUCKET = "test-bucket";
public static final String REGION = "us-west-2";

@Autowired
private FolioS3Client folioS3Client;

static {
s3 = new GenericContainer<>("minio/minio:latest")
.withEnv("MINIO_ACCESS_KEY", S3_ACCESS_KEY)
Expand Down Expand Up @@ -271,7 +275,7 @@ void shouldDeleteErrorsByRequestId_whenErrorFound(VertxTestContext testContext)
}

private void verifyErrorCSVFile(String linkToError, List<String> initErrorFileContent) {
try (InputStream inputStream = new URL(linkToError).openStream();
try (InputStream inputStream = folioS3Client.read(linkToError);
Scanner scanner = new Scanner(inputStream)) {
List<String> listCsvLines = new ArrayList<>();
while (scanner.hasNextLine()) {
Expand Down

0 comments on commit 90b58ec

Please sign in to comment.