Search in sources :

Example 6 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument 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");
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) HashMap(java.util.HashMap) Node(org.dom4j.Node) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) Test(org.junit.Test)

Example 7 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.

the class TestService method testOrgContact.

// @Test
public void testOrgContact() throws Exception {
    String serviceurl = "orgauthorities/urn:cspace:name(organization)/items";
    String filename = "orgItem.xml";
    String partname = "organizations_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());
    // doesn't work because name urn gets translated to id
    // assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
    // CSPACE-305 hasn't regressed
    log.info("CREATE ORG" + url.getURL());
    // create contact person
    String serviceurlContact = "orgauthorities/urn:cspace:name(organization)/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 ORG");
    // 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);
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Example 8 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument 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);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 9 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.

the class AuthorizationStorage method simpleRetrieveJSONFullPath.

public JSONObject simpleRetrieveJSONFullPath(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        JSONObject out = new JSONObject();
        if (thisr.isMultipart()) {
            ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
            for (String section : thisr.getServicesRecordPathKeys()) {
                String path = thisr.getServicesRecordPath(section);
                String[] parts = path.split(":", 2);
                convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, "", "");
            }
        } else {
            ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300)) {
                if (doc.getStatus() == 401) {
                    throw new UnauthorizedException("Username and/or password are invalid.", doc.getStatus(), filePath);
                } else if (doc.getStatus() == 409) {
                    throw new ConflictException("Conflict with request. The user's tenant may be disabled. Contact your CollectionSpace administrator.", doc.getStatus(), filePath);
                } else {
                    String status = Integer.toString(doc.getStatus());
                    throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
                }
            }
            convertToJson(out, doc.getDocument(), thisr, "GET", "common", "");
        }
        return out;
    } 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.getLocalizedMessage(), e);
    }
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) JSONObject(org.json.JSONObject) ConflictException(org.collectionspace.csp.api.persistence.ConflictException) UnauthorizedException(org.collectionspace.csp.api.persistence.UnauthorizedException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 10 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument 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);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) JSONObject(org.json.JSONObject) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)28 Document (org.dom4j.Document)24 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)20 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)18 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)17 Node (org.dom4j.Node)16 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)8 List (java.util.List)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)4 Field (org.collectionspace.chain.csp.schema.Field)3 JSONArray (org.json.JSONArray)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 ReturnUnknown (org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)2