Search in sources :

Example 41 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class AuthorizationStorage method getPaths.

/**
 * Returns a list of csid's from a certain type of record
 */
@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Document list = null;
        List<String> out = new ArrayList<String>();
        String path = getRestrictedPath(r.getServicesURL(), restrictions, "res", "", false, "");
        ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
        if (all.getStatus() != 200) {
            throw new ConnectionException("Bad request in Authorization Storage: status not 200", all.getStatus(), path);
        }
        list = all.getDocument();
        List<Node> objects = list.selectNodes(r.getServicesListPath());
        for (Node object : objects) {
            String csid = object.valueOf("@csid");
            out.add(csid);
            setGleanedValue(cache, r.getServicesURL() + "/" + csid, view_map.get(object.getName()), object.getText());
        }
        return out.toArray(new String[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 42 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class AuthorizationStorage method retrieveJSON.

@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String[] parts = filePath.split("/");
        if (parts.length > 2) {
            Spec s = r.getSpec();
            // {csid}/userrole/{csid}
            if (s.hasRecordByWebUrl(parts[1])) {
                String path = s.getRecordByWebUrl(parts[1]).getServicesURL();
                int len = parts.length - 1;
                for (int i = 0; i < len; i++) {
                    path = path.replace("*", parts[i]);
                    i++;
                }
                filePath = path + "/" + parts[len];
                return simpleRetrieveJSONFullPath(creds, cache, filePath, s.getRecordByWebUrl(parts[1]).getRecord());
            } else {
                // {csid}/refobj/bob
                String extra = "";
                if (parts.length == 3) {
                    extra = parts[2];
                }
                return viewRetrieveJSON(root, creds, cache, parts[0], parts[1], extra, restrictions);
            }
        } else {
            return simpleRetrieveJSON(creds, cache, filePath);
        }
    } catch (JSONException x) {
        throw new UnderlyingStorageException("Error building JSON", x);
    }
}
Also used : JSONException(org.json.JSONException) Spec(org.collectionspace.chain.csp.schema.Spec) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 43 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class AuthorizationStorage method simpleRetrieveJSONFullPath.

public JSONObject simpleRetrieveJSONFullPath(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        JSONObject out = new JSONObject();
        if (thisr.isMultipart()) {
            ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
            for (String section : thisr.getServicesRecordPathKeys()) {
                String path = thisr.getServicesRecordPath(section);
                String[] parts = path.split(":", 2);
                convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, "", "");
            }
        } else {
            ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300)) {
                if (doc.getStatus() == 401) {
                    throw new UnauthorizedException("Username and/or password are invalid.", doc.getStatus(), filePath);
                } else if (doc.getStatus() == 409) {
                    throw new ConflictException("Conflict with request. The user's tenant may be disabled. Contact your CollectionSpace administrator.", doc.getStatus(), filePath);
                } else {
                    String status = Integer.toString(doc.getStatus());
                    throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
                }
            }
            convertToJson(out, doc.getDocument(), thisr, "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 : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) JSONObject(org.json.JSONObject) ConflictException(org.collectionspace.csp.api.persistence.ConflictException) UnauthorizedException(org.collectionspace.csp.api.persistence.UnauthorizedException) JSONException(org.json.JSONException) 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 44 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class AuthorizationStorage method getPathsJSON.

/**
 * Gets a list of csids of a certain type of record together with the pagination info
 * permissions might need to break the mold tho.
 */
@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        JSONObject out = new JSONObject();
        // PLS: why on earth would reports be routed through AuthStorage?!?!
        if (r.getID().equals("reports") || r.getID().equals("batch")) {
            String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), "", false, "");
            JSONObject data = getListView(creds, cache, path, r.getServicesListPath(), "csid", false, r);
            return data;
        }
        Document list = null;
        String prefix = null;
        Boolean queryadded = false;
        JSONObject pagination = new JSONObject();
        List<String> listitems = new ArrayList<String>();
        String serviceurl = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), "", false, "");
        ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, serviceurl, null, creds, cache);
        if (all.getStatus() != 200) {
            throw new ConnectionException("Bad request in Authorization Storage getPathsJSON: status not 200", all.getStatus(), serviceurl);
        }
        list = all.getDocument();
        String listItemPath = r.getServicesListPath();
        String[] listItemPathElements = listItemPath.split("/");
        if (listItemPathElements.length != 2) {
            throw new RuntimeException("Illegal list item path " + listItemPath);
        }
        String listNodeName = listItemPathElements[0];
        String listItemNodeName = listItemPathElements[1];
        String listNodeChildrenSelector = "/" + listNodeName + "/*";
        List<Node> nodes = list.selectNodes(listNodeChildrenSelector);
        for (Node node : nodes) {
            if (listItemNodeName.equals(node.getName())) {
                String csid = node.valueOf("@csid");
                listitems.add(csid);
                String fullPath = r.getServicesURL() + "/" + csid;
                if (view_map.get(node.getName()) != null) {
                    setGleanedValue(cache, fullPath, view_map.get(node.getName()), node.getText());
                }
                // Now glean the values we need from the child nodes
                List<Node> fields = node.selectNodes("*");
                for (Node field : fields) {
                    // Is this a field we want?
                    String json_name = view_map.get(field.getName());
                    if (json_name != null) {
                        String value = field.getText();
                        setGleanedValue(cache, r.getServicesURL() + "/" + csid, json_name, value);
                    }
                }
            } else {
                pagination.put(node.getName(), node.getText());
            }
        }
        out.put("pagination", pagination);
        out.put("listItems", listitems.toArray(new String[0]));
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) JSONObject(org.json.JSONObject) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 45 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class AuthorizationStorage method deleteJSON.

/**
 * Remove an object in the Service Layer.
 */
@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        int status = 0;
        if (r.hasDeleteMethod()) {
            // get all data to post back
            // hopefully the filepath we have is the filepath to get the data from
            Document doc = null;
            try {
                doc = simpleRetrieveXML(creds, cache, filePath + "/");
                String nodepath = "/" + r.getServicesListPath() + "/*";
                List<Node> nodes = doc.selectNodes(nodepath);
                if (nodes.size() > 0) {
                    status = 201;
                    status = conn.getNone(RequestMethod.DELETE, filePath, null, creds, cache);
                // post it back to delete it
                /*
						log.info("TRYING TO DELETE ACCOUNTROLE");
						log.info(doc.asXML());
						log.info(filePath+"?_method=delete");
						ReturnedURL url = null;
						url = conn.getURL(RequestMethod.POST, filePath+"?_method=delete", doc, creds, cache);
						status = url.getStatus();
						//status=conn.getNone(RequestMethod.POST,r.getServicesURL()+"/"+filePath+"?_method=delete",doc,creds,cache);
						 	
	*/
                } else {
                    // if we didn't need to delete it because there was nothing there then everything was good
                    status = 201;
                }
            } catch (ExistException ex) {
                // ignore as nothign to delete if nothing exists
                status = 201;
            }
        } else {
            status = conn.getNone(RequestMethod.DELETE, r.getServicesURL() + "/" + filePath, null, creds, cache);
        }
        if (// XXX CSPACE-73, should be 404
        status > 299 || status < 200)
            throw new UnderlyingStorageException("Service layer exception status=" + status, status, r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    }
}
Also used : Node(org.dom4j.Node) 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) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)108 JSONObject (org.json.JSONObject)75 JSONException (org.json.JSONException)73 ExistException (org.collectionspace.csp.api.persistence.ExistException)57 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)55 UIException (org.collectionspace.csp.api.ui.UIException)40 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)39 JSONArray (org.json.JSONArray)34 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)30 Document (org.dom4j.Document)29 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)23 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 Field (org.collectionspace.chain.csp.schema.Field)14 Record (org.collectionspace.chain.csp.schema.Record)14 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Node (org.dom4j.Node)10 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)9 IOException (java.io.IOException)7