Search in sources :

Example 26 with ConnectionException

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

the class ServicesRelationStorage method retrieveJSON.

@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Boolean isHierarchical = false;
        String[] parts = null;
        if (isPathType(filePath, new String[] { "main" }, 1)) {
            parts = extractPaths(filePath, new String[] { "main" }, 1);
        } else if (isPathType(filePath, new String[] { "hierarchical" }, 1)) {
            parts = extractPaths(filePath, new String[] { "hierarchical" }, 1);
            isHierarchical = true;
        }
        ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.GET, "/relations/" + parts[0], null, creds, cache);
        if (out.getStatus() == 404)
            throw new UnderlyingStorageException("Could not retrieve relation", out.getStatus(), "/relations/" + parts[0]);
        Document doc = out.getDocument("relations_common");
        if (doc == null)
            throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common", out.getStatus(), "/relations/" + parts[0]);
        return relationToData(cache, factory.load(parts[0], doc));
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JaxenException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e);
    }
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) JaxenException(org.jaxen.JaxenException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 27 with ConnectionException

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

the class ServicesRelationStorage method autocreateJSON.

@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        extractPaths(filePath, new String[] { "main" }, 0);
        Map<String, Document> in = new HashMap<String, Document>();
        Document datapath = dataToRelation(cache, null, data).toDocument();
        // log.info("AUTOCREATE"+datapath.asXML());
        in.put("relations_common", datapath);
        ReturnedURL out = conn.getMultipartURL(RequestMethod.POST, "/relations/", in, creds, cache);
        if (out.getStatus() > 299)
            throw new UnderlyingStorageException("Could not add relation status=" + out.getStatus(), out.getStatus(), "/relations/");
        return out.getURLTail();
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not add relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve data" + e.getLocalizedMessage());
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) 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) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 28 with ConnectionException

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

the class ServicesRelationStorage method deleteJSON.

@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String[] parts = extractPaths(filePath, new String[] { "main" }, 1);
        // log.info("DELETE"+parts[0]);
        int status = conn.getNone(RequestMethod.DELETE, "/relations/" + parts[0], null, creds, cache);
        if (status > 299)
            throw new UnderlyingStorageException("Could not delete relation, status=" + status, status, "/relations/" + parts[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not delete relation" + 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 29 with ConnectionException

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

the class UserStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        // XXX when CSPACE-1458 is fixed, remove the call to
        // xxx_cspace1458_fix, and just pass jsonObject as this arg. (fao
        // Chris or somoeone else at CARET).
        jsonObject = correctPassword(jsonObject);
        Document in = XmlJsonConversion.convertToXml(r, jsonObject, "common", "PUT");
        // Document
        // in=XmlJsonConversion.convertToXml(r,xxx_cspace1458_fix(filePath,jsonObject,creds,cache),"common");
        // log.info("Sending: " + in.asXML());
        ReturnedDocument doc = conn.getXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, in, creds, cache);
        // ExistException("Not found: "+r.getServicesURL()+"/"+filePath);
        if (doc.getStatus() > 299 || doc.getStatus() < 200)
            throw new UnderlyingStorageException("Bad response ", doc.getStatus(), r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException" + e.getLocalizedMessage(), e);
    }
}
Also used : JSONException(org.json.JSONException) Document(org.dom4j.Document) 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) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 30 with ConnectionException

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

the class UserStorage method retrieveJSON.

@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Boolean isUserRole = false;
        String[] parts = filePath.split("/");
        if (parts.length >= 2 && parts[1].equals("userrole")) {
            isUserRole = true;
            String path = r.getSpec().getRecord("userrole").getServicesURL();
            int len = parts.length - 1;
            int i = 0;
            for (i = 0; i < len; i++) {
                path = path.replace("*", parts[i]);
                i++;
            }
            if (len >= i) {
                path = path + "/" + parts[len];
            } else {
                path = path + "/";
            }
            filePath = path;
        } else {
            filePath = r.getServicesURL() + "/" + filePath;
        }
        ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
        JSONObject out = new JSONObject();
        Document xml = null;
        xml = doc.getDocument();
        if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
            throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
        if (isUserRole)
            out = XmlJsonConversion.convertToJson(r.getSpec().getRecord("userrole"), xml, "GET", "common", "", // XXX hardwired common section :(
            "");
        else
            // XXX hardwired common section :(
            out = XmlJsonConversion.convertToJson(r, xml, "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 : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Document(org.dom4j.Document) 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)

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