Search in sources :

Example 1 with ReturnUnknown

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

the class TestService method testReporting.

// @Test
// remove test as never know if all the bits for the report are there to test
public void testReporting() throws Exception {
    ReturnedURL url;
    int getStatus;
    Document doc;
    String serviceurl = "acquisitions/";
    String partname = "acquisitions_common";
    String filename = "acquisitionXMLJSON.xml";
    String xpath = "acquisitions_common/accessionDate";
    String expected = "April 1, 2010";
    // POST (Create Acquisition Record)
    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("Failed to receive 201 status code on create", 201, url.getStatus());
    // find report
    ReturnedDocument doc3 = conn.getXMLDocument(RequestMethod.GET, "reports?doctype=Acquisition", null, creds, cache);
    assertEquals(200, doc3.getStatus());
    Set<String> csids = new HashSet<String>();
    String reportcsid = "";
    for (Node n : (List<Node>) doc3.getDocument().selectNodes("abstract-common-list/list-item/csid")) {
        reportcsid = n.getText();
    }
    assertFalse("No Acquisition report to test with", reportcsid.equals(""));
    if (!reportcsid.equals("")) {
        // only runs if there is a report to run
        String reportsurl = "reports/" + reportcsid;
        String csid = url.getURLTail();
        Document report = getDocument("reportrun.xml");
        report.selectSingleNode("invocationContext/singleCSID").setText(csid);
        // DO REPORT
        // run report
        ReturnUnknown doc2 = conn.getReportDocument(RequestMethod.POST, reportsurl, report, creds, cache);
        JSONObject out = new JSONObject();
        out.put("getByteBody", doc2.getBytes());
        out.put("contenttype", doc2.getContentType());
        assertEquals("Failed to receive 200 status code on create", 200, doc2.getStatus());
    }
    // DELETE (Delete Acquisition)
    int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals("Failed to receive expected 200 status code on delete", 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("Failed to receive expected 404 status code on repeated delete of same record", 404, status);
    log.info("DELETE");
    // 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();
    }
    assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, // ensures CSPACE-209 hasn't regressed
    getStatus);
    assertNull("Contents of deleted record were unexpectedly not null", doc);
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnUnknown(org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown) 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) JSONObject(org.json.JSONObject) List(java.util.List) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) HashSet(java.util.HashSet)

Example 2 with ReturnUnknown

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

the class BlobStorage method viewRetrieveImg.

/*
	 * This method returns actual blob bits -either the original blob bits or those of a derivative
	 */
public JSONObject viewRetrieveImg(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String inFilePath, String view, String extra, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException, UnsupportedEncodingException {
    JSONObject out = new JSONObject();
    String servicesurl = r.getServicesURL() + "/";
    try {
        String contentSuffix = "/content";
        String filePath = inFilePath;
        if (view.equalsIgnoreCase(ORIGINAL_CONTENT)) {
            filePath = filePath + contentSuffix;
        } else {
            filePath = filePath + "/derivatives/" + view + contentSuffix;
        }
        String softpath = filePath;
        if (r.hasSoftDeleteMethod()) {
            softpath = softpath(filePath);
        }
        if (r.hasHierarchyUsed("screen")) {
            softpath = hierarchicalpath(softpath);
        }
        // ReturnUnknown doc = conn.getUnknownDocument(RequestMethod.GET, servicesurl+softpath, null, creds, cache);
        ReturnUnknown doc = viewRetrieveImg(view, servicesurl + softpath, creds, cache);
        if (doc.getStatus() < 200 || doc.getStatus() >= 300) {
            throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
        }
        // REM: We're returning an array of bytes here and we probably should be using a stream of bytes
        out.put("getByteBody", doc.getBytes());
        out.put("contenttype", doc.getContentType());
        out.put("contentdisposition", doc.getContentDisposition());
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception " + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
    return out;
}
Also used : ReturnUnknown(org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown) JSONObject(org.json.JSONObject) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 3 with ReturnUnknown

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

the class BlobStorage method viewRetrieveImg.

/**
 * Try several times to get an image view.  Views (aka Derivatives) are created asynchronously by Nuxeo in the
 * Services layer, so it might take a second or two for them to become available.
 *
 * @param url
 * @param creds
 * @param cache
 * @return
 * @throws ConnectionException
 */
private ReturnUnknown viewRetrieveImg(String view, String url, CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException {
    ReturnUnknown result = null;
    int attempts = 0;
    long timeOutValue = System.currentTimeMillis() + DERIVATIVE_TIMEOUT;
    while (result == null && System.currentTimeMillis() < timeOutValue) {
        result = conn.getUnknownDocument(RequestMethod.GET, url, null, creds, cache);
        if (result.getStatus() < 200 || result.getStatus() >= 300) {
            // Go to sleep for 1/2 second while derivative gets created.
            sleep(500);
            result = null;
        }
        attempts++;
    }
    if (System.currentTimeMillis() > timeOutValue && result == null) {
        log.warn(String.format("Timed out after trying %s time(s) to retrieve '%s' view/derivative.", attempts, view));
    } else {
        log.debug(String.format("Successfully retrieved '%s' view/derivative after %s attempt(s).", view, attempts));
    }
    return result;
}
Also used : ReturnUnknown(org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)

Example 4 with ReturnUnknown

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

Aggregations

ReturnUnknown (org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)4 JSONObject (org.json.JSONObject)3 HashMap (java.util.HashMap)2 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)2 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)2 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)2 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)2 Document (org.dom4j.Document)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Returned (org.collectionspace.chain.csp.persistence.services.connection.Returned)1 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)1 Node (org.dom4j.Node)1 JSONException (org.json.JSONException)1