use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class BlobStorage method autocreateJSON.
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
ReturnedURL url = null;
try {
byte[] bitten = (byte[]) jsonObject.get("getbyteBody");
String uploadname = jsonObject.getString("fileName");
String type = jsonObject.getString("contentType");
String path = r.getServicesURL();
url = conn.getStringURL(RequestMethod.POST, path, bitten, uploadname, type, creds, cache);
} catch (ConnectionException e) {
throw new UnderlyingStorageException(e.getMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnimplementedException("JSONException", e);
}
return conn.getBase() + url.getURL();
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method subautocreateJSON.
/**
* needs some tests.. just copied from ConfiguredVocabStorage
* @param root
* @param creds
* @param cache
* @param myr
* @param jsonObject
* @param savePrefix
* @return
* @throws ExistException
* @throws UnimplementedException
* @throws UnderlyingStorageException
*/
public String subautocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, Record myr, JSONObject jsonObject, String savePrefix) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
ReturnedURL url = null;
Document doc = null;
// XXX I would hope this might be removed if userroles etc ever get improved to be more like the rest
if (myr.hasPrimaryField()) {
for (String section : myr.getServicesRecordPathKeys()) {
doc = XmlJsonConversion.convertToXml(myr, jsonObject, section, "POST");
String path = myr.getServicesURL();
path = path.replace("*", getSubCsid(jsonObject, myr.getPrimaryField()));
deleteJSON(root, creds, cache, path);
url = conn.getURL(RequestMethod.POST, path, doc, creds, cache);
}
} else {
url = autoCreateSub(creds, cache, jsonObject, doc, savePrefix, myr);
}
// create related sub records?
for (FieldSet allfs : myr.getAllSubRecords("POST")) {
Record sr = allfs.usesRecordId();
if (sr.isType("authority")) {
} else {
String savePath = url.getURL() + "/" + sr.getServicesURL();
if (jsonObject.has(sr.getID())) {
Object subdata = jsonObject.get(sr.getID());
if (subdata instanceof JSONArray) {
JSONArray subarray = (JSONArray) subdata;
for (int i = 0; i < subarray.length(); i++) {
JSONObject subrecord = subarray.getJSONObject(i);
subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
}
} else if (subdata instanceof JSONObject) {
JSONObject subrecord = (JSONObject) subdata;
subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
}
}
}
}
return url.getURLTail();
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Cannot parse surrounding JSON" + e.getLocalizedMessage(), e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class ServicesIDGenerator method retrieveJSON.
/**
* This function generates a new id for the next inputted object.
* The id in the static list on top is the primary key in the generators_id table on the service layer.
*/
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
String val = conn.getTextDocument(RequestMethod.POST, "idgenerators/" + generators.get(filePath) + "/ids", null, creds, cache);
JSONObject out = new JSONObject();
out.put("next", val);
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("JSON exception" + e.getLocalizedMessage(), e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class AuthorizationStorage method refViewRetrieveJSON.
@Override
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
try {
JSONObject out = new JSONObject();
// not all the records need a reference, look in cspace-config.xml for which that don't
if (r.hasTermsUsed()) {
String path = r.getServicesURL() + "/" + filePath + "/authorityrefs";
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (all.getStatus() != 200)
throw new ConnectionException("Bad request problem in AuthorizationStorage/refViewRetrieve: status not 200", all.getStatus(), path);
Document list = all.getDocument();
for (Object node : list.selectNodes("authority-ref-list/authority-ref-item")) {
if (!(node instanceof Element))
continue;
String key = ((Element) node).selectSingleNode("sourceField").getText();
String uri = ((Element) node).selectSingleNode("uri").getText();
String refname = ((Element) node).selectSingleNode("refName").getText();
if (uri != null && uri.startsWith("/"))
uri = uri.substring(1);
JSONObject data = miniForURI(storage, creds, cache, refname, uri, restrictions);
out.put(key, data);
}
}
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection problem in AuthorizationStorage:" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class AuthorizationStorage method updateJSON.
@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
Map<String, Document> parts = new HashMap<String, Document>();
Document doc = null;
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "PUT");
parts.put(record_path[0], doc);
}
int status = 0;
if (r.isMultipart()) {
ReturnedMultipartDocument docm = conn.getMultipartXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, parts, creds, cache);
status = docm.getStatus();
} else {
ReturnedDocument docm = conn.getXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, doc, creds, cache);
status = docm.getStatus();
}
if (status == 404)
throw new ExistException("Not found: " + r.getServicesURL() + "/" + filePath);
if (status > 299 || status < 200)
throw new UnderlyingStorageException("Bad response " + status, status, r.getServicesURL() + "/" + filePath);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception", e);
} catch (JSONException e) {
throw new UnimplementedException("JSONException", e);
}
}
Aggregations