use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class TestService method testPersonContact.
// @Test
public void testPersonContact() throws Exception {
String serviceurl = "personauthorities/urn:cspace:name(person)/items";
String filename = "personItem.xml";
String partname = "persons_common";
ReturnedURL url;
log.info("Testing " + serviceurl + " with " + filename + " and partname=" + partname);
// POST (Create)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
}
assertEquals(201, url.getStatus());
// log.info("CREATE PERSON" + url.getURL());
// assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
// CSPACE-305 hasn't regressed
log.info("CREATE PERSON" + url.getURL());
// create contact person
String serviceurlContact = "personauthorities/urn:cspace:name(person)/items/" + url.getURLTail() + "/contacts";
String filenameContact = "personItemContact.xml";
String partnameContact = "contacts_common";
log.info("ADD CONTACT USING THIS URL " + serviceurlContact);
testPostGetDelete(serviceurlContact, partnameContact, filenameContact, "contacts_common/email", "email@example.com");
// DELETE (Delete)
int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals(404, status);
log.info("DELETE PERSON");
// GET once more to make sure it isn't there
int getStatus;
Document doc;
if (partname != null) {
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, getStatus);
assertNull(doc);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class ServicesRelationStorage method autocreateJSON.
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
extractPaths(filePath, new String[] { "main" }, 0);
Map<String, Document> in = new HashMap<String, Document>();
Document datapath = dataToRelation(cache, null, data).toDocument();
// log.info("AUTOCREATE"+datapath.asXML());
in.put("relations_common", datapath);
ReturnedURL out = conn.getMultipartURL(RequestMethod.POST, "/relations/", in, creds, cache);
if (out.getStatus() > 299)
throw new UnderlyingStorageException("Could not add relation status=" + out.getStatus(), out.getStatus(), "/relations/");
return out.getURLTail();
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Could not add relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Could not retrieve data" + e.getLocalizedMessage());
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class UserStorage method autoCreateSub.
@Override
protected ReturnedURL autoCreateSub(CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, Document doc, String savePrefix, Record r) throws JSONException, UnderlyingStorageException, ConnectionException {
ReturnedURL url;
jsonObject = correctPassword(jsonObject);
jsonObject = correctScreenName(jsonObject);
jsonObject = correctUserId(jsonObject);
doc = XmlJsonConversion.convertToXml(r, jsonObject, "common", "POST");
url = conn.getURL(RequestMethod.POST, r.getServicesURL() + "/", doc, creds, cache);
if (url.getStatus() > 299 || url.getStatus() < 200)
throw new UnderlyingStorageException("Bad response ", url.getStatus(), r.getServicesURL() + "/");
return url;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class ConfiguredVocabStorage method autocreateJSON.
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
Map<String, Document> body = new HashMap<String, Document>();
String vocab = null;
String pathurl = "/" + r.getServicesURL() + "/";
if (filePath.equals("")) {
for (String section : r.getServicesInstancesPaths()) {
String path = r.getServicesInstancesPath(section);
String[] record_path = path.split(":", 2);
String[] tag_path = record_path[1].split(",", 2);
Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, vocab, null, r, true);
if (temp != null) {
body.put(record_path[0], temp);
// log.info(temp.asXML());
}
}
} else {
vocab = RefName.shortIdToPath(filePath);
pathurl = "/" + r.getServicesURL() + "/" + vocab + ITEMS_SUFFIX;
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
String[] tag_path = record_path[1].split(",", 2);
Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, vocab, null, r, false);
if (temp != null) {
body.put(record_path[0], temp);
// log.info(temp.asXML());
}
}
}
handleHierarchyPayloadSend(r, body, jsonObject, null);
ReturnedURL out = conn.getMultipartURL(RequestMethod.POST, pathurl, body, creds, cache);
if (out.getStatus() > 299)
throw new UnderlyingStorageException("Could not create vocabulary", out.getStatus(), pathurl);
String csid = out.getURLTail();
// create related sub records?
for (FieldSet fs : r.getAllSubRecords("POST")) {
Record sr = fs.usesRecordId();
// sr.getID()
if (sr.isType("authority")) {
String savePath = out.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 {
log.warn("autocreateJSON: Contact subrecord is malformed (not a JSONObject)!");
if (log.isDebugEnabled()) {
log.debug("autocreateJSON: Contact subrecord: " + subdata.toString());
}
}
}
} 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 csid;
} 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 BlobStorage method retrieveJSON.
@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
JSONObject result = null;
try {
if (r.isType("report") == true) {
Document doc = null;
Map<String, Document> parts = new HashMap<String, Document>();
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc = XmlJsonConversion.convertToXml(r, restrictions, section, "POST");
if (doc != null) {
parts.put(record_path[0], doc);
}
}
Returned response = null;
JSONObject out = new JSONObject();
if (filePath.contains("/output") == true) {
//
// <Please document what this request returns>
//
response = conn.getReportDocument(RequestMethod.GET, "reports/" + filePath, null, creds, cache);
} else if (filePath.contains(PUBLISH_URL_SUFFIX) == true) {
//
// If they asked to publish the report then we return a URL to the publicitems service
//
response = conn.getPublishedReportDocumentURL(RequestMethod.POST, "reports/" + filePath, doc, creds, cache);
ReturnedURL returnedURL = (ReturnedURL) response;
out.put("Location", returnedURL.getURL());
} else {
//
// This request returns the contents of the report.
//
response = conn.getReportDocument(RequestMethod.POST, "reports/" + filePath, doc, creds, cache);
ReturnUnknown returnUnknown = (ReturnUnknown) response;
out.put("body", returnUnknown.getBodyAsStream());
out.put("contenttype", returnUnknown.getContentType());
out.put("contentdisposition", returnUnknown.getContentDisposition());
}
int status = response.getStatus();
if (status > 299 || status < 200) {
throw new UnderlyingStorageException("Bad response ", status, r.getServicesURL() + "/");
}
result = out;
} else if (r.isType("batch")) {
Document doc = null;
Map<String, Document> parts = new HashMap<String, Document>();
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc = XmlJsonConversion.convertToXml(r, restrictions, section, "POST");
if (doc != null) {
parts.put(record_path[0], doc);
}
}
ReturnedDocument doc2 = null;
if (filePath.contains("/output")) {
doc2 = conn.getBatchDocument(RequestMethod.GET, "batch/" + filePath, null, creds, cache);
} else {
doc2 = conn.getBatchDocument(RequestMethod.POST, "batch/" + filePath, doc, creds, cache);
}
if (doc2.getStatus() > 299 || doc2.getStatus() < 200)
throw new UnderlyingStorageException("Bad response ", doc2.getStatus(), r.getServicesURL() + "/");
JSONObject out = new JSONObject();
this.convertToJson(out, doc2.getDocument(), r.getSpec().getRecord("invocationresults"), "", "invocationResults", filePath);
return out;
} else {
//
// We're being asked for an image or some other attachment.
//
String[] parts = filePath.split("/");
if (parts.length >= 2) {
String imagefilePath = parts[0];
String view = parts[1];
String extra = "";
if (parts.length == 3) {
extra = parts[2];
}
if (view.equalsIgnoreCase(ORIGINAL_CONTENT)) {
result = originalViewRetrieveImg(root, creds, cache, imagefilePath, view, extra, restrictions);
} else {
result = viewRetrieveImg(root, creds, cache, imagefilePath, view, extra, restrictions);
}
} else
result = simpleRetrieveJSON(creds, cache, filePath);
}
} catch (JSONException x) {
throw new UnderlyingStorageException("Error building JSON", x);
} catch (UnsupportedEncodingException x) {
throw new UnderlyingStorageException("Error UnsupportedEncodingException JSON", x);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
}
return result;
}
Aggregations