use of org.collectionspace.csp.api.persistence.ExistException 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;
}
use of org.collectionspace.csp.api.persistence.ExistException 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();
}
}
use of org.collectionspace.csp.api.persistence.ExistException 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;
}
use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.
the class StubJSONStore method fileFromPath.
/**
* Generate a file from a path.
*
* @param path the path
* @return the file
* @throws UnderlyingStorageException
* @throws ExistException
*/
private File fileFromPath(String path) throws UnderlyingStorageException, ExistException {
if (path.startsWith("/"))
path = path.substring(1);
String[] parts = path.split("/");
if (parts.length == 1)
throw new ExistException("Cannot store in root directory");
List<String> fullpath = new ArrayList<String>();
for (int i = 0; i < parts.length; i++) {
if (// Repeated slash, ignore
"".equals(parts[i]))
continue;
fullpath.add(parts[i]);
}
String file = fullpath.remove(fullpath.size() - 1);
File root = new File(store_root);
for (String dir : fullpath) {
dir = dir.replaceAll("[^A-Za-z0-9_,.-]", "");
root = new File(root, dir);
if (!root.exists()) {
root.mkdir();
}
}
file = file.replaceAll("[^A-Za-z0-9_,.-]", "");
return new File(root, file + ".json");
}
use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.
the class StubJSONStore method retrieveJSON.
/* (non-Javadoc)
* @see org.collectionspace.JSONStore#retrieveJson(java.lang.String)
*/
public JSONObject retrieveJSON(String filePath, JSONObject restrictions) throws ExistException, UnderlyingStorageException, UnimplementedException {
// XXX hack: support views properly
String XXXCHOP = "/view";
String XXXCHOP2 = "/refs";
if (filePath.endsWith(XXXCHOP))
filePath = filePath.substring(0, filePath.length() - XXXCHOP.length());
if (filePath.endsWith(XXXCHOP2))
return new JSONObject();
if (idRequest(filePath))
return id.retrieveJSON(filePath, restrictions);
File jsonFile = fileFromPath(filePath);
if (!jsonFile.exists()) {
throw new ExistException("No such file: " + filePath);
}
try {
FileReader r = new FileReader(jsonFile);
String data = IOUtils.toString(r);
r.close();
JSONObject jsonObject = new JSONObject(data);
return jsonObject;
} catch (IOException ioe) {
// XXX
return new JSONObject();
} catch (JSONException je) {
// XXX
return new JSONObject();
}
}
Aggregations