Search in sources :

Example 6 with ReturnedURL

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

the class GenericStorage method autocreateJSON.

/**
 * Convert the JSON from the UI Layer into XML for the Service layer while using the XML structure from cspace-config.xml
 * Send the XML through to the Service Layer to store it in the database
 * The Service Layer returns a url to the object we just stored.
 * @param {ContextualisedStorage} root
 * @param {CSPRequestCredentials} creds
 * @param {CSPRequestCache} cache
 * @param {String} filePath part of the path to the Service URL (containing the type of object)
 * @param {JSONObject} jsonObject The JSON string coming in from the UI Layer, containing the object to be stored
 * @return {String} csid The id of the object in the database
 */
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // used by userroles and permroles as they have complex urls
        if (r.hasPrimaryField()) {
            for (String section : r.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
                String path = r.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, r.getPrimaryField()));
                String restrictedPath = getRestrictedPath(path, restrictions, null);
                deleteJSON(root, creds, cache, restrictedPath);
                url = conn.getURL(RequestMethod.POST, restrictedPath, doc, creds, cache);
            }
        } else {
            String restrictedPath = getRestrictedPath(r.getServicesURL(), restrictions, null);
            // REM - We need a way to send query params (restrictions) on POST and UPDATE
            url = autoCreateSub(creds, cache, jsonObject, doc, restrictedPath, r);
        }
        // I am developing this.. it might not work...
        for (FieldSet fs : r.getAllSubRecords("POST")) {
            Record sr = fs.usesRecordId();
            // sr.getID()
            if (sr.isType("authority")) {
            // need to use code from configuredVocabStorage
            } else {
                String savePath = url.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 {
                    // 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 url.getURLTail();
    } catch (ConnectionException e) {
        String msg = e.getMessage();
        if (e.getStatus() == 403) {
            // permissions error
            msg += " permissions error";
        }
        throw new UnderlyingStorageException(msg, e.getStatus(), e.getUrl(), e);
    } catch (UnderlyingStorageException e) {
        // REM - CSPACE-5632: Need to catch and rethrow this exception type to prevent throwing an "UnimplementedException" exception below.
        throw e;
    } catch (Exception e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) JSONArray(org.json.JSONArray) 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) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) DocumentException(org.dom4j.DocumentException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) IOException(java.io.IOException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 7 with ReturnedURL

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

the class GenericStorage method autoCreateSub.

/**
 * create sub records so can be connected to their parent record
 * e.g. blob/media contact/person
 * @param creds
 * @param cache
 * @param jsonObject
 * @param doc
 * @param savePrefix
 * @param r
 * @return
 * @throws JSONException
 * @throws UnderlyingStorageException
 * @throws ConnectionException
 * @throws ExistException
 */
protected ReturnedURL autoCreateSub(CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, Document doc, String savePrefix, Record r) throws JSONException, UnderlyingStorageException, ConnectionException, ExistException {
    ReturnedURL url;
    Map<String, Document> parts = new HashMap<String, Document>();
    Document doc2 = doc;
    for (String section : r.getServicesRecordPathKeys()) {
        String path = r.getServicesRecordPath(section);
        String[] record_path = path.split(":", 2);
        doc2 = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
        if (doc2 != null) {
            doc = doc2;
            parts.put(record_path[0], doc2);
        // log.info(doc.asXML());
        // log.info(savePrefix);
        }
    }
    // This checks for hierarchy support, and does nothing if not appropriate.
    handleHierarchyPayloadSend(r, parts, jsonObject, null);
    // some records are accepted as multipart in the service layers, others arent, that's why we split up here
    if (r.isMultipart())
        url = conn.getMultipartURL(RequestMethod.POST, savePrefix, parts, creds, cache);
    else
        url = conn.getURL(RequestMethod.POST, savePrefix, doc, creds, cache);
    if (url.getStatus() > 299 || url.getStatus() < 200)
        throw new UnderlyingStorageException("Bad response ", url.getStatus(), savePrefix);
    return url;
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) 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) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 8 with ReturnedURL

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

the class BlobStorage method autocreateJSON.

@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    ReturnedURL url = null;
    try {
        byte[] bitten = (byte[]) jsonObject.get("getbyteBody");
        String uploadname = jsonObject.getString("fileName");
        String type = jsonObject.getString("contentType");
        String path = r.getServicesURL();
        url = conn.getStringURL(RequestMethod.POST, path, bitten, uploadname, type, creds, cache);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException(e.getMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException", e);
    }
    return conn.getBase() + url.getURL();
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 9 with ReturnedURL

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

the class GenericStorage method subautocreateJSON.

/**
 * needs some tests.. just copied from ConfiguredVocabStorage
 * @param root
 * @param creds
 * @param cache
 * @param myr
 * @param jsonObject
 * @param savePrefix
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 */
public String subautocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, Record myr, JSONObject jsonObject, String savePrefix) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // XXX I would hope this might be removed if userroles etc ever get improved to be more like the rest
        if (myr.hasPrimaryField()) {
            for (String section : myr.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(myr, jsonObject, section, "POST");
                String path = myr.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, myr.getPrimaryField()));
                deleteJSON(root, creds, cache, path);
                url = conn.getURL(RequestMethod.POST, path, doc, creds, cache);
            }
        } else {
            url = autoCreateSub(creds, cache, jsonObject, doc, savePrefix, myr);
        }
        // create related sub records?
        for (FieldSet allfs : myr.getAllSubRecords("POST")) {
            Record sr = allfs.usesRecordId();
            if (sr.isType("authority")) {
            } else {
                String savePath = url.getURL() + "/" + sr.getServicesURL();
                if (jsonObject.has(sr.getID())) {
                    Object subdata = jsonObject.get(sr.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);
                        }
                    } else if (subdata instanceof JSONObject) {
                        JSONObject subrecord = (JSONObject) subdata;
                        subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                    }
                }
            }
        }
        return url.getURLTail();
    } 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) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject) 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) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 10 with ReturnedURL

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

the class TestDummyData method create.

private String create(String serviceurl, String partname, String Createfilename, String mungeurl) 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());
    String[] path1 = url.getURL().split("/");
    return "/" + mungeurl + "/" + path1[2];
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) HashMap(java.util.HashMap) Document(org.dom4j.Document)

Aggregations

ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)20 Document (org.dom4j.Document)18 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)17 HashMap (java.util.HashMap)16 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)15 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)9 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)6 JSONException (org.json.JSONException)6 Node (org.dom4j.Node)5 JSONObject (org.json.JSONObject)5 Test (org.junit.Test)5 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)3 Record (org.collectionspace.chain.csp.schema.Record)3 JSONArray (org.json.JSONArray)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ReturnUnknown (org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)2 Field (org.collectionspace.chain.csp.schema.Field)2 Group (org.collectionspace.chain.csp.schema.Group)2