PayloadParameterImpl.java [src/csip] 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-2022, Olaf David and others, 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 csip;
import csip.api.server.ServiceException;
import csip.api.server.PayloadParameter;
import static csip.ModelDataService.GEOMETRY;
import static csip.ModelDataService.KEY_DESC;
import static csip.ModelDataService.UNIT;
import static csip.ModelDataService.VALUE;
import csip.utils.JSONUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* Payload Parameter access.
*
* @author od
*/
class PayloadParameterImpl implements PayloadParameter {
private Map<String, JSONObject> paramMap;
PayloadParameterImpl(Map<String, JSONObject> paramMap) {
this.paramMap = paramMap;
}
private Map<String, JSONObject> getParamMap() {
return paramMap;
}
private JSONObject get(String name) throws ServiceException {
JSONObject p = getParamMap().get(name);
if (p == null)
throw new ServiceException("Parameter not found: '" + name + "'");
return p;
}
public boolean has(String name) {
return getParamMap().containsKey(name);
}
public PayloadParameterImpl require(String... names) throws ServiceException {
for (String name : names) {
if (!has(name))
throw new ServiceException("Required parameter not found: " + name);
}
return this;
}
private static void checkRange(String name, double min, double max, double val) throws ServiceException {
if (val < min || val > max)
throw new ServiceException(name + " is: " + val + ", must be in range [" + min + ", " + max + "]");
}
public int getCount() {
return getParamMap().size();
}
public Collection<String> getNames() {
return getParamMap().keySet();
}
public PayloadParameterImpl getParams(String name) throws ServiceException {
try {
Object v = get(name).get(VALUE);
return new PayloadParameterImpl(JSONUtils.preprocess(v));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public int[] getParamsSize(String name) throws Exception {
JSONArray v = get(name).getJSONArray(VALUE);
List<Integer> l = new ArrayList<>();
while (true) {
try {
l.add(v.length());
v = v.getJSONArray(0); // assumes regular array.
} catch (JSONException ex) {
break;
}
}
return l.stream().mapToInt(i -> i).toArray();
}
public PayloadParameterImpl getParams(String name, int... i) throws ServiceException {
try {
JSONArray v = get(name).getJSONArray(VALUE);
for (int j = 0; j < i.length; j++) {
v = v.getJSONArray(i[j]);
}
return new PayloadParameterImpl(JSONUtils.preprocess(v));
} catch (JSONException ex) {
throw new ServiceException("Error accessing " + name + " index: " + Arrays.toString(i), ex);
}
}
public JSONObject getParamJSON(String name) throws ServiceException {
return get(name);
}
public String getString(String name) throws ServiceException {
try {
return get(name).getString(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String getString(String name, String def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getString(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public int getInt(String name) throws ServiceException {
try {
return get(name).getInt(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public int getInt(String name, int def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getInt(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public double getDouble(String name) throws ServiceException {
try {
return get(name).getDouble(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public double getDouble(String name, double min, double max) throws ServiceException {
double val = getDouble(name);
checkRange(name, min, max, val);
return val;
}
public double getDouble(String name, double def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getDouble(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public double getDouble(String name, double def, double min, double max) throws ServiceException {
double val = getDouble(name, def);
checkRange(name, min, max, val);
return val;
}
public boolean getBoolean(String name) throws ServiceException {
try {
return get(name).getBoolean(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public boolean getBoolean(String name, boolean def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getBoolean(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public long getLong(String name) throws ServiceException {
try {
return get(name).getLong(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public long getLong(String name, long def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getLong(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public JSONObject getJSON(String name) throws ServiceException {
try {
return get(name).getJSONObject(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public JSONObject getJSON(String name, JSONObject def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getJSONObject(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public JSONArray getJSONArray(String name) throws ServiceException {
try {
return get(name).getJSONArray(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public JSONArray getJSONArray(String name, JSONArray def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.getJSONArray(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public int[] getIntArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.toIntArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public int[] getIntArray(String name, int[] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.toIntArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public boolean[] getBooleanArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.toBooleanArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public boolean[] getBooleanArray(String name, boolean[] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.toBooleanArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public long[] getLongArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.toLongArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public long[] getLongArray(String name, long[] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.toLongArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String[] getStringArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.toStringArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String[] getStringArray(String name, String[] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.toStringArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String[][] get2DStringArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.to2DStringArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String[][] get2DStringArray(String name, String[][] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.to2DStringArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public double[] getDoubleArray(String name) throws ServiceException {
try {
JSONArray a = get(name).getJSONArray(VALUE);
return JSONUtils.toDoubleArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public double[] getDoubleArray(String name, double[] def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : JSONUtils.toDoubleArray(p.getJSONArray(VALUE));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
public String getUnit(String name) throws ServiceException {
try {
return get(name).getString(UNIT);
} catch (JSONException ex) {
throw new ServiceException("No unit for " + name);
}
}
public String getDescr(String name) throws ServiceException {
try {
return get(name).getString(KEY_DESC);
} catch (JSONException ex) {
throw new ServiceException("No description for " + name);
}
}
public JSONObject getGeometry(String name) throws ServiceException {
try {
return get(name).getJSONObject(GEOMETRY);
} catch (JSONException ex) {
throw new ServiceException("No geometry for " + name);
}
}
public String getMetaInfo(String name, String metaKey) throws ServiceException {
JSONObject p = get(name);
try {
if (p.has(metaKey))
return p.getString(metaKey);
} catch (JSONException e) {
throw new ServiceException("error getting meta value '" + metaKey + "' for parameter: " + name);
}
return null;
}
public JSONObject getParam(String name) throws ServiceException {
return get(name);
}
public JSONObject getParam(String name, JSONObject def) throws ServiceException {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p;
}
}