PayloadMetaInfoImpl.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.PayloadMetaInfo;
import csip.utils.JSONUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
/**
* Payload Metainfo access.
*
* @author od
*/
class PayloadMetaInfoImpl extends ModelDataServiceAPI implements PayloadMetaInfo {
PayloadMetaInfoImpl(ModelDataService mds) {
super(mds);
}
public String getString(String name) throws ServiceException {
try {
return mds.metainfo.getString(name);
} catch (JSONException ex) {
throw new ServiceException(ex);
}
}
public String getString(String name, String def) {
return mds.metainfo.optString(name, def);
}
public String[] getStringArray(String name) throws ServiceException {
try {
JSONArray a = mds.metainfo.getJSONArray(name);
String[] s = new String[a.length()];
for (int i = 0; i < s.length; i++) {
s[i] = a.getString(i);
}
return s;
} catch (JSONException ex) {
throw new ServiceException(ex);
}
}
public String[] getStringArray(String name, String[] def) {
try {
JSONArray a = mds.metainfo.getJSONArray(name);
String[] s = new String[a.length()];
for (int i = 0; i < s.length; i++) {
s[i] = a.getString(i);
}
return s;
} catch (JSONException ex) {
return def;
}
}
public int getInt(String name) throws ServiceException {
try {
return mds.metainfo.getInt(name);
} catch (JSONException ex) {
throw new ServiceException(ex);
}
}
public int getInt(String name, int def) throws ServiceException {
return mds.metainfo.optInt(name, def);
}
public double getDouble(String name) throws ServiceException {
try {
return mds.metainfo.getDouble(name);
} catch (JSONException ex) {
throw new ServiceException(ex);
}
}
public double getDouble(String name, double def) throws ServiceException {
return mds.metainfo.optDouble(name, def);
}
public boolean getBoolean(String name) throws ServiceException {
try {
return mds.metainfo.getBoolean(name);
} catch (JSONException ex) {
throw new ServiceException(ex);
}
}
public boolean getBoolean(String name, boolean def) {
return mds.metainfo.optBoolean(name, def);
}
public boolean hasName(String name) {
return mds.metainfo.has(name);
}
public PayloadMetaInfoImpl require(String... names) throws ServiceException {
for (String name : names) {
if (!hasName(name))
throw new ServiceException("Metainfo key not found :" + names);
}
return this;
}
public Collection<String> getNames() {
Set<String> s = new TreeSet<>();
Iterator<?> i = mds.metainfo.keys();
while (i.hasNext()) {
s.add(i.next().toString());
}
return s;
}
public int getCount() {
return mds.metainfo.length();
}
public void setWarning(String msg) throws ServiceException {
try {
mds.metainfo.put("warning", msg);
} catch (JSONException ex) {
throw new ServiceException("Warning failed.");
}
}
public void put(String key, String value) throws ServiceException {
try {
mds.metainfo.put(key, value);
} catch (JSONException ex) {
throw new ServiceException("Put metainfo entry failed for: " + key);
}
}
public void put(String key, int value) throws ServiceException {
try {
mds.metainfo.put(key, value);
} catch (JSONException ex) {
throw new ServiceException("Put metainfo entry failed for: " + key);
}
}
public void put(String key, long value) throws ServiceException {
try {
mds.metainfo.put(key, value);
} catch (JSONException ex) {
throw new ServiceException("Put metainfo entry failed for: " + key);
}
}
public void put(String key, double value) throws ServiceException {
try {
mds.metainfo.put(key, value);
} catch (JSONException ex) {
throw new ServiceException("Put metainfo entry failed for: " + key);
}
}
public void put(String key, String[] value) throws ServiceException {
try {
mds.metainfo.put(key, Arrays.asList(value));
} catch (JSONException ex) {
throw new ServiceException("Put metainfo entry failed for: " + key);
}
}
public void appendWarning(String msg) throws ServiceException {
try {
String prevmsg = JSONUtils.getJSONString(mds.metainfo, "warning", "");
prevmsg += "|" + msg;
mds.metainfo.put("warning", prevmsg);
} catch (JSONException ex) {
throw new ServiceException("Warning failed.");
}
}
public String toString() {
return mds.metainfo.toString();
}
}