Search in sources :

Example 21 with ReturnedDocument

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

Example 22 with ReturnedDocument

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

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

Example 24 with ReturnedDocument

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

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

the class GenericStorage method refViewRetrieveJSON.

/**
 * get data needed for terms Used block of a record
 * @param creds
 * @param cache
 * @param path
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 * @throws JSONException
 * @throws UnsupportedEncodingException
 */
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String path, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException, UnsupportedEncodingException {
    try {
        JSONObject out = new JSONObject();
        JSONObject pagination = new JSONObject();
        JSONObject listitems = new JSONObject();
        // not all the records need a reference, look in cspace-config.xml for which that don't
        if (r.hasTermsUsed()) {
            path = getRestrictedPath(path, restrictions, "kw", "", false, "");
            if (r.hasSoftDeleteMethod()) {
                // XXX completely not the right thinsg to do but a good enough hack
                path = softpath(path);
            }
            if (r.hasHierarchyUsed("screen")) {
                path = hierarchicalpath(path);
            }
            ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
            if (all.getStatus() != 200)
                throw new ConnectionException("Bad request during identifier cache map update: status not 200", all.getStatus(), path);
            Document list = all.getDocument();
            // assumes consistency in service layer payloads - possible could configure this rather than hard code?
            List<Node> nodes = list.selectNodes("authority-ref-list/*");
            for (Node node : nodes) {
                if (node.getName().equals("authority-ref-item")) {
                    if (!(node instanceof Element))
                        continue;
                    if (((Element) node).hasContent()) {
                        String key = ((Element) node).selectSingleNode("sourceField").getText();
                        String refname = ((Element) node).selectSingleNode("refName").getText();
                        String itemDisplayName = ((Element) node).selectSingleNode("itemDisplayName").getText();
                        String uri = "";
                        if (null != ((Element) node).selectSingleNode("uri")) {
                            // seems to be missing sometimes
                            uri = ((Element) node).selectSingleNode("uri").getText();
                        }
                        String fieldName = key;
                        if (key.split(":").length > 1) {
                            fieldName = key.split(":")[1];
                        }
                        Field fieldinstance = null;
                        if (r.getFieldFullList(fieldName) instanceof Repeat) {
                            Repeat rp = (Repeat) r.getFieldFullList(fieldName);
                            for (FieldSet a : rp.getChildren("GET")) {
                                if (a instanceof Field && a.hasAutocompleteInstance()) {
                                    fieldinstance = (Field) a;
                                }
                            }
                        } else {
                            fieldinstance = (Field) r.getFieldFullList(fieldName);
                        }
                        if (fieldinstance != null) {
                            JSONObject data = new JSONObject();
                            data.put("sourceField", key);
                            data.put("itemDisplayName", itemDisplayName);
                            data.put("refname", refname);
                            data.put("uri", uri);
                            // JSONObject data=miniForURI(storage,creds,cache,refname,null,restrictions);
                            /*
								if(!data.has("refid")){//incase of permissions errors try our best
									data.put("refid",refname);
									if(data.has("displayName")){
										String itemDisplayName=((Element)node).selectSingleNode("itemDisplayName").getText();
										String temp = data.getString("displayName");
										data.remove("displayName");
										data.put(temp, itemDisplayName);
									}
								}
								*/
                            data.put("sourceFieldselector", fieldinstance.getSelector());
                            data.put("sourceFieldName", fieldName);
                            data.put("sourceFieldType", r.getID());
                            if (listitems.has(key)) {
                                JSONArray temp = listitems.getJSONArray(key).put(data);
                                listitems.put(key, temp);
                            } else {
                                JSONArray temp = new JSONArray();
                                temp.put(data);
                                listitems.put(key, temp);
                            }
                        }
                    }
                } else {
                    pagination.put(node.getName(), node.getText());
                }
            }
        }
        out.put("pagination", pagination);
        out.put("listItems", listitems);
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection problem" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : Node(org.dom4j.Node) Element(org.dom4j.Element) JSONArray(org.json.JSONArray) Repeat(org.collectionspace.chain.csp.schema.Repeat) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)28 Document (org.dom4j.Document)24 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)20 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)18 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)17 Node (org.dom4j.Node)16 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)8 List (java.util.List)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)4 Field (org.collectionspace.chain.csp.schema.Field)3 JSONArray (org.json.JSONArray)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 ReturnUnknown (org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)2