ServiceCall.java [src/java/d/util] 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 d.util;

import csip.api.server.ServiceException;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
abstract public class ServiceCall extends csip.utils.Client {

    protected JSONObject request;
    protected JSONObject results = null;
    protected JSONObject requestMetainfoObject = null;
    protected JSONObject resultMetaInfoObject = null;
    protected JSONArray callResultSection = null;
    protected String metaError = "";
    protected String URI;
    protected String errorPrefix="";

    public ServiceCall(String URI) {
        super();
        this.URI = URI;
    }

    public void call() throws ServiceException {
        try {
            execPost();
        } catch (ServiceException ex) {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + ex.getMessage(), ex);
        }
    }

    protected void parseResults() throws ServiceException {
        //   Error checking has already been done on these results.
        // If we get to this function, no further error
        // checking is necessary, except to look at the "results" array in the output, if desired.
        if (null == callResultSection) {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Cannot find results in the call to " + this.URI + " .");
        }
    }

    abstract protected void createRequest() throws ServiceException;

    public String getMetaError() {
        return metaError;
    }

    public JSONObject getRequest() {
        return request;
    }

    public JSONObject getResults() {
        return results;
    }

    public JSONObject getReturnMetainfo() {
        return resultMetaInfoObject;
    }

    public JSONArray getResultSection() {
        return callResultSection;
    }

    public void setRequest(String request) throws JSONException, ServiceException {
        if ((null != request) && (!request.isEmpty())) {
            this.request = new JSONObject(request);
        } else {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Attempt to set request data with empty string.");
        }
    }

    public void setRequest(JSONObject request) throws ServiceException {
        if ((null != request) && (request.length() > 0)) {
            this.request = request;
        } else {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Attempt to set request data with empty JSONObject.");
        }
    }

    public void setURI(String URI) throws ServiceException {
        if ((null != URI) && (!URI.isEmpty())) {
            this.URI = URI;
        } else {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Attempt to set URI with empty string.");
        }
    }

    private void checkErrors() throws ServiceException {
        try {

            if ((null == results) || (results.length() <= 0)) {
                throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "No data was returned from " + URI + " for this request. ");
            }

            resultMetaInfoObject = results.getJSONObject("metainfo");
            metaError = resultMetaInfoObject.optString("error", "");

            if ((null != metaError) && (!metaError.isEmpty())) {
                throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Service Call failed with error: " + metaError);
            }

            callResultSection = this.results.optJSONArray("result");

        } catch (JSONException ex) {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Missing metainfo in return data. ", ex);
        }
    }

    protected void throwServiceCallException(String message) throws ServiceException {
        throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + message);
    }

    protected void throwServiceCallException(String message, Exception ex) throws ServiceException {
        throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + message + ex.getMessage(), ex);
    }

    protected final void execPost() throws ServiceException {
        createRequest();

        try {
            results = super.doPOST(URI, request);
        } catch (Exception ex) {
            throw new ServiceException((errorPrefix.isEmpty() ? "" : (errorPrefix + ":  ")) + "Error making a connection to that location: " + URI + ".  " + ex.getMessage(), ex);
        }

        checkErrors();
        parseResults();
    }
}