Skip to content

Commit

Permalink
'#1487 Corrects error treatment to use the correct initialization
Browse files Browse the repository at this point in the history
exception, and also to inform error only for georeferenced file.
  • Loading branch information
patrickdalla committed Sep 21, 2023
1 parent d36fc22 commit 0f1045f
Showing 1 changed file with 55 additions and 43 deletions.
98 changes: 55 additions & 43 deletions iped-geo/src/main/java/iped/geo/nominatim/NominatimTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ public class NominatimTask extends AbstractTask {
static String baseUrl = "";

static boolean unresolvedServer = true;
static private Exception unresolvedException = new Exception("Task not initialized");

private NominatimConfig nominatimConfig;

private Exception unresolvedException;

@Override
public List<Configurable<?>> getConfigurables() {
return Arrays.asList(new NominatimConfig());
Expand All @@ -81,50 +80,55 @@ public List<Configurable<?>> getConfigurables() {

@Override
public void init(ConfigurationManager configurationManager) throws Exception {
count.incrementAndGet();
if (cm == null) {
nominatimConfig = (NominatimConfig) configurationManager.findObject(NominatimConfig.class);

cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAXTOTAL);
cm.setDefaultMaxPerRoute(nominatimConfig.getConnectionPoolSize());
myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
try {
count.incrementAndGet();
if (cm == null) {
nominatimConfig = (NominatimConfig) configurationManager.findObject(NominatimConfig.class);

cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAXTOTAL);
cm.setDefaultMaxPerRoute(nominatimConfig.getConnectionPoolSize());
myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return CUSTOM_KEEP_ALIVE;
}
return CUSTOM_KEEP_ALIVE;
}
};

try {
httpClient = HttpClients.custom().setKeepAliveStrategy(myStrategy)
.setConnectionManager(cm).build();
String defaultTestCountry = "brazil";
baseUrl = nominatimConfig.getProtocol() + "://" + nominatimConfig.getHostName() + ":"
+ nominatimConfig.getHostPort();
HttpGet get = new HttpGet(baseUrl + nominatimConfig.getServiceTestUrlQuery());
try (CloseableHttpResponse response = httpClient.execute(get)) {
unresolvedServer = false;
} catch (ClientProtocolException cpe) {
};

try {
httpClient = HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(cm).build();
String defaultTestCountry = "brazil";
baseUrl = nominatimConfig.getProtocol() + "://" + nominatimConfig.getHostName() + ":"
+ nominatimConfig.getHostPort();
HttpGet get = new HttpGet(baseUrl + nominatimConfig.getServiceTestUrlQuery());
try (CloseableHttpResponse response = httpClient.execute(get)) {
unresolvedServer = false;
} catch (ClientProtocolException cpe) {
unresolvedException = cpe;
unresolvedServer = true;
}
} catch (Exception e) {
unresolvedException = e;
unresolvedServer = true;
}
} catch (Exception e) {
unresolvedException = e;
unresolvedServer = true;
}

/* thread to monitor connections closed by the host */
staleMonitor = new IdleConnectionMonitorThread(cm);
staleMonitor.start();
/* thread to monitor connections closed by the host */
staleMonitor = new IdleConnectionMonitorThread(cm);
staleMonitor.start();
}
} catch (Exception e) {
unresolvedException = e;
unresolvedServer = true;
}
}

Expand Down Expand Up @@ -201,8 +205,16 @@ protected void process(IItem evidence) throws Exception {
}

} else {
// add the error message to nominatim metadata
evidence.getMetadata().add(NOMINATIM_METADATA, getErrorJson(unresolvedException));
// check to see if there would be a location to resolve
String featureString = evidence.getMetadata().get(GeofileParser.FEATURE_STRING);

if (featureString == null || featureString.length() < 5) {
String location = evidence.getMetadata().get(ExtraProperties.LOCATIONS);
if (location != null && location.length() >= 1) {
// add the error message to nominatim metadata
evidence.getMetadata().add(NOMINATIM_METADATA, getErrorJson(unresolvedException));
}
}
}
}

Expand Down

0 comments on commit 0f1045f

Please sign in to comment.