Skip to content

Commit

Permalink
NPE and types fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Dutoit committed Oct 12, 2017
1 parent e4759bd commit 189b133
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
32 changes: 25 additions & 7 deletions src/main/java/ch/mno/copper/collect/WebCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import ch.mno.copper.collect.connectors.JmxConnector;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import net.minidev.json.JSONArray;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -86,14 +88,21 @@ else if ("contentType".equals(key)) {
results.add(data.getData());
} else {
try {
net.minidev.json.JSONArray res = JsonPath.read(data.getData(), key);
if (res == null || res.size() == 0) {
LOG.info("Warning: jsonpath " + key + " not found in " + data);
results.add("ERR_NOT_FOUND");
} else if (res.size() > 1) {
results.add("TOO_MUCH_VALUES_FOUND");
Object o = JsonPath.read(data.getData(), key);
if (o instanceof JSONArray) {
net.minidev.json.JSONArray res = (JSONArray) o;
if (res == null || res.size() == 0) {
LOG.info("Warning: jsonpath " + key + " not found in " + data);
results.add("ERR_NOT_FOUND");
} else if (res.size() > 1) {
results.add("TOO_MUCH_VALUES_FOUND");
} else {
results.add(res.get(0).toString());
}
} else if (o instanceof String) {
results.add((String)o);
} else {
results.add(res.get(0).toString());
results.add(o.toString());
}
} catch (PathNotFoundException e) {
LOG.error("Path not found: " + key);
Expand All @@ -103,4 +112,13 @@ else if ("contentType".equals(key)) {
}
return results;
}

public static void main(String[] args) {
List<Pair<String, String>> values = new ArrayList<>();
values.add(new ImmutablePair<>("responseCode", "rs"));
values.add(new ImmutablePair<>("status", "s"));
WebCollector.query("http://tom.etat-de-vaud.ch:1530/ws/infra/status", null, null, values);
}

}

14 changes: 12 additions & 2 deletions src/main/java/ch/mno/copper/collect/connectors/HttpConnector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.mno.copper.collect.connectors;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand Down Expand Up @@ -88,8 +89,17 @@ public HttpResponseData<String> get2(String uri) throws ConnectorException {
data.setData(EntityUtils.toString(response.getEntity()).trim());
}
data.setResponseCode(response.getStatusLine().getStatusCode());
data.setContentLength(response.getLastHeader("Content-Length").getValue());
data.setContentType(response.getLastHeader("Content-Type").getValue());

Header lastHeader = response.getLastHeader("Content-Length");
if (lastHeader!=null) {
data.setContentLength(lastHeader.getValue());
}


Header lastHeader1 = response.getLastHeader("Content-Type");
if (lastHeader1!=null) {
data.setContentType(lastHeader1.getValue());
}

return data;
} catch (IOException e) {
Expand Down

0 comments on commit 189b133

Please sign in to comment.