Skip to content

Commit

Permalink
EDGOAIPMH-61: 503 status returned after several initial harvests (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlliaDaliek authored Apr 14, 2021
1 parent eaa5a01 commit f8325b9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/folio/edge/oaipmh/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.folio.edge.core.Constants;
import org.folio.edge.core.EdgeVerticle2;
import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClientFactory;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClientFactory;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
Expand Down
45 changes: 26 additions & 19 deletions src/main/java/org/folio/edge/oaipmh/OaiPmhHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.folio.edge.core.Handler;
import org.folio.edge.core.security.SecureStore;
import org.folio.edge.core.utils.OkapiClientFactory;
import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClient;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClient;

import com.google.common.collect.Iterables;

Expand Down Expand Up @@ -51,7 +51,9 @@ protected void handle(RoutingContext ctx) {

handleCommon(ctx, new String[0], new String[0] , (okapiClient, params) -> {
final OaiPmhOkapiClient oaiPmhClient = new OaiPmhOkapiClient(okapiClient);
oaiPmhClient.call(request.params(), request.headers(), response -> handleProxyResponse(ctx, response), t -> oaiPmhFailureHandler(ctx, t));
oaiPmhClient.call(request.params(), request.headers(),
response -> handleProxyResponse(ctx, response, oaiPmhClient),
throwable -> oaiPmhFailureHandler(ctx, throwable, oaiPmhClient));
});
}

Expand Down Expand Up @@ -83,32 +85,36 @@ private boolean supportedAcceptHeaders(HttpServerRequest request) {
* This method contains processing of oai-pmh-mod response in normal processing flow
*
* @param ctx routing context
* @param response populated http-response
* @param oaiPmhResponse populated http-response
*/
@Override
protected void handleProxyResponse(RoutingContext ctx, HttpClientResponse response) {
protected void handleProxyResponse(RoutingContext ctx, HttpClientResponse oaiPmhResponse, OaiPmhOkapiClient okapiClient) {
HttpServerResponse edgeResponse = ctx.response();
int httpStatusCode = response.statusCode();
ctx.response().setStatusCode(response.statusCode());
int httpStatusCode = oaiPmhResponse.statusCode();
ctx.response().setStatusCode(oaiPmhResponse.statusCode());
if (EXPECTED_CODES.contains(httpStatusCode)) {
edgeResponse.putHeader(HttpHeaders.CONTENT_TYPE, TEXT_XML);
// In case the repository logic already compressed the response, lets transfer header to avoid potential doubled compression
Optional<String> encodingHeader = Optional.ofNullable(response.getHeader(HttpHeaders.CONTENT_ENCODING));
Optional<String> encodingHeader = Optional.ofNullable(oaiPmhResponse.getHeader(HttpHeaders.CONTENT_ENCODING));
encodingHeader.ifPresent(value -> edgeResponse.putHeader(HttpHeaders.CONTENT_ENCODING, value));
/*
* Using bodyHandler to wait for full response body to be read. Alternative option is to use endHandler and pumping i.e.
* Pump.pump(response, ctx.response()).start() but this requires chunked response (ctx.response().setChunked(true)) and there
* is no any guarantee that all harvesters support such responses.
*/
response.bodyHandler(buffer -> {
oaiPmhResponse.bodyHandler(buffer -> {
edgeResponse.end(buffer);
if (!encodingHeader.isPresent()) {
log.debug("Response from oai-pmh headers:{} \n {}", Iterables.toString(response.headers()), buffer);
if (encodingHeader.isEmpty()) {
log.debug("Returned oai-pmh response doesn't contain encoding header.");
}
log.debug("Edge response headers: {}", Iterables.toString(edgeResponse.headers()));
okapiClient.close();
});
oaiPmhResponse.exceptionHandler(throwable -> {
log.error("Exception occurred while getting oai-pmh response.", throwable);
okapiClient.close();
});
} else {
log.error(String.format("Error in the response from repository: (%d)", response.statusCode()));
log.error("Error in the response from repository: status code - {}, response status message - {}", oaiPmhResponse.statusCode(), oaiPmhResponse.statusMessage());
okapiClient.close();
internalServerError(ctx, "Internal Server Error");
}
}
Expand All @@ -129,14 +135,15 @@ protected void requestTimeout(RoutingContext ctx, String msg) {
* This handler-method for processing exceptional flow
*
* @param ctx current routing context
* @param t throwable object
* @param throwable throwable object
*/
public void oaiPmhFailureHandler(RoutingContext ctx, Throwable t) {
log.error("Exception in calling OKAPI", t);
if (t instanceof TimeoutException) {
requestTimeout(ctx, t.getMessage());
private void oaiPmhFailureHandler(RoutingContext ctx, Throwable throwable, OaiPmhOkapiClient client) {
log.error("Exception in calling OKAPI", throwable);
client.close();
if (throwable instanceof TimeoutException) {
requestTimeout(ctx, throwable.getMessage());
} else {
internalServerError(ctx, t != null? t.getMessage(): "");
internalServerError(ctx, throwable != null? throwable.getMessage(): "");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.folio.edge.oaipmh.clients.aoipmh;
package org.folio.edge.oaipmh.clients;

import static io.vertx.core.http.HttpHeaders.ACCEPT;
import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.folio.edge.oaipmh.clients.aoipmh;
package org.folio.edge.oaipmh.clients;

import org.folio.edge.core.utils.OkapiClientFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import io.vertx.core.Vertx;

import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClient;
import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClientFactory;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClient;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClientFactory;
import org.junit.Before;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.folio.edge.core.utils.test.TestUtils;
import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClient;
import org.folio.edge.oaipmh.clients.aoipmh.OaiPmhOkapiClientFactory;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClient;
import org.folio.edge.oaipmh.clients.OaiPmhOkapiClientFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down

0 comments on commit f8325b9

Please sign in to comment.