use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class GenericStorage method simpleRetrieveJSON.
/**
* return data just as the service layer gives it to the App layer
* no extra columns required
* @param creds
* @param cache
* @param filePath
* @param servicesurl
* @param thisr
* @return
* @throws ExistException
* @throws UnimplementedException
* @throws UnderlyingStorageException
*/
public JSONObject simpleRetrieveJSON(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String servicesurl, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
String csid = "";
if (filePath == null) {
filePath = "";
}
String[] path_parts = filePath.split("/");
if (path_parts.length > 1)
csid = path_parts[1];
else
csid = filePath;
JSONObject out = new JSONObject();
try {
String softpath = filePath;
if (thisr.hasSoftDeleteMethod()) {
softpath = softpath(filePath);
}
if (thisr.hasHierarchyUsed("screen")) {
softpath = hierarchicalpath(softpath);
}
if (thisr.isMultipart()) {
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
for (String section : thisr.getServicesRecordPathKeys()) {
String path = thisr.getServicesRecordPath(section);
String[] parts = path.split(":", 2);
if (doc.getDocument(parts[0]) != null) {
convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, csid);
}
}
// If this record has hierarchy, will pull out the relations section and map it to the hierarchy
// fields (special case handling of XML-JSON
handleHierarchyPayloadRetrieve(thisr, doc, out, csid);
} else {
ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
convertToJson(out, doc.getDocument(), thisr, "GET", "common", csid);
}
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception", e);
}
/*
* Get data for any sub records that are part of this record e.g. contact in person, blob in media
*/
try {
for (FieldSet fs : thisr.getAllSubRecords("GET")) {
Boolean validator = true;
Record sr = fs.usesRecordId();
if (fs.usesRecordValidator() != null) {
validator = false;
if (out.has(fs.usesRecordValidator())) {
String test = out.getString(fs.usesRecordValidator());
if (test != null && !test.equals("")) {
validator = true;
}
}
}
if (validator) {
String getPath = servicesurl + filePath + "/" + sr.getServicesURL();
if (null != fs.getServicesUrl()) {
getPath = fs.getServicesUrl();
}
if (fs.getWithCSID() != null) {
getPath = getPath + "/" + out.getString(fs.getWithCSID());
}
// need to get update and delete working? tho not going to be used for media as handling blob seperately
if (fs instanceof Group) {
JSONObject outer = simpleRetrieveJSON(creds, cache, getPath, "", sr);
JSONArray group = new JSONArray();
group.put(outer);
out.put(fs.getID(), group);
}
if (fs instanceof Repeat) {
// NEED TO GET A LIST OF ALL THE THINGS
JSONArray repeat = new JSONArray();
String path = getPath;
while (!path.equals("")) {
JSONObject data = getListView(creds, cache, path, sr.getServicesListPath(), "csid", false, r);
if (data.has("listItems")) {
String[] results = (String[]) data.get("listItems");
for (String result : results) {
JSONObject rout = simpleRetrieveJSON(creds, cache, getPath + "/" + result, "", sr);
// add in csid so I can do update with a modicum of confidence
rout.put("_subrecordcsid", result);
repeat.put(rout);
}
}
if (data.has("pagination")) {
Integer ps = Integer.valueOf(data.getJSONObject("pagination").getString("pageSize"));
Integer pn = Integer.valueOf(data.getJSONObject("pagination").getString("pageNum"));
Integer ti = Integer.valueOf(data.getJSONObject("pagination").getString("totalItems"));
if (ti > (ps * (pn + 1))) {
JSONObject restrictions = new JSONObject();
restrictions.put("pageSize", Integer.toString(ps));
restrictions.put("pageNum", Integer.toString(pn + 1));
path = getRestrictedPath(getPath, restrictions, sr.getServicesSearchKeyword(), "", false, "");
// need more values
} else {
path = "";
}
}
}
// group.put(outer);
out.put(fs.getID(), repeat);
}
}
}
} catch (Exception e) {
// ignore exceptions for sub records at the moment - make it more intelligent later
// throw new UnderlyingStorageException("Service layer exception",e);
}
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class GenericStorage method getHardListView.
/**
* return list view of items
* TODO make getHardListView and getRepeatableHardListView to share more code as they aren't different enough to warrant the level of code repeat
* @param creds
* @param cache
* @param path
* @param nodeName
* @param matchlistitem
* @param csidfield
* @param fullcsid
* @return
* @throws ConnectionException
* @throws JSONException
*/
protected JSONObject getHardListView(CSPRequestCredentials creds, CSPRequestCache cache, String path, String listItemPath, String csidfield, Boolean fullcsid) throws ConnectionException, JSONException {
String[] listItemPathElements = listItemPath.split("/");
if (listItemPathElements.length != 2) {
throw new IllegalArgumentException("Illegal list item path " + listItemPath);
}
String listNodeName = listItemPathElements[0];
String listItemNodeName = listItemPathElements[1];
String listNodeChildrenSelector = "/" + listNodeName + "/*";
JSONObject out = new JSONObject();
JSONObject pagination = new JSONObject();
Document list = null;
List<String> listitems = new ArrayList<String>();
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (all.getStatus() != 200) {
// throw new StatusException(all.getStatus(),path,"Bad request during identifier cache map update: status not 200");
throw new ConnectionException("Bad request during identifier cache map update: status not 200 is " + Integer.toString(all.getStatus()), all.getStatus(), path);
}
list = all.getDocument();
List<Node> nodes = list.selectNodes(listNodeChildrenSelector);
if (listNodeName.equals("roles_list") || listNodeName.equals("permissions_list")) {
// XXX CSPACE-1887 workaround
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
String csid = node.valueOf("@csid");
listitems.add(csid);
} else {
pagination.put(node.getName(), node.getText());
}
}
} else {
String[] allfields = null;
String fieldsReturnedName = r.getServicesFieldsPath();
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
List<Node> fields = node.selectNodes("*");
String csid = "";
String extraFieldValue = "";
String urlPlusCSID = null;
if (node.selectSingleNode(csidfield) != null) {
csid = node.selectSingleNode(csidfield).getText();
urlPlusCSID = r.getServicesURL() + "/" + csid;
setGleanedValue(cache, urlPlusCSID, "csid", csid);
}
for (Node field : fields) {
if (csidfield.equals(field.getName())) {
if (!fullcsid) {
int idx = csid.lastIndexOf("/");
if (idx != -1) {
csid = csid.substring(idx + 1);
urlPlusCSID = r.getServicesURL() + "/" + csid;
}
}
listitems.add(csid);
} else {
String json_name = view_map.get(field.getName());
if (json_name != null) {
String value = field.getText();
// XXX hack to cope with multi values
if (value == null || "".equals(value)) {
List<Node> inners = field.selectNodes("*");
for (Node n : inners) {
value += n.getText();
}
}
setGleanedValue(cache, urlPlusCSID, json_name, value);
}
}
}
if (allfields == null || allfields.length == 0) {
if (log.isWarnEnabled()) {
log.warn("getHardListView(): Missing fieldsReturned value - may cause fan-out!\nRecord:" + r.getID() + " request to: " + path);
}
} else {
// Mark all the fields not yet found as gleaned -
for (String s : allfields) {
String gleaned = getGleanedValue(cache, urlPlusCSID, s);
if (gleaned == null) {
setGleanedValue(cache, urlPlusCSID, s, "");
}
}
}
} else if (fieldsReturnedName.equals(node.getName())) {
String myfields = node.getText();
allfields = myfields.split("\\|");
} else {
pagination.put(node.getName(), node.getText());
}
}
}
out.put("pagination", pagination);
out.put("listItems", listitems.toArray(new String[0]));
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class ServicesBaseClass method setup.
protected void setup() throws ConnectionException {
Spec spec = null;
String ims_base = "";
try {
base = getBaseUrl();
ims_base = getIMSBaseUrl();
spec = getDefaultSpec();
} catch (CSPDependencyException e) {
assertNotNull("Base service url invalid in config file", base);
}
// XXX still yuck but centralised now
log.info("ServicesBaseClass setting up connection using base URL:" + base);
conn = new ServicesConnection(base, ims_base);
creds = new ServicesRequestCredentials();
creds.setCredential(ServicesStorageGenerator.CRED_USERID, spec.getAdminData().getAuthUser());
creds.setCredential(ServicesStorageGenerator.CRED_PASSWORD, spec.getAdminData().getAuthPass());
ReturnedDocument out = conn.getXMLDocument(RequestMethod.GET, "accounts/0/accountperms", null, creds, cache);
Assume.assumeTrue(out.getStatus() == 200);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestAccount method testAccountCreate.
@Test
public void testAccountCreate() throws Exception {
Storage ss = makeServicesStorage();
/* delete user so we can create it later - will return 404 if user doesn't exist */
JSONObject data = ss.getPathsJSON("users/", new JSONObject("{\"userId\":\"test31@collectionspace.org\"}"));
String[] paths = (String[]) data.get("listItems");
if (paths.length > 0)
ss.deleteJSON("users/" + paths[0]);
JSONObject u1 = getJSON("user1.json");
/* create the user based on json */
/* will give a hidden 500 error if userid is not unique (useful eh?) */
String path = ss.autocreateJSON("users/", u1, null);
assertNotNull(path);
JSONObject u2 = getJSON("user1.json");
ss.updateJSON("users/" + path, u2, new JSONObject());
JSONObject u3 = ss.retrieveJSON("users/" + path, new JSONObject());
assertNotNull(u3);
// Check output
assertEquals("Test Mccollectionspace.org", u3.getString("screenName"));
assertEquals("test31@collectionspace.org", u3.getString("userId"));
assertEquals("test31@collectionspace.org", u3.getString("email"));
assertEquals("active", u3.getString("status"));
// Check the method we're about to use to check if login works works
creds.setCredential(ServicesStorageGenerator.CRED_USERID, "test31@collectionspace.org");
creds.setCredential(ServicesStorageGenerator.CRED_PASSWORD, "blahblah");
cache.reset();
ReturnedDocument out = conn.getXMLDocument(RequestMethod.GET, "collectionobjects", null, creds, cache);
assertFalse(out.getStatus() == 200);
// Check login works
creds.setCredential(ServicesStorageGenerator.CRED_USERID, "test31@collectionspace.org");
creds.setCredential(ServicesStorageGenerator.CRED_PASSWORD, "testtestt");
cache.reset();
out = conn.getXMLDocument(RequestMethod.GET, "collectionobjects", null, creds, cache);
log.debug("Status", out.getStatus());
// assertTrue(out.getStatus()==200);
//
ss.deleteJSON("users/" + path);
/* tidy up and delete user */
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestService method testCRUD.
// TODO merge this method with testPostGetDelete - this is Chris's temporary
// fork to help testing repeatable fields
/**
* Test Create, Update, Read and Delete for a record type
*
* @param serviceurl
* @param partname
* @param Createfilename
* @param Updatefilename
* @param xpath
* @param expected
*/
private void testCRUD(String serviceurl, String partname, String Createfilename, String Updatefilename, String xpath, String expected) 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());
// ensures e.g.
assertTrue(url.getURL().startsWith("/" + serviceurl));
// CSPACE-305
// hasn't
// regressed
// GET (Read)
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();
}
assertEquals(200, getStatus);
assertNotNull(doc);
Node n = doc.selectSingleNode(xpath);
assertNotNull(n);
String text = n.getText();
assertEquals(expected, text);
// Update
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(Updatefilename));
conn.getMultipartXMLDocument(RequestMethod.PUT, url.getURL(), parts, creds, cache);
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
conn.getXMLDocument(RequestMethod.PUT, url.getURL(), getDocument(Updatefilename), creds, cache);
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
assertEquals(200, getStatus);
assertNotNull(doc);
n = doc.selectSingleNode(xpath);
assertNotNull(n);
text = n.getText();
assertEquals(expected, text);
// log.info(doc.asXML());
// Get
// 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);
// GET once more to make sure it isn't there
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);
}
Aggregations