Skip to content

HTTP client

Piotr Andzel edited this page May 8, 2019 · 6 revisions

HTTP client is a ready to use component designed to send requests to the end points supporting OAI-PMH protocol. It returns responses already translated into one of several objects defined in core packages.

Features

Usage

  1. The simplest way to use OAI-PMH HTTP client is to create an instance of a client with the URL of the OAI-PMH service, then send a request, receive and finally interpret response, for example:
// use 'Closeable' interface within 'try with resources' phrase to automatically close the client
try ( Client oaiClient = new Client(new URL("http://example.com/oai")); ) {

   // create instance of the 'ListIdentifiers' request
   ListIdentifiersRequest request = new ListIdentifiersRequest("oai_dc", null, null, null);

   do {
      // send the request
      Response genericResponse = oaiClient.execute(request);

      // check if response is in a right format; break the loop if not
      if (!(genericResponse instance of ListIdentifiersResponse)) {
         break;
      }

      // cast to the actual response
      ListIdentifiersResponse actualResponse = (ListIdentifiersResponse)genericResponse;

      // iterate through headers
      Arrays.stream(actualResponse.headers).forEach(header -> {
         // do something with header
      });

      // check resumption token; break the loop if empty
      if (actualResponse.resumptionToken==null) {
         break;
      }

      // create another request with resumption token and repeat the loop to send it again
      request = new ListIdentifiersRequest(actualResponse.resumptionToken);

   } while(true);

} catch (ProtocolResponse protocolEx) {
   // handle protocol exceptions here, like badVerb or cannotDisseminateFormat, etc.
} catch (URISyntaxException | IOException ex) {
   // handle all other exceptions
}
  1. Modification of the previous request but without the respect to the robots.txt:
// decide to ignore robots.txt
boolean ignoreRobots = true;
try ( Client oaiClient = new Client(new URL("http://example.com/oai"), ignoreRobots); ) {
   ...
}
  1. Using custom Apache HTTP Client instance:
// create and customize HTTP client
CloseableHttpClient customHttpClient = HttpClients.createSystem();
try ( Client oaiClient = new Client(customHttpClient, new URL("http://example.com/oai")); ) {
   ...
}

next...

Clone this wiki locally