Search in sources :

Example 21 with ConnectionException

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

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

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

the class ServicesIDGenerator method retrieveJSON.

/**
 * This function generates a new id for the next inputted object.
 * The id in the static list on top is the primary key in the generators_id table on the service layer.
 */
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String val = conn.getTextDocument(RequestMethod.POST, "idgenerators/" + generators.get(filePath) + "/ids", null, creds, cache);
        JSONObject out = new JSONObject();
        out.put("next", val);
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("JSON exception" + e.getLocalizedMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 24 with ConnectionException

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

the class AuthorizationStorage method refViewRetrieveJSON.

@Override
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
    try {
        JSONObject out = new JSONObject();
        // not all the records need a reference, look in cspace-config.xml for which that don't
        if (r.hasTermsUsed()) {
            String path = r.getServicesURL() + "/" + filePath + "/authorityrefs";
            ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
            if (all.getStatus() != 200)
                throw new ConnectionException("Bad request problem in AuthorizationStorage/refViewRetrieve: status not 200", all.getStatus(), path);
            Document list = all.getDocument();
            for (Object node : list.selectNodes("authority-ref-list/authority-ref-item")) {
                if (!(node instanceof Element))
                    continue;
                String key = ((Element) node).selectSingleNode("sourceField").getText();
                String uri = ((Element) node).selectSingleNode("uri").getText();
                String refname = ((Element) node).selectSingleNode("refName").getText();
                if (uri != null && uri.startsWith("/"))
                    uri = uri.substring(1);
                JSONObject data = miniForURI(storage, creds, cache, refname, uri, restrictions);
                out.put(key, data);
            }
        }
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection problem in AuthorizationStorage:" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) Element(org.dom4j.Element) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 25 with ConnectionException

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

the class AuthorizationStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Map<String, Document> parts = new HashMap<String, Document>();
        Document doc = null;
        for (String section : r.getServicesRecordPathKeys()) {
            String path = r.getServicesRecordPath(section);
            String[] record_path = path.split(":", 2);
            doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "PUT");
            parts.put(record_path[0], doc);
        }
        int status = 0;
        if (r.isMultipart()) {
            ReturnedMultipartDocument docm = conn.getMultipartXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, parts, creds, cache);
            status = docm.getStatus();
        } else {
            ReturnedDocument docm = conn.getXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, doc, creds, cache);
            status = docm.getStatus();
        }
        if (status == 404)
            throw new ExistException("Not found: " + r.getServicesURL() + "/" + filePath);
        if (status > 299 || status < 200)
            throw new UnderlyingStorageException("Bad response " + status, status, r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : HashMap(java.util.HashMap) 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) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) 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