1 |
| package photospace.space; |
2 |
| |
3 |
| import javax.servlet.http.*; |
4 |
| import org.apache.commons.httpclient.*; |
5 |
| import org.apache.commons.httpclient.methods.*; |
6 |
| import org.apache.commons.lang.*; |
7 |
| import org.apache.commons.logging.*; |
8 |
| import com.hp.hpl.jena.rdf.model.*; |
9 |
| import com.hp.hpl.jena.shared.*; |
10 |
| import photospace.meta.rdf.*; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| public class Locator |
16 |
| implements java.io.Serializable |
17 |
| { |
18 |
| private static final Log log = LogFactory.getLog(Locator.class); |
19 |
| |
20 |
| private static final String BASE_URL = "http://rpc.geocoder.us/service/rest?address="; |
21 |
| |
22 |
2
| public Position locate(String address) throws Exception
|
23 |
| { |
24 |
0
| if (StringUtils.isEmpty(address)) return null;
|
25 |
| |
26 |
2
| HttpClient client = new HttpClient();
|
27 |
2
| GetMethod get = new GetMethod(BASE_URL);
|
28 |
2
| get.setQueryString(new NameValuePair[] { new NameValuePair("address", address) } );
|
29 |
2
| client.executeMethod(get);
|
30 |
| |
31 |
2
| if (HttpServletResponse.SC_OK != get.getStatusCode())
|
32 |
| { |
33 |
0
| log.warn(get.getURI() + " returned " + get.getStatusLine());
|
34 |
0
| return null;
|
35 |
| } |
36 |
| |
37 |
2
| log.debug(get.getURI() + " responded:\n" + get.getResponseBodyAsString());
|
38 |
2
| Model rdf;
|
39 |
2
| try
|
40 |
| { |
41 |
2
| rdf = ModelFactory.createDefaultModel();
|
42 |
2
| rdf.read(get.getResponseBodyAsStream(), "");
|
43 |
| } |
44 |
| catch (JenaException e) |
45 |
| { |
46 |
1
| return null;
|
47 |
| } |
48 |
| |
49 |
1
| Resource point = rdf.listSubjects().nextResource();
|
50 |
1
| Position position = new Position();
|
51 |
1
| if (point.hasProperty(GeoVocab.LAT)) position.setLatitude(new Double(point.getProperty(GeoVocab.LAT).getDouble()));
|
52 |
1
| if (point.hasProperty(GeoVocab.LONG)) position.setLongitude(new Double(point.getProperty(GeoVocab.LONG).getDouble()));
|
53 |
0
| if (point.hasProperty(GeoVocab.ALT)) position.setAltitude(new Double(point.getProperty(GeoVocab.ALT).getDouble()));
|
54 |
| |
55 |
1
| return position;
|
56 |
| } |
57 |
| } |