PayloadParameterImpl.java [src/csip] Revision: 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.ModelDataServiceConstants.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 extends ModelDataServiceAPI implements PayloadParameter {
private Map<String, JSONObject> paramMap;
PayloadParameterImpl(ModelDataService mds, Map<String, JSONObject> paramMap) {
super(mds);
this.paramMap = paramMap;
}
private Map<String, JSONObject> getParamMap() {
return paramMap;
}
private JSONObject get0(String name) throws ServiceException {
JSONObject p = getParamMap().get(name);
if (p == null)
throw new ServiceException("Parameter not found: '" + name + "'");
return p;
}
@Override
public boolean has(String name) {
return getParamMap().containsKey(name);
}
@Override
public PayloadParameterImpl require(String... names) throws ServiceException {
for (String name : names) {
if (!has(name))
throw new ServiceException("Required parameter not found: " + name);
}
return this;
}
@Override
public int getCount() {
return getParamMap().size();
}
@Override
public Collection<String> getNames() {
return getParamMap().keySet();
}
@Override
public PayloadParameterImpl getParams(String name) throws ServiceException {
try {
Object v = get0(name).get(VALUE);
return new PayloadParameterImpl(mds, JSONUtils.preprocess(v));
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
public int[] getParamsSize(String name) throws Exception {
JSONArray v = get0(name).getJSONArray(VALUE);
List<Integer> l = new ArrayList<>();
boolean br = true;
while (br) {
try {
l.add(v.length());
v = v.getJSONArray(0); // assumes regular array.
} catch (JSONException ex) {
br = false;
}
}
return l.stream().mapToInt(i -> i).toArray();
}
@Override
public PayloadParameterImpl getParams(String name, int... i) throws ServiceException {
try {
JSONArray v = get0(name).getJSONArray(VALUE);
for (int j = 0; j < i.length; j++) {
v = v.getJSONArray(i[j]);
}
return new PayloadParameterImpl(mds, JSONUtils.preprocess(v));
} catch (JSONException ex) {
throw new ServiceException("Error accessing " + name + " index: " + Arrays.toString(i), ex);
}
}
@Override
public Object get(String name) throws ServiceException {
try {
return get0(name).get(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
public Object get(String name, Object def) throws ServiceException {
try {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p.get(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
public JSONObject getParamJSON(String name) throws ServiceException {
return get0(name);
}
@Override
public String getString(String name) throws ServiceException {
try {
return get0(name).getString(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
private String getStringWithCheck(String name, Check c) throws ServiceException {
try {
String i = get0(name).getString(VALUE);
c.forString(i, name + ": ");
return i;
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
private String getString0(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);
}
}
@Override
public String getString(String name, Object defOrCheck) throws ServiceException {
if (defOrCheck == null) {
return getString0(name, (String) null);
} else if (defOrCheck instanceof Check) {
return getStringWithCheck(name, (Check) defOrCheck);
} else {
return getString0(name, defOrCheck.toString());
}
}
@Override
public int getInt(String name) throws ServiceException {
try {
return get0(name).getInt(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
public int getInt(String name, Check v) throws ServiceException {
try {
int i = get0(name).getInt(VALUE);
v.forNumber(i, name + ": ");
return i;
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public double getDouble(String name) throws ServiceException {
try {
return get0(name).getDouble(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
public double getDouble(String name, Check v) throws ServiceException {
try {
double d = get0(name).getDouble(VALUE);
v.forNumber(d, name + ": ");
return d;
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public boolean getBoolean(String name) throws ServiceException {
try {
return get0(name).getBoolean(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public long getLong(String name) throws ServiceException {
try {
return get0(name).getLong(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public JSONObject getJSON(String name) throws ServiceException {
try {
return get0(name).getJSONObject(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public JSONArray getJSONArray(String name) throws ServiceException {
try {
return get0(name).getJSONArray(VALUE);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public int[] getIntArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.toIntArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public boolean[] getBooleanArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.toBooleanArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public long[] getLongArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.toLongArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public String[] getStringArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.toStringArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public String[][] get2DStringArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.to2DStringArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public double[] getDoubleArray(String name) throws ServiceException {
try {
JSONArray a = get0(name).getJSONArray(VALUE);
return JSONUtils.toDoubleArray(a);
} catch (JSONException ex) {
throw new ServiceException("No Value for " + name, ex);
}
}
@Override
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);
}
}
@Override
public String getUnit(String name) throws ServiceException {
try {
return get0(name).getString(UNIT);
} catch (JSONException ex) {
throw new ServiceException("No unit for " + name);
}
}
@Override
public String getDescr(String name) throws ServiceException {
try {
return get0(name).getString(KEY_DESC);
} catch (JSONException ex) {
throw new ServiceException("No description for " + name);
}
}
@Override
public JSONObject getGeometry(String name) throws ServiceException {
try {
return get0(name).getJSONObject(GEOMETRY);
} catch (JSONException ex) {
throw new ServiceException("No geometry for " + name);
}
}
@Override
public String getMetaInfo(String name, String metaKey) throws ServiceException {
JSONObject p = get0(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;
}
@Override
public JSONObject getParam(String name) throws ServiceException {
return get0(name);
}
@Override
public JSONObject getParam(String name, JSONObject def) throws ServiceException {
JSONObject p = getParamMap().get(name);
return (p == null) ? def : p;
}
}