use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class RecordTraverser method store_get.
private void store_get(Storage storage, UIRequest request, String path) throws UIException {
JSONObject outputJSON = new JSONObject();
try {
String[] bits = path.split("/");
String token = bits[0];
Integer indexvalue = Integer.valueOf(bits[1]);
String key = UISession.SEARCHTRAVERSER + "" + token;
if (request.getSession().getValue(key) instanceof JSONObject) {
JSONObject alldata = (JSONObject) request.getSession().getValue(key);
JSONObject pagination = new JSONObject();
if (alldata.has("pagination")) {
pagination = alldata.getJSONObject("pagination");
}
JSONArray data = alldata.getJSONArray("results");
Integer pgSz = alldata.getInt("pageSize");
Integer pageNm = alldata.getInt("pageNum");
Integer total = alldata.getInt("total");
Integer offset = pgSz * pageNm;
Integer numInstances = alldata.getInt("numInstances");
String base = alldata.getString("record");
String instance = alldata.getString("instance");
JSONObject restriction = alldata.getJSONObject("restriction");
// only works 100% for records - auths is a problem...
// multiplying by numInstance isn't quite enough...
// Integer relativeindexvalue = indexvalue - offset ;
Integer prevpageNm = 0;
Integer prevval = indexvalue - 1;
if (prevval >= 0 && prevval <= total) {
JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, numInstances, base, instance, restriction, prevpageNm, prevval);
outputJSON.put("previous", item);
}
Integer postpageNm = 0;
Integer postval = indexvalue + 1;
if (postval < total && postval >= 0) {
JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, numInstances, base, instance, restriction, postpageNm, postval);
outputJSON.put("next", item);
}
Integer currpageNm = 0;
if (indexvalue < total && indexvalue >= 0) {
JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, currpageNm, base, instance, restriction, prevpageNm, indexvalue);
outputJSON.put("current", item);
}
outputJSON.put("index", indexvalue);
outputJSON.put("token", token);
outputJSON.put("total", total);
} else {
outputJSON.put("error", "Cannot find the traverser token");
}
} catch (JSONException e) {
throw new UIException("Error with the traverser data", e);
} catch (ExistException e) {
throw new UIException("Error with the traverser data", e);
} catch (UnimplementedException e) {
throw new UIException("Error with the traverser data", e);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
}
request.sendJSONResponse(outputJSON);
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class StructuredDateParser method run.
@Override
public void run(Object in, String[] tail) throws UIException {
UIRequest request = ((Request) in).getUIRequest();
String displayDate = request.getRequestArgument("displayDate");
JSONObject output = new JSONObject();
StructuredDateInternal structuredDate = null;
StructuredDateFormatException formatException = null;
try {
structuredDate = StructuredDateInternal.parse(displayDate);
} catch (StructuredDateFormatException e) {
formatException = e;
}
try {
if (formatException != null) {
// The convention in app layer error responses appears to be to
// send a boolean isError, and an array of error messages.
output.put("isError", true);
output.put("messages", new String[] { "Unrecognized date format", formatException.getMessage() });
}
if (structuredDate != null) {
String tenantDomain = request.getTenant();
output.put("structuredDate", structuredDateToJSON(tenantDomain, structuredDate));
}
} catch (JSONException e) {
throw new UIException("Error building JSON", e);
}
request.sendJSONResponse(output);
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class VocabRedirector method redirect.
private void redirect(CSPRequestCache cache, Storage storage, UIRequest request, String[] tail) throws UIException {
try {
JSONArray out = new JSONArray();
String vocabConstraint = request.getRequestArgument(CONSTRAIN_VOCAB_PARAM);
out = pathForAll(tail[0], vocabConstraint);
request.sendJSONResponse(out);
int cacheMaxAgeSeconds = adminData.getAutocompleteListCacheAge();
if (cacheMaxAgeSeconds > 0) {
request.setCacheMaxAgeSeconds(cacheMaxAgeSeconds);
}
} catch (JSONException e) {
throw new UIException("JSON building failed", e);
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class WebAutoComplete method autocomplete.
private void autocomplete(CSPRequestCache cache, Storage storage, UIRequest request) throws UIException {
try {
String[] path = request.getPrincipalPath();
// Last path element is the field on which user is querying.
String fieldName = path[path.length - 1];
JSONArray out = new JSONArray();
boolean hasHierarchy = r.hasHierarchyUsed("screen");
boolean isHierarchyAutoComplete = false;
if (hasHierarchy) {
// Configures the hierarchy section.
Structure s = r.getStructure("screen");
if (s.hasOption(fieldName)) {
// This is one of the hierarchy fields
isHierarchyAutoComplete = true;
}
}
if (r.isType("authority") || !isHierarchyAutoComplete) {
out = doAuthorityAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(CONSTRAIN_VOCAB_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
} else if (isHierarchyAutoComplete) {
out = doRecordAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
} else {
throw new ConfigException("WebAutoComplete called for record that does not support autocomplete!: " + r.getID());
}
request.sendJSONResponse(out);
} catch (JSONException e) {
throw new UIException("JSONException during autocompletion", e);
} catch (ExistException e) {
throw new UIException("ExistException during autocompletion", e);
} catch (UnimplementedException e) {
throw new UIException("UnimplementedException during autocompletion", e);
} catch (ConfigException e) {
throw new UIException("ConfigException during autocompletion", e);
} catch (UnderlyingStorageException x) {
throw new UIException("UnderlyingStorageException during autocompletion" + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class WebLoginStatus method testlogin.
public void testlogin(Request in) throws UIException {
UIRequest request = in.getUIRequest();
try {
Storage storage = in.getStorage();
JSONObject output = new JSONObject();
UISession uiSession = request.getSession();
if (uiSession != null && uiSession.getValue(UISession.USERID) != null) {
if (uiSession.getValue(UISession.USERID).equals("")) {
output.put("login", false);
} else {
JSONObject perms = null;
// See if there is a cache of the permissions for this user and tenant.
String userId = (String) uiSession.getValue(UISession.USERID);
String tenantId = (String) uiSession.getValue(UISession.TENANT);
perms = findPermsInCache(userId, tenantId);
boolean fFoundInCache;
if (perms != null) {
fFoundInCache = true;
} else {
fFoundInCache = false;
perms = getPermissions(storage);
}
if (perms.has("permissions")) {
// Will only slow down edge case of user with no roles.
if (!fFoundInCache) {
addPermsToCache(userId, tenantId, perms);
}
output.put("permissions", perms.getJSONObject("permissions"));
output.put("csid", perms.getString("csid"));
output.put("screenName", perms.getString("screenName"));
output.put("userId", perms.getString("userId"));
output.put("login", true);
int maxInterval = 0;
UIRequest uir = in.getUIRequest();
if (uir != null) {
HttpSession httpSession = request.getHttpSession();
if (httpSession != null) {
maxInterval = httpSession.getMaxInactiveInterval();
}
}
// Need to consider the shorter of session timeout and cookie expiry.
// cookie life is in minutes, so convert to seconds.
int cookieLife = 60 * spec.getAdminData().getCookieLife();
if (maxInterval == 0 || maxInterval >= cookieLife) {
maxInterval = cookieLife;
}
output.put("maxInactive", maxInterval);
} else {
output.put("login", false);
output.put("message", "no roles associated with this user");
}
}
} else {
output.put("login", false);
}
request.sendJSONResponse(output);
} catch (JSONException x) {
throw new UIException("Failed to parse json: " + x.getMessage(), x);
} catch (ExistException x) {
// failed login test
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());
}
}
Aggregations