Mercurial > hg > anteater
comparison src/de/mpiwg/anteater/places/impl/PlacemakerPlaceFinder.java @ 0:036535fcd179
anteater
author | jdamerow |
---|---|
date | Fri, 14 Sep 2012 10:30:43 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:036535fcd179 |
---|---|
1 package de.mpiwg.anteater.places.impl; | |
2 | |
3 import java.net.URI; | |
4 | |
5 import javax.ws.rs.core.MediaType; | |
6 import javax.ws.rs.core.MultivaluedMap; | |
7 import javax.ws.rs.core.UriBuilder; | |
8 | |
9 import com.sun.jersey.api.client.Client; | |
10 import com.sun.jersey.api.client.UniformInterfaceException; | |
11 import com.sun.jersey.api.client.WebResource; | |
12 import com.sun.jersey.api.client.config.ClientConfig; | |
13 import com.sun.jersey.api.client.config.DefaultClientConfig; | |
14 import com.sun.jersey.core.util.MultivaluedMapImpl; | |
15 | |
16 import de.mpiwg.anteater.logging.IAnteaterLogger; | |
17 import de.mpiwg.anteater.places.IPlaceFinder; | |
18 | |
19 public class PlacemakerPlaceFinder implements IPlaceFinder { | |
20 | |
21 public final static String COMPONENT_NAME = PlacemakerPlaceFinder.class.getSimpleName(); | |
22 | |
23 private IAnteaterLogger logger; | |
24 | |
25 public PlacemakerPlaceFinder(IAnteaterLogger logger) { | |
26 this.logger = logger; | |
27 } | |
28 | |
29 @Override | |
30 public String findPlaces(String text) { | |
31 logger.logMessage(COMPONENT_NAME, "Waiting 1 second to not be blocked by Yahoo! Placemaker..."); | |
32 try { | |
33 Thread.sleep(1000); | |
34 } catch (InterruptedException e) { | |
35 // TODO Auto-generated catch block | |
36 e.printStackTrace(); | |
37 } | |
38 logger.logMessage(COMPONENT_NAME, "Sending place extraction request to Yahoo! Placemaker..."); | |
39 | |
40 ClientConfig config = new DefaultClientConfig(); | |
41 Client client = Client.create(config); | |
42 WebResource service = client.resource(getBaseURI()); | |
43 | |
44 MultivaluedMap<String, String> values = new MultivaluedMapImpl(); | |
45 values.add("documentContent", text); | |
46 values.add("documentType", "text/plain"); | |
47 values.add("appid", | |
48 "nV7rNc_V34F5EjG5VjBaFxIxpwxemBhUa0h4N2To8CnfjVBBmLrTVOgaCsXXU7EP"); | |
49 | |
50 String result = ""; | |
51 try { | |
52 result = service.accept(MediaType.TEXT_XML).post(String.class, values); | |
53 } catch (UniformInterfaceException ex) { | |
54 logger.logMessage(COMPONENT_NAME, "Received exception from Placemaker: " + ex.getMessage()); | |
55 } | |
56 | |
57 return result; | |
58 } | |
59 | |
60 private static URI getBaseURI() { | |
61 return UriBuilder.fromUri("http://wherein.yahooapis.com/").path("v1") | |
62 .path("document").build(); | |
63 } | |
64 | |
65 } |