use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.
the class GetOaiIdentifiersHelper method handle.
@Override
public Future<javax.ws.rs.core.Response> handle(Request request, Context ctx) {
Promise<javax.ws.rs.core.Response> promise = Promise.promise();
try {
ResponseHelper responseHelper = getResponseHelper();
List<OAIPMHerrorType> errors = validateRequest(request);
if (!errors.isEmpty()) {
OAIPMH oai;
if (request.isRestored()) {
oai = responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
} else {
oai = responseHelper.buildOaipmhResponseWithErrors(request, errors);
}
promise.complete(getResponseHelper().buildFailureResponse(oai, request));
return promise.future();
}
requestAndProcessSrsRecords(request, ctx, promise);
} catch (Exception e) {
handleException(promise, e);
}
return promise.future();
}
use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.
the class GetOaiIdentifiersHelper method buildListIdentifiers.
/**
* Builds {@link ListIdentifiersType} with headers if there is any item or {@code null}
*
* @param request request
* @param srsRecords the response from the storage which contains items
* @return {@link ListIdentifiersType} with headers if there is any or {@code null}
*/
private OAIPMH buildListIdentifiers(Request request, JsonObject srsRecords) {
ResponseHelper responseHelper = getResponseHelper();
JsonArray instances = storageHelper.getItems(srsRecords);
Integer totalRecords = storageHelper.getTotalRecords(srsRecords);
if (request.isRestored() && !canResumeRequestSequence(request, totalRecords, instances)) {
return responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FLOW_ERROR);
}
if (instances != null && !instances.isEmpty()) {
logger.debug("{} entries retrieved out of {}.", instances.size(), totalRecords);
ListIdentifiersType identifiers = new ListIdentifiersType().withResumptionToken(buildResumptionToken(request, instances, totalRecords));
String identifierPrefix = request.getIdentifierPrefix();
instances.stream().map(object -> (JsonObject) object).filter(instance -> StringUtils.isNotEmpty(storageHelper.getIdentifierId(instance))).filter(instance -> filterInstance(request, instance)).map(instance -> addHeader(identifierPrefix, request, instance)).forEach(identifiers::withHeaders);
if (identifiers.getHeaders().isEmpty()) {
OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request);
return oaipmh.withErrors(createNoRecordsFoundError());
}
ResumptionTokenType resumptionToken = buildResumptionToken(request, instances, totalRecords);
OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request).withListIdentifiers(identifiers);
addResumptionTokenToOaiResponse(oaipmh, resumptionToken);
return oaipmh;
}
return responseHelper.buildOaipmhResponseWithErrors(request, createNoRecordsFoundError());
}
use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.
the class MarcWithHoldingsRequestHelper method buildRecordsResponse.
private Future<Response> buildRecordsResponse(Request request, String requestId, List<JsonObject> batch, Map<String, JsonObject> srsResponse, boolean firstBatch, String nextInstanceId, boolean deletedRecordSupport) {
Promise<Response> promise = Promise.promise();
try {
List<RecordType> records = buildRecordsList(request, batch, srsResponse, deletedRecordSupport);
logger.debug("Build records response, instances = {}, instances with srs records = {}.", batch.size(), records.size());
ResponseHelper responseHelper = getResponseHelper();
OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request);
if (records.isEmpty() && nextInstanceId == null && firstBatch) {
oaipmh.withErrors(createNoRecordsFoundError());
} else {
oaipmh.withListRecords(new ListRecordsType().withRecords(records));
}
Response response;
if (oaipmh.getErrors().isEmpty()) {
if (!firstBatch || nextInstanceId != null) {
ResumptionTokenType resumptionToken = buildResumptionTokenFromRequest(request, requestId, records.size(), nextInstanceId);
oaipmh.getListRecords().withResumptionToken(resumptionToken);
}
response = responseHelper.buildSuccessResponse(oaipmh);
} else {
response = responseHelper.buildFailureResponse(oaipmh, request);
}
promise.complete(response);
} catch (Exception e) {
handleException(promise, e);
}
return promise.future();
}
use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.
the class OaiPmhImpl method getFutureWithErrorResponse.
private Future<Response> getFutureWithErrorResponse(Throwable t, Request request) {
final Response errorResponse;
if (t instanceof IllegalArgumentException) {
final ResponseHelper rh = ResponseHelper.getInstance();
OAIPMH oaipmh = rh.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
errorResponse = rh.buildFailureResponse(oaipmh, request);
} else {
errorResponse = GetOaiRecordsResponse.respond500WithTextPlain(t.getMessage());
}
return succeededFuture(errorResponse);
}
use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.
the class GetOaiSetsHelper method handle.
@Override
public Future<Response> handle(Request request, Context ctx) {
Promise<Response> promise = Promise.promise();
ResponseHelper responseHelper = getResponseHelper();
try {
OAIPMH oai;
if (request.getResumptionToken() != null) {
oai = responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
promise.complete(responseHelper.buildFailureResponse(oai, request));
return promise.future();
}
oai = responseHelper.buildBaseOaipmhResponse(request).withListSets(new ListSetsType().withSets(getSupportedSetTypes()));
promise.complete(responseHelper.buildSuccessResponse(oai));
} catch (Exception e) {
logger.error("Error happened while processing ListSets verb request.", e);
promise.fail(e);
}
return promise.future();
}
Aggregations