Search in sources :

Example 16 with UnderlyingStorageException

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

the class UserDetailsCreateUpdate method store_set.

private void store_set(Storage storage, UIRequest request, String path) throws UIException {
    JSONObject data = null;
    data = request.getJSONBody();
    boolean notfailed = true;
    String msg = "";
    try {
        boolean currentUserPasswordChange = false;
        String newPassword = null;
        boolean absorbedSvcsError = false;
        if (create) {
            path = sendJSON(storage, null, data);
        // assign to default role.
        } else {
            // Check for password update. If doing that, absorb 403 errors and redirect
            // as though we are doing a logout.
            JSONObject fields = data.optJSONObject("fields");
            if (fields != null && fields.has(PASSWORD_FIELD)) {
                String passwd = fields.getString(PASSWORD_FIELD);
                if (passwd != null) {
                    if (passwd.isEmpty()) {
                        // Preclude removl of a password
                        fields.remove(PASSWORD_FIELD);
                    } else {
                        String editedUserId = fields.getString(USER_ID_FIELD);
                        UISession session = request.getSession();
                        if (session != null) {
                            Object currentUserId = session.getValue(UISession.USERID);
                            if (currentUserId != null && currentUserId.equals(editedUserId)) {
                                newPassword = passwd;
                                currentUserPasswordChange = true;
                            }
                        }
                    }
                }
            }
            path = sendJSON(storage, path, data);
            // credentials
            if (currentUserPasswordChange) {
                request.getSession().setValue(UISession.PASSWORD, newPassword);
            }
        }
        if (path == null) {
            throw new UIException("Insufficient data for create (no fields?)");
        }
        data.put("csid", path);
        try {
            assignRole(storage, path, data);
        } catch (UnderlyingStorageException usex) {
            Integer status = usex.getStatus();
            if (status != null && (status == HttpStatus.SC_FORBIDDEN || status == HttpStatus.SC_UNAUTHORIZED)) {
                absorbedSvcsError = true;
                msg = "Cannot update roles for this account.";
                log.warn("UserDetailsCreateUpdate changing roles, and absorbing error returned: " + usex.getStatus());
            } else {
                // Propagate
                throw usex;
            }
        }
        boolean isError = !notfailed;
        data.put("isError", isError);
        JSONObject messages = new JSONObject();
        messages.put("message", msg);
        messages.put("severity", "info");
        JSONArray arr = new JSONArray();
        arr.put(messages);
        data.put("messages", arr);
        // Elide the value of the password field before returning a response
        data.optJSONObject("fields").remove(PASSWORD_FIELD);
        request.sendJSONResponse(data);
        request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
        if (create && notfailed)
            request.setSecondaryRedirectPath(new String[] { url_base, path });
    } catch (JSONException x) {
        throw new UIException("Failed to parse json: ", x);
    } catch (ExistException x) {
        throw new UIException("Existence exception: ", x);
    } catch (UnimplementedException x) {
        throw new UIException("Unimplemented exception: ", x);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : JSONObject(org.json.JSONObject) UISession(org.collectionspace.csp.api.ui.UISession) JSONArray(org.json.JSONArray) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 17 with UnderlyingStorageException

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

the class UserDetailsRead method getJSON.

/* Wrapper exists to decomplexify exceptions */
private JSONObject getJSON(Storage storage, String csid) throws UIException {
    JSONObject out = new JSONObject();
    try {
        if (record_type) {
            JSONObject fields = storage.retrieveJSON(base + "/" + csid, new JSONObject());
            // XXX remove this, subject to UI team approval?
            fields.put("csid", csid);
            JSONObject roles = storage.retrieveJSON(base + "/" + csid + "/" + "userrole", new JSONObject());
            JSONArray allroles = Generic.getRoles(storage, roles);
            fields.put("role", allroles);
            out.put("fields", fields);
            out.put("isError", false);
            JSONObject messages = new JSONObject();
            messages.put("message", "");
            messages.put("severity", "info");
            JSONArray arr = new JSONArray();
            arr.put(messages);
            out.put("messages", arr);
            out.put("relations", new JSONArray());
        } else {
            out = storage.retrieveJSON(base + "/" + csid, new JSONObject());
        }
    } catch (ExistException e) {
        throw new UIException("JSON Not found ", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        return uiexception.getJSON();
    } catch (JSONException e) {
        throw new UIException("Could not create JSON" + e, e);
    }
    if (out == null) {
        throw new UIException("No JSON Found");
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 18 with UnderlyingStorageException

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

the class UserDetailsReset method getcsID.

private JSONObject getcsID(Storage storage, String emailparam) throws UIException {
    JSONObject restriction = new JSONObject();
    JSONObject failedJSON = new JSONObject();
    try {
        failedJSON.put("isError", true);
        if (emailparam != null && emailparam != "") {
            restriction.put("email", emailparam);
            restriction.put("pageSize", "40");
            int resultsize = 1;
            int pagenum = 0;
            String checkpagination = "";
            while (resultsize > 0) {
                restriction.put("pageNum", pagenum);
                /* XXX need to force it to only do an exact match */
                JSONObject data = storage.getPathsJSON(base, restriction);
                String[] paths = (String[]) data.get("listItems");
                pagenum++;
                if (paths.length == 0 || checkpagination.equals(paths[0])) {
                    resultsize = 0;
                // testing whether we have actually returned the same page or the next page - all csid returned should be unique
                } else {
                    checkpagination = paths[0];
                    /* make sure it is an exact match */
                    for (int i = 0; i < paths.length; i++) {
                        // GET full details
                        JSONObject fields = storage.retrieveJSON(base + "/" + paths[i], new JSONObject());
                        String emailtest = fields.getString("email");
                        if (emailtest.equals(emailparam)) {
                            JSONObject outputJSON = new JSONObject();
                            outputJSON.put("fields", fields);
                            outputJSON.put("isError", false);
                            outputJSON.put("csid", paths[i]);
                            return outputJSON;
                        }
                    }
                }
            }
            failedJSON.put("message", "Could not find a user with email " + emailparam);
        } else {
            failedJSON.put("message", "No email specified ");
        }
        return failedJSON;
    } catch (JSONException e) {
        throw new UIException("JSONException during search on email address", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during search on email address", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during search on email address", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        return uiexception.getJSON();
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 19 with UnderlyingStorageException

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

the class UserRolesRead method getJSON.

/* Wrapper exists to decomplexify exceptions */
private JSONObject getJSON(Storage storage, String csid) throws UIException {
    JSONObject out = new JSONObject();
    try {
        if (!record_type) {
            JSONObject fields = storage.retrieveJSON("base+/" + csid, new JSONObject());
            // XXX remove this, subject to UI team approval?
            fields.put("csid", csid);
            out.put("fields", fields);
            out.put("isError", false);
            JSONObject messages = new JSONObject();
            messages.put("message", "");
            messages.put("severity", "info");
            JSONArray arr = new JSONArray();
            arr.put(messages);
            out.put("messages", arr);
            out.put("relations", new JSONArray());
        } else {
            out = storage.retrieveJSON(this.sub_base + "/" + csid, new JSONObject());
        }
    } catch (ExistException e) {
        throw new UIException("ExistException " + e, e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        return uiexception.getJSON();
    } catch (JSONException e) {
        throw new UIException("Could not create JSON" + e, e);
    }
    if (out == null) {
        throw new UIException("No JSON Found");
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 20 with UnderlyingStorageException

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

the class SplittingStorage method getPathsJSON.

/**
 * For each type of storage, this function will get the paths and pagination information, this will be brought together into one object
 */
@Override
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restriction) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        // XXX THIS SHOULD BE LOOKED AT AND CHANGED !!!
        JSONObject out = new JSONObject();
        JSONObject pagination = new JSONObject();
        JSONObject moredata = new JSONObject();
        boolean passed = false;
        List<String[]> separatelists = new ArrayList<String[]>();
        String[] parts = split(rootPath, true);
        if ("".equals(parts[0])) {
            return out.put("listItems", children.keySet().toArray(new String[0]));
        } else {
            List<String> list = new ArrayList<String>();
            for (Map.Entry<String, ContextualisedStorage> e : children.entrySet()) {
                if (e.getKey().equals(parts[0])) {
                    ContextualisedStorage storage = e.getValue();
                    JSONObject data = storage.getPathsJSON(root, creds, cache, parts[1], restriction);
                    if (data == null) {
                        data = new JSONObject();
                        data.put("listItems", new String[0]);
                    }
                    if (data.has("moredata")) {
                        moredata = data.getJSONObject("moredata");
                    }
                    JSONObject paging = new JSONObject();
                    if (data.has("pagination")) {
                        paging = data.getJSONObject("pagination");
                    }
                    if (!passed) {
                        pagination = paging;
                        passed = true;
                    } else {
                        pagination.put("totalItems", pagination.getInt("totalItems") + paging.getInt("totalItems"));
                        int pageSize = pagination.getInt("pageSize");
                        int totalinpage = pagination.getInt("itemsInPage") + paging.getInt("itemsInPage");
                        if (totalinpage > pageSize) {
                            pagination.put("itemsInPage", pageSize);
                        } else {
                            pagination.put("itemsInPage", totalinpage);
                        }
                    }
                    // create one merged list
                    String[] paths = (String[]) data.get("listItems");
                    if (paths == null) {
                        continue;
                    }
                    for (String s : paths) {
                        list.add(s);
                    }
                    // keep the separate lists in a field
                    separatelists.add(paths);
                }
            }
            pagination.put("separatelists", separatelists);
            out.put("moredata", moredata);
            out.put("pagination", pagination);
            out.put("listItems", list.toArray(new String[0]));
            return out;
        }
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Error parsing JSON");
    }
}
Also used : ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

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