Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Comment on lines +101 to +102
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
*/
package org.apache.tika.parser.geo.topic.gazetteer;

import java.lang.reflect.Type;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -42,6 +43,10 @@ public class GeoGazetteerClient {

private static final Logger LOG = LoggerFactory.getLogger(GeoGazetteerClient.class);

// gson ignored fields Location doesn't have
private static final ObjectMapper MAPPER =
new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

private String url;

/**
Expand Down Expand Up @@ -74,11 +79,7 @@ public Map<String, List<Location>> getLocations(List<String> locations) {
HttpResponse resp = httpClient.execute(httpGet);
String respJson = IOUtils.toString(resp.getEntity().getContent(), StandardCharsets.UTF_8);

@SuppressWarnings("serial") Type typeDef =
new TypeToken<Map<String, List<Location>>>() {
}.getType();

return new Gson().fromJson(respJson, typeDef);
return parseLocations(respJson);

} catch (Exception e) {
LOG.error(e.getMessage(), e);
Expand All @@ -87,6 +88,11 @@ public Map<String, List<Location>> getLocations(List<String> locations) {
return null;
}

static Map<String, List<Location>> parseLocations(String json) throws IOException {
return MAPPER.readValue(json, new TypeReference<Map<String, List<Location>>>() {
});
}

/**
* Ping lucene-geo-gazetteer API
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tika.parser.geo.topic.gazetteer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class GeoGazetteerClientTest {

@Test
public void testParseLocations() throws Exception {
// lucene-geo-gazetteer sends coordinates as numbers, and fields Location doesn't have
String json = "{\"Pasadena\":[{\"name\":\"Pasadena\",\"countryCode\":\"US\",\"admin1Code\":\"CA\","
+ "\"admin2Code\":\"037\",\"latitude\":34.14778,\"longitude\":-118.14452,\"population\":141029}],"
+ "\"Nowhere\":[]}";
Map<String, List<Location>> locations = GeoGazetteerClient.parseLocations(json);
assertEquals(2, locations.size());
assertTrue(locations.get("Nowhere").isEmpty());
Location loc = locations.get("Pasadena").get(0);
assertEquals("Pasadena", loc.getName());
assertEquals("US", loc.getCountryCode());
assertEquals("CA", loc.getAdmin1Code());
assertEquals("037", loc.getAdmin2Code());
assertEquals("34.14778", loc.getLatitude());
assertEquals("-118.14452", loc.getLongitude());
}

@Test
public void testNullLocations() throws Exception {
assertNull(GeoGazetteerClient.parseLocations("null"));
}
}
Loading