Mercurial > hg > anteater
diff src/de/mpiwg/anteater/species/scientific/impl/GNRDNameFinder.java @ 0:036535fcd179
anteater
author | jdamerow |
---|---|
date | Fri, 14 Sep 2012 10:30:43 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/mpiwg/anteater/species/scientific/impl/GNRDNameFinder.java Fri Sep 14 10:30:43 2012 +0200 @@ -0,0 +1,92 @@ +package de.mpiwg.anteater.species.scientific.impl; + +import java.net.URI; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriBuilder; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +import de.mpiwg.anteater.logging.IAnteaterLogger; +import de.mpiwg.anteater.species.scientific.IScientificNamesFinder; +import de.mpiwg.anteater.xml.INameFinderParser; +import de.mpiwg.anteater.xml.impl.GNRDXMLParser; + +public class GNRDNameFinder implements IScientificNamesFinder { + + public static final String STATUS_WAITING = "303"; + public static final String STATUS_DONE = "200"; + + public final static String COMPONENT_NAME = GNRDNameFinder.class.getSimpleName(); + + private IAnteaterLogger logger; + + public GNRDNameFinder(IAnteaterLogger logger) { + this.logger = logger; + } + + @Override + public String findScientificNames(String text) { + logger.logMessage(COMPONENT_NAME, "Asking GNRD service for names..."); + + ClientConfig config = new DefaultClientConfig(); + Client client = Client.create(config); + WebResource service = client.resource(getBaseURI()); + + Client clientPollCall = Client.create(config); + WebResource pollService = null; + + MultivaluedMap<String, String> values = new MultivaluedMapImpl(); + values.add("text", text); + + String result = service.accept(MediaType.TEXT_XML).post(String.class, values); + + while (true) { + INameFinderParser parser = new GNRDXMLParser(result); + String status = parser.getStatus(); + logger.logMessage(COMPONENT_NAME, "Polling URL: " + parser.getTokenURL()); + logger.logMessageWithoutNewLine(COMPONENT_NAME, "Polling for results..."); + + if (status.trim().equals(STATUS_WAITING)) { + logger.logMessage("waiting 10 seconds before retry."); + // wait 2 sec before polling + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if (pollService == null) { + String pollURL = parser.getTokenURL(); + pollService = clientPollCall.resource(createBaseURI(pollURL)); + } + + result = pollService.accept(MediaType.TEXT_XML).get(String.class); + } + else { + logger.logMessage("results received."); + break; + } + } + + if (result == null || result.trim().isEmpty()) + return null; + + return result; + } + + protected URI getBaseURI() { + return UriBuilder.fromUri("http://gnrd.globalnames.org/") + .path("name_finder.xml").build(); + } + + public URI createBaseURI(String uri) { + return UriBuilder.fromUri(uri).build(); + } +}