use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class GenericStorage method autocreateJSON.
/**
* Convert the JSON from the UI Layer into XML for the Service layer while using the XML structure from cspace-config.xml
* Send the XML through to the Service Layer to store it in the database
* The Service Layer returns a url to the object we just stored.
* @param {ContextualisedStorage} root
* @param {CSPRequestCredentials} creds
* @param {CSPRequestCache} cache
* @param {String} filePath part of the path to the Service URL (containing the type of object)
* @param {JSONObject} jsonObject The JSON string coming in from the UI Layer, containing the object to be stored
* @return {String} csid The id of the object in the database
*/
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
ReturnedURL url = null;
Document doc = null;
// used by userroles and permroles as they have complex urls
if (r.hasPrimaryField()) {
for (String section : r.getServicesRecordPathKeys()) {
doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
String path = r.getServicesURL();
path = path.replace("*", getSubCsid(jsonObject, r.getPrimaryField()));
String restrictedPath = getRestrictedPath(path, restrictions, null);
deleteJSON(root, creds, cache, restrictedPath);
url = conn.getURL(RequestMethod.POST, restrictedPath, doc, creds, cache);
}
} else {
String restrictedPath = getRestrictedPath(r.getServicesURL(), restrictions, null);
// REM - We need a way to send query params (restrictions) on POST and UPDATE
url = autoCreateSub(creds, cache, jsonObject, doc, restrictedPath, r);
}
// I am developing this.. it might not work...
for (FieldSet fs : r.getAllSubRecords("POST")) {
Record sr = fs.usesRecordId();
// sr.getID()
if (sr.isType("authority")) {
// need to use code from configuredVocabStorage
} else {
String savePath = url.getURL() + "/" + sr.getServicesURL();
if (fs instanceof Field) {
// get the fields form inline XXX untested - might not work...
JSONObject subdata = new JSONObject();
// loop thr jsonObject and find the fields I need
for (FieldSet subfs : sr.getAllFieldTopLevel("POST")) {
String key = subfs.getID();
if (jsonObject.has(key)) {
subdata.put(key, jsonObject.get(key));
}
}
subautocreateJSON(root, creds, cache, sr, subdata, savePath);
} else if (fs instanceof Group) {
// JSONObject
if (jsonObject.has(fs.getID())) {
Object subdata = jsonObject.get(fs.getID());
if (subdata instanceof JSONObject) {
JSONObject subrecord = (JSONObject) subdata;
subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
}
}
} else {
// JSONArray
if (jsonObject.has(fs.getID())) {
Object subdata = jsonObject.get(fs.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);
}
}
}
}
}
}
return url.getURLTail();
} catch (ConnectionException e) {
String msg = e.getMessage();
if (e.getStatus() == 403) {
// permissions error
msg += " permissions error";
}
throw new UnderlyingStorageException(msg, e.getStatus(), e.getUrl(), e);
} catch (UnderlyingStorageException e) {
// REM - CSPACE-5632: Need to catch and rethrow this exception type to prevent throwing an "UnimplementedException" exception below.
throw e;
} catch (Exception e) {
throw new UnimplementedException("JSONException", e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class GenericStorage method autoCreateSub.
/**
* create sub records so can be connected to their parent record
* e.g. blob/media contact/person
* @param creds
* @param cache
* @param jsonObject
* @param doc
* @param savePrefix
* @param r
* @return
* @throws JSONException
* @throws UnderlyingStorageException
* @throws ConnectionException
* @throws ExistException
*/
protected ReturnedURL autoCreateSub(CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, Document doc, String savePrefix, Record r) throws JSONException, UnderlyingStorageException, ConnectionException, ExistException {
ReturnedURL url;
Map<String, Document> parts = new HashMap<String, Document>();
Document doc2 = doc;
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc2 = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
if (doc2 != null) {
doc = doc2;
parts.put(record_path[0], doc2);
// log.info(doc.asXML());
// log.info(savePrefix);
}
}
// This checks for hierarchy support, and does nothing if not appropriate.
handleHierarchyPayloadSend(r, parts, jsonObject, null);
// some records are accepted as multipart in the service layers, others arent, that's why we split up here
if (r.isMultipart())
url = conn.getMultipartURL(RequestMethod.POST, savePrefix, parts, creds, cache);
else
url = conn.getURL(RequestMethod.POST, savePrefix, doc, creds, cache);
if (url.getStatus() > 299 || url.getStatus() < 200)
throw new UnderlyingStorageException("Bad response ", url.getStatus(), savePrefix);
return url;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL 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.ReturnedURL 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.ReturnedURL in project application by collectionspace.
the class TestDummyData method create.
private String create(String serviceurl, String partname, String Createfilename, String mungeurl) throws Exception {
ReturnedURL url;
log.info("Testing " + serviceurl + " with " + Createfilename + " and partname=" + partname);
// POST (Create)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(Createfilename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(Createfilename), creds, cache);
}
assertEquals(201, url.getStatus());
String[] path1 = url.getURL().split("/");
return "/" + mungeurl + "/" + path1[2];
}
Aggregations