use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class Util method getDistributionsOrChecklists.
/**
* Generates data for rendering of distributions table.
*
* @param type
* @param wkt
* @param lsids
* @param geomIdx
* @return
*/
public static String[] getDistributionsOrChecklists(String type, String wkt, String lsids, String geomIdx) {
try {
StringBuilder sbProcessUrl = new StringBuilder();
sbProcessUrl.append("/").append(type);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(CommonData.getLayersServer() + sbProcessUrl.toString());
LOGGER.debug(CommonData.getLayersServer() + sbProcessUrl.toString());
if (wkt != null) {
post.addParameter(StringConstants.WKT, wkt);
}
if (lsids != null) {
post.addParameter(StringConstants.LSIDS, lsids);
}
if (geomIdx != null) {
post.addParameter(StringConstants.GEOM_IDX, geomIdx);
}
post.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
int result = client.executeMethod(post);
if (result == 200) {
String txt = post.getResponseBodyAsString();
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(txt);
if (ja == null || ja.isEmpty()) {
return new String[0];
} else {
String[] lines = new String[ja.size() + 1];
lines[0] = "SPCODE,SCIENTIFIC_NAME,AUTHORITY_FULL,COMMON_NAME,FAMILY,GENUS_NAME,SPECIFIC_NAME,MIN_DEPTH,MAX_DEPTH,METADATA_URL,LSID,AREA_NAME,AREA_SQ_KM";
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
String spcode = jo.containsKey(StringConstants.SPCODE) ? jo.get(StringConstants.SPCODE).toString() : "";
String scientific = jo.containsKey(StringConstants.SCIENTIFIC) ? jo.get(StringConstants.SCIENTIFIC).toString() : "";
String auth = jo.containsKey(StringConstants.AUTHORITY) ? jo.get(StringConstants.AUTHORITY).toString() : "";
String common = jo.containsKey(StringConstants.COMMON_NAM) ? jo.get(StringConstants.COMMON_NAM).toString() : "";
String family = jo.containsKey(StringConstants.FAMILY) ? jo.get(StringConstants.FAMILY).toString() : "";
String genus = jo.containsKey(StringConstants.GENUS) ? jo.get(StringConstants.GENUS).toString() : "";
String name = jo.containsKey(StringConstants.SPECIFIC_N) ? jo.get(StringConstants.SPECIFIC_N).toString() : "";
String min = jo.containsKey(StringConstants.MIN_DEPTH) ? jo.get(StringConstants.MIN_DEPTH).toString() : "";
String max = jo.containsKey(StringConstants.MAX_DEPTH) ? jo.get(StringConstants.MAX_DEPTH).toString() : "";
String md = jo.containsKey(StringConstants.METADATA_U) ? jo.get(StringConstants.METADATA_U).toString() : "";
String lsid = jo.containsKey(StringConstants.LSID) ? jo.get(StringConstants.LSID).toString() : "";
String areaName = jo.containsKey(StringConstants.AREA_NAME) ? jo.get(StringConstants.AREA_NAME).toString() : "";
String areaKm = jo.containsKey(StringConstants.AREA_KM) ? jo.get(StringConstants.AREA_KM).toString() : "";
String dataResourceUid = jo.containsKey(StringConstants.DATA_RESOURCE_UID) ? jo.get(StringConstants.DATA_RESOURCE_UID).toString() : "";
lines[i + 1] = spcode + "," + wrap(scientific) + "," + wrap(auth) + "," + wrap(common) + "," + wrap(family) + "," + wrap(genus) + "," + wrap(name) + "," + min + "," + max + "," + wrap(md) + "," + wrap(lsid) + "," + wrap(areaName) + "," + wrap(areaKm) + "," + wrap(dataResourceUid);
}
return lines;
}
}
} catch (Exception e) {
LOGGER.error("error building distribution or checklist csv", e);
}
return new String[0];
}
use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class EnvLayersCombobox method refresh.
public void refresh(String val) {
//don't do autocomplete when < 1 characters
if (val.length() < 0) {
return;
}
String baseUrl = CommonData.getLayersServer() + "/fields/";
try {
Iterator it = getItems().iterator();
JSONArray results;
String lsurl = baseUrl;
if (val.length() == 0) {
results = CommonData.getLayerListJSONArray();
} else {
lsurl += "search/?q=" + URLEncoder.encode(val, StringConstants.UTF_8);
LOGGER.debug("nsurl: " + lsurl);
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(lsurl);
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
client.executeMethod(get);
String slist = get.getResponseBodyAsString();
JSONParser jp = new JSONParser();
results = (JSONArray) jp.parse(slist);
}
LOGGER.debug("got " + results.size() + " layers");
Sessions.getCurrent().setAttribute("layerlist", results);
if (!results.isEmpty()) {
for (int i = 0; i < results.size(); i++) {
JSONObject field = (JSONObject) results.get(i);
JSONObject layer = (JSONObject) field.get("layer");
if (!field.get(StringConstants.ENABLED).toString().equalsIgnoreCase("true") || !field.containsKey(StringConstants.NAME) || !layer.containsKey(StringConstants.TYPE) || !field.get(StringConstants.ADD_TO_MAP).toString().equalsIgnoreCase("true") || (!StringConstants.ENVIRONMENTAL.equalsIgnoreCase(layer.get(StringConstants.TYPE).toString()) && (includeLayers != null && !"AllLayers".equalsIgnoreCase(includeLayers) && !"MixLayers".equalsIgnoreCase(includeLayers)))) {
continue;
}
String displayName = field.get(StringConstants.NAME).toString();
String type = layer.get(StringConstants.TYPE).toString();
String name = field.get(StringConstants.ID).toString();
Comboitem myci;
if (it != null && it.hasNext()) {
myci = ((Comboitem) it.next());
myci.setLabel(displayName);
} else {
it = null;
myci = new Comboitem(displayName);
myci.setParent(this);
}
String c2 = "";
if (layer.containsKey(StringConstants.CLASSIFICATION2) && !StringConstants.NULL.equals(layer.get(StringConstants.CLASSIFICATION2))) {
c2 = layer.get(StringConstants.CLASSIFICATION2) + ": ";
}
String c1 = "";
if (layer.containsKey(StringConstants.CLASSIFICATION1) && !StringConstants.NULL.equals(layer.get(StringConstants.CLASSIFICATION1))) {
c1 = layer.get(StringConstants.CLASSIFICATION1) + ": ";
}
myci.setDescription(c1 + c2 + type);
myci.setValue(field);
}
}
if (includeAnalysisLayers) {
for (MapLayer ml : getMapComposer().getAnalysisLayers()) {
String displayName = ml.getDisplayName();
String type;
String name;
String classification1;
String classification2;
if (ml.getSubType() == LayerUtilitiesImpl.MAXENT) {
type = StringConstants.ENVIRONMENTAL;
classification1 = StringConstants.ANALYSIS;
classification2 = StringConstants.PREDICTION;
name = ml.getName();
} else if (ml.getSubType() == LayerUtilitiesImpl.GDM) {
type = StringConstants.ENVIRONMENTAL;
classification1 = StringConstants.ANALYSIS;
classification2 = StringConstants.GDM;
name = ml.getName();
} else if (ml.getSubType() == LayerUtilitiesImpl.ODENSITY) {
type = StringConstants.ENVIRONMENTAL;
classification1 = StringConstants.ANALYSIS;
classification2 = StringConstants.OCCURRENCE_DENSITY;
name = ml.getName();
} else if (ml.getSubType() == LayerUtilitiesImpl.SRICHNESS) {
type = StringConstants.ENVIRONMENTAL;
classification1 = StringConstants.ANALYSIS;
classification2 = StringConstants.SPECIES_RICHNESS;
name = ml.getName();
} else {
continue;
}
Comboitem myci;
if (it != null && it.hasNext()) {
myci = ((Comboitem) it.next());
myci.setLabel(displayName);
} else {
it = null;
myci = new Comboitem(displayName);
myci.setParent(this);
}
myci.setDescription(classification1 + ": " + classification2 + " " + type);
myci.setDisabled(false);
JSONObject jo = new JSONObject();
jo.put(StringConstants.NAME, name);
myci.setValue(jo);
}
}
while (it != null && it.hasNext()) {
it.next();
it.remove();
}
} catch (Exception e) {
LOGGER.error("Error searching for layers:", e);
}
}
use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class GazetteerAutoComplete method refresh.
/**
* Refresh comboitem based on the specified value.
*/
private void refresh(String val) {
String searchString = val.trim().replaceAll("\\s+", "+");
searchString = (searchString.isEmpty()) ? "a" : searchString;
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(CommonData.getLayersServer() + "/search?limit=40&q=" + searchString + "&userObjects=false");
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
client.executeMethod(get);
String slist = get.getResponseBodyAsString();
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(slist);
if (ja == null) {
return;
}
Iterator it = getItems().iterator();
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
String itemString = jo.get(StringConstants.NAME).toString();
String description = (jo.containsKey(StringConstants.DESCRIPTION) ? jo.get(StringConstants.DESCRIPTION).toString() : "") + " (" + jo.get("fieldname") + ")";
if (it != null && it.hasNext()) {
Comboitem ci = (Comboitem) it.next();
ci.setLabel(itemString);
ci.setValue(jo);
ci.setDescription(description);
} else {
it = null;
Comboitem ci = new Comboitem();
ci.setLabel(itemString);
ci.setValue(jo);
ci.setDescription(description);
ci.setParent(this);
}
}
while (it != null && it.hasNext()) {
it.next();
it.remove();
}
} catch (Exception e) {
LOGGER.error("error selecting gaz autocomplete item", e);
}
}
use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class GazetteerPointSearch method pointSearch.
/**
* *
* Given a lon,lat and layer - queries the gaz for a polygon
*
* @param lon longitude
* @param lat latitude
* @param layer geoserver layer to search
* @return returns a link to a geojson feature in the gaz
*/
public static Map<String, String> pointSearch(String lon, String lat, String layer, String geoserver) {
try {
String uri = CommonData.getLayersServer() + "/intersect/" + layer + "/" + lat + "/" + lon;
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(uri);
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
int result = client.executeMethod(get);
String slist = get.getResponseBodyAsString();
LOGGER.debug("URI: " + uri);
LOGGER.debug("result: " + result);
LOGGER.debug("slist: " + slist);
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(slist);
if (ja != null && !ja.isEmpty()) {
JSONObject jo = (JSONObject) ja.get(0);
Map<String, String> map = new HashMap<String, String>();
for (Object k : jo.keySet()) {
map.put((String) k, jo.get((String) k) == null ? "" : jo.get((String) k).toString());
}
return map;
}
} catch (Exception e1) {
LOGGER.error("error with gaz point search", e1);
}
return null;
}
use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.
the class ImportAnalysisController method getParametersGdm.
boolean getParametersGdm() {
try {
//TODO: analysis output url into config
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(CommonData.getSatServer().replace("/alaspatial", "") + "/output/gdm/" + pid + "/ala.properties");
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
int result = client.executeMethod(get);
if (result == 200) {
String slist = get.getResponseBodyAsString();
for (String row : slist.split("\n")) {
if (row.startsWith("envlist")) {
gdmEnvlist = row.replace("envlist=", "").split("\\\\:");
}
}
return true;
}
} catch (Exception e) {
LOGGER.error("Error getting gdm parameters pid=" + pid, e);
}
return false;
}
Aggregations