use of org.dom4j.Node in project application by collectionspace.
the class VocabInstanceCache method buildVocabularies.
// XXX pagination? argh
@SuppressWarnings("unchecked")
private void buildVocabularies(CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException, UnderlyingStorageException {
ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/" + r.getServicesURL() + "/", null, creds, cache);
Document doc = data.getDocument();
if (doc == null)
throw new UnderlyingStorageException("Could not retrieve vocabularies");
String[] path_parts = r.getServicesInstancesPath().split(":", 2);
String[] tag_parts = path_parts[1].split(",", 2);
List<Node> objects = doc.getDocument().selectNodes(tag_parts[1]);
for (Node object : objects) {
String name = "MISSING";
if (null != object.selectSingleNode("displayName")) {
name = object.selectSingleNode("displayName").getText();
}
if (null == object.selectSingleNode("shortIdentifier")) {
continue;
}
String base = object.selectSingleNode("shortIdentifier").getText();
if (base == null)
continue;
if (!vocabs.containsKey(base)) {
vocabs.put(base, name);
}
csids.put(base, object.selectSingleNode("csid").getText());
}
}
use of org.dom4j.Node in project application by collectionspace.
the class TestService method testPersonContactPostViaCSIDs.
@Test
public void testPersonContactPostViaCSIDs() throws Exception {
String filename = "";
String partname = "";
ReturnedURL url = null;
Map<String, Document> parts = new HashMap<String, Document>();
StringBuilder serviceurl = new StringBuilder("");
ReturnedMultipartDocument rdocs = null;
ReturnedDocument rdoc = null;
int status = 0;
Document doc = null;
String text = "";
String xpath = "";
// POST (Create) a person authority
serviceurl.append("personauthorities/");
partname = "personauthorities_common";
filename = "personAuth.xml";
log.info("Testing create at " + serviceurl + " with " + filename + " and partname=" + partname);
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl.toString(), parts, creds, cache);
assertEquals(201, url.getStatus());
String authUrl = url.getURL();
String authId = url.getURLTail();
// Test creation with a GET
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, authUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
assertEquals(200, status);
assertNotNull(doc);
log.info("CREATED PERSONAUTHORITY AT " + authUrl);
// POST (Create) a person item within the person authority
serviceurl.append(authId + "/items/");
partname = "persons_common";
String partname1 = "relations-common-list";
filename = "personItem.xml";
String filename1 = "relationshipItem.xml";
log.info("Testing create at " + serviceurl + " with " + filename + " and partname=" + partname);
if (partname != null) {
parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
// parts.put(partname1, getDocument(filename1));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl.toString(), parts, creds, cache);
}
assertEquals(201, url.getStatus());
String itemUrl = url.getURL();
String itemId = url.getURLTail();
// Test creation with a GET
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, itemUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
assertEquals(200, status);
assertNotNull(doc);
// Test that the parent authority lists this item as a child
String parentUrl = authUrl + "/items/";
log.info("LIST from " + parentUrl);
rdoc = conn.getXMLDocument(RequestMethod.GET, parentUrl, null, creds, cache);
status = rdoc.getStatus();
doc = rdoc.getDocument();
assertEquals(200, status);
log.info(doc.asXML());
xpath = "//totalItems";
Node n = doc.selectSingleNode(xpath);
assertNotNull(n);
text = n.getText();
assertNotNull(text);
log.info("Value of XPath expression '" + xpath + "' = " + text);
assert (!text.trim().equals("0"));
xpath = "//list-item/csid";
List<Node> nodes = doc.selectNodes(xpath);
assertNotNull(nodes);
assert (nodes.size() > 0);
boolean foundItemInAuthority = false;
for (Node node : nodes) {
log.info("found '" + node.getText().trim() + "' comparing to " + itemId);
if (node.getText().trim().equals(itemId)) {
foundItemInAuthority = true;
}
}
assert (foundItemInAuthority);
log.info("CREATED PERSON AT " + itemUrl);
// POST (Create) a contact sub-resource within the person item
// and perform a full POST, GET, DELETE cycle on that contact
serviceurl.append(itemId + "/contacts");
partname = "contacts_common";
filename = "personItemContact.xml";
log.info("ADDING CONTACT USING THIS URL " + serviceurl);
testPostGetDelete(serviceurl.toString(), partname, filename, "contacts_common/emailGroupList/emailGroup/email", "test@example.com");
// DELETE (Delete) the person item within the person authority
status = conn.getNone(RequestMethod.DELETE, itemUrl, null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, itemUrl, null, creds, cache);
assertEquals(404, status);
// GET once more to make sure it isn't there
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, itemUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, status);
assertNull(doc);
log.info("DELETED PERSON");
// DELETE (Delete) the person authority
status = conn.getNone(RequestMethod.DELETE, authUrl, null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, authUrl, null, creds, cache);
assertEquals(404, status);
// GET once more to make sure it isn't there
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, authUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, status);
assertNull(doc);
log.info("DELETED PERSON AUTHORITY");
}
use of org.dom4j.Node in project application by collectionspace.
the class AuthorizationStorage method getPaths.
/**
* Returns a list of csid's from a certain type of record
*/
@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
Document list = null;
List<String> out = new ArrayList<String>();
String path = getRestrictedPath(r.getServicesURL(), restrictions, "res", "", false, "");
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (all.getStatus() != 200) {
throw new ConnectionException("Bad request in Authorization Storage: status not 200", all.getStatus(), path);
}
list = all.getDocument();
List<Node> objects = list.selectNodes(r.getServicesListPath());
for (Node object : objects) {
String csid = object.valueOf("@csid");
out.add(csid);
setGleanedValue(cache, r.getServicesURL() + "/" + csid, view_map.get(object.getName()), object.getText());
}
return out.toArray(new String[0]);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
}
}
use of org.dom4j.Node in project application by collectionspace.
the class AuthorizationStorage method getPathsJSON.
/**
* Gets a list of csids of a certain type of record together with the pagination info
* permissions might need to break the mold tho.
*/
@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
JSONObject out = new JSONObject();
// PLS: why on earth would reports be routed through AuthStorage?!?!
if (r.getID().equals("reports") || r.getID().equals("batch")) {
String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), "", false, "");
JSONObject data = getListView(creds, cache, path, r.getServicesListPath(), "csid", false, r);
return data;
}
Document list = null;
String prefix = null;
Boolean queryadded = false;
JSONObject pagination = new JSONObject();
List<String> listitems = new ArrayList<String>();
String serviceurl = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), "", false, "");
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, serviceurl, null, creds, cache);
if (all.getStatus() != 200) {
throw new ConnectionException("Bad request in Authorization Storage getPathsJSON: status not 200", all.getStatus(), serviceurl);
}
list = all.getDocument();
String listItemPath = r.getServicesListPath();
String[] listItemPathElements = listItemPath.split("/");
if (listItemPathElements.length != 2) {
throw new RuntimeException("Illegal list item path " + listItemPath);
}
String listNodeName = listItemPathElements[0];
String listItemNodeName = listItemPathElements[1];
String listNodeChildrenSelector = "/" + listNodeName + "/*";
List<Node> nodes = list.selectNodes(listNodeChildrenSelector);
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
String csid = node.valueOf("@csid");
listitems.add(csid);
String fullPath = r.getServicesURL() + "/" + csid;
if (view_map.get(node.getName()) != null) {
setGleanedValue(cache, fullPath, view_map.get(node.getName()), node.getText());
}
// Now glean the values we need from the child nodes
List<Node> fields = node.selectNodes("*");
for (Node field : fields) {
// Is this a field we want?
String json_name = view_map.get(field.getName());
if (json_name != null) {
String value = field.getText();
setGleanedValue(cache, r.getServicesURL() + "/" + csid, json_name, value);
}
}
} else {
pagination.put(node.getName(), node.getText());
}
}
out.put("pagination", pagination);
out.put("listItems", listitems.toArray(new String[0]));
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
}
}
use of org.dom4j.Node in project application by collectionspace.
the class AuthorizationStorage method deleteJSON.
/**
* Remove an object in the Service Layer.
*/
@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
int status = 0;
if (r.hasDeleteMethod()) {
// get all data to post back
// hopefully the filepath we have is the filepath to get the data from
Document doc = null;
try {
doc = simpleRetrieveXML(creds, cache, filePath + "/");
String nodepath = "/" + r.getServicesListPath() + "/*";
List<Node> nodes = doc.selectNodes(nodepath);
if (nodes.size() > 0) {
status = 201;
status = conn.getNone(RequestMethod.DELETE, filePath, null, creds, cache);
// post it back to delete it
/*
log.info("TRYING TO DELETE ACCOUNTROLE");
log.info(doc.asXML());
log.info(filePath+"?_method=delete");
ReturnedURL url = null;
url = conn.getURL(RequestMethod.POST, filePath+"?_method=delete", doc, creds, cache);
status = url.getStatus();
//status=conn.getNone(RequestMethod.POST,r.getServicesURL()+"/"+filePath+"?_method=delete",doc,creds,cache);
*/
} else {
// if we didn't need to delete it because there was nothing there then everything was good
status = 201;
}
} catch (ExistException ex) {
// ignore as nothign to delete if nothing exists
status = 201;
}
} else {
status = conn.getNone(RequestMethod.DELETE, r.getServicesURL() + "/" + filePath, null, creds, cache);
}
if (// XXX CSPACE-73, should be 404
status > 299 || status < 200)
throw new UnderlyingStorageException("Service layer exception status=" + status, status, r.getServicesURL() + "/" + filePath);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception", e);
}
}
Aggregations