Search in sources :

Example 31 with ConnectionException

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

the class ConfiguredVocabStorage method deleteJSON.

@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String vocab = RefName.shortIdToPath(filePath.split("/")[0]);
        String url = generateURL(vocab, filePath.split("/")[1], "", this.r);
        if (r.hasSoftDeleteMethod()) {
            // The url we compute already has the filepath built in, so just pass an empty
            // filepath, and it will work out.
            String emptyFilepath = "";
            super.transitionWorkflowJSON(root, creds, cache, emptyFilepath, url, WORKFLOW_TRANSITION_DELETE);
        } else {
            int status = conn.getNone(RequestMethod.DELETE, url, null, creds, cache);
            if (status > 299)
                throw new UnderlyingStorageException("Could not retrieve vocabulary", status, url);
        }
    // cache.removeCached(getClass(),new String[]{"namefor",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"reffor",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"shortId",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"csidfor",vocab,filePath.split("/")[1]});
    // delete name and id versions from teh cache?
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 32 with ConnectionException

use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException 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);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) 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) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 33 with ConnectionException

use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException 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 34 with ConnectionException

use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException 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)

Example 35 with ConnectionException

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

the class GenericStorage method transitionWorkflowJSON.

public void transitionWorkflowJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String serviceurl, String workflowTransition) throws UnderlyingStorageException {
    try {
        String url = serviceurl + filePath + WORKFLOW_SUBRESOURCE + "/" + workflowTransition;
        int status = conn.getNone(RequestMethod.PUT, url, null, creds, cache);
        if (status > 299 || status < 200)
            throw new UnderlyingStorageException("Bad response ", status, serviceurl + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception (workflow transition)" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)39 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)37 JSONException (org.json.JSONException)28 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)26 Document (org.dom4j.Document)24 JSONObject (org.json.JSONObject)23 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 ArrayList (java.util.ArrayList)11 Node (org.dom4j.Node)11 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)10 HashMap (java.util.HashMap)8 Field (org.collectionspace.chain.csp.schema.Field)8 Record (org.collectionspace.chain.csp.schema.Record)8 ExistException (org.collectionspace.csp.api.persistence.ExistException)8 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)8 JSONArray (org.json.JSONArray)8 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)6 Group (org.collectionspace.chain.csp.schema.Group)6 IOException (java.io.IOException)3