USGS.java [src/java/svap/utils] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package svap.utils;

import csip.ServiceException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Rumpal Sidhu
 */
public abstract class USGS {

  protected String geometry;
  protected String geometryType = "esriGeometryPoint";
  ;
    protected String spatialReference = "4326";
  protected String spatialRelationShip = "esriSpatialRelIntersects";
  protected String outFields;
  protected String format;
  protected String whereClause;
  protected String outSpatialReference = "4326";  //Just in case we want USGS to convert between CRS during this call...setting to default to WGS-84 since all CSIP services use only WGS-84


  public void setOutFields(String outFileds) {
    this.outFields = outFileds;
  }


  public void setFormat(String format) {
    this.format = format;
  }


  public void setWhereClause(String whereClause) {
    this.whereClause = whereClause;
  }


  protected JSONObject callService(String url) throws URISyntaxException, IOException, JSONException, ServiceException {
    String responseString;
    if (null != url) {
      try (CloseableHttpClient httpClient = HttpClients.createDefault();) {
        List<NameValuePair> parameters = new ArrayList();
        parameters.add(new BasicNameValuePair("geometry", geometry));
        parameters.add(new BasicNameValuePair("geometryType", geometryType));
        parameters.add(new BasicNameValuePair("inSR", spatialReference));
        parameters.add(new BasicNameValuePair("spatialRel", spatialRelationShip));
        parameters.add(new BasicNameValuePair("outFields", outFields));
        parameters.add(new BasicNameValuePair("outSR", outSpatialReference));
        parameters.add(new BasicNameValuePair("f", format));

        if (whereClause != null) {
          parameters.add(new BasicNameValuePair("where=", whereClause));
        }

        HttpPost httpPost = new HttpPost(new URI(url));
        httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));

        try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
          StatusLine status = httpResponse.getStatusLine();
          switch (status.getStatusCode()) {
            case HttpStatus.SC_OK:
              InputStream responseStream = httpResponse.getEntity().getContent();
              responseString = IOUtils.toString(responseStream);
              break;

            case HttpStatus.SC_SERVICE_UNAVAILABLE:
              throw new ServiceException("The USGS Server reported that its USGS service is currently unavaible.  This may mean that USGS is conducting maintenace or scheduled down-time on its servers at this time. It may also mean that USGS has changed its server address. HTTP Status Code: " + HttpStatus.SC_SERVICE_UNAVAILABLE + " For URL: " + url);

            case HttpStatus.SC_NOT_FOUND:
              throw new ServiceException("The USGS Server reported that the USGS service requested was not found.  Double check the service path for the USGS service in the local configuration file.  HTTP Status Code: " + HttpStatus.SC_NOT_FOUND + " For URL: " + url);

            default:
              throw new ServiceException("The USGS Server did not return the expected response.  Expected status 200 OK, but received: " + status.getStatusCode() + ":" + status.getReasonPhrase() + " For URL: " + url);
          }
        }
      }
    } else {
      throw new ServiceException("No URL String specified in USGS call attempt.");
    }
    return new JSONObject(responseString);
  }

}