use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class RealmContextFilter method evaluate.
private Context evaluate(Context context, String hostname, List<String> requestUri, List<String> overrideRealmParameter) throws ResourceException {
if (!coreWrapper.isValidFQDN(hostname)) {
throw new BadRequestException("FQDN \"" + hostname + "\" is not valid.");
}
SSOToken adminToken = coreWrapper.getAdminToken();
String dnsAliasRealm = RealmUtils.cleanRealm(getRealmFromAlias(adminToken, hostname));
StringBuilder matchedUriBuilder = new StringBuilder();
String currentRealm = dnsAliasRealm;
int consumedElementsCount = 0;
for (String element : requestUri) {
try {
String subrealm = RealmUtils.cleanRealm(element);
currentRealm = resolveRealm(adminToken, currentRealm, subrealm);
matchedUriBuilder.append(subrealm);
consumedElementsCount++;
} catch (InternalServerErrorException ignored) {
break;
}
}
String overrideRealm = null;
try {
if (overrideRealmParameter != null && !overrideRealmParameter.isEmpty()) {
overrideRealm = resolveRealm(adminToken, "/", RealmUtils.cleanRealm(overrideRealmParameter.get(0)));
}
} catch (InternalServerErrorException e) {
throw new BadRequestException("Invalid realm, " + overrideRealmParameter.get(0), e);
}
List<String> remainingUri = requestUri.subList(consumedElementsCount, requestUri.size());
String matchedUri = matchedUriBuilder.length() > 1 ? matchedUriBuilder.substring(1) : matchedUriBuilder.toString();
RealmContext realmContext = new RealmContext(new UriRouterContext(context, matchedUri, Paths.joinPath(remainingUri), Collections.<String, String>emptyMap()));
realmContext.setDnsAlias(hostname, dnsAliasRealm);
realmContext.setSubRealm(matchedUri, RealmUtils.cleanRealm(currentRealm.substring(dnsAliasRealm.length())));
realmContext.setOverrideRealm(overrideRealm);
return realmContext;
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class BatchResource method actionCollection.
@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context serverContext, ActionRequest actionRequest) {
if (!actionRequest.getAction().equals(BATCH)) {
final String msg = "Action '" + actionRequest.getAction() + "' not implemented for this resource";
final NotSupportedException exception = new NotSupportedException(msg);
debug.error(msg, exception);
return exception.asPromise();
}
String scriptId = null;
try {
JsonValue scriptIdValue = actionRequest.getContent().get(SCRIPT_ID);
if (scriptIdValue == null) {
if (debug.errorEnabled()) {
debug.error("BatchResource :: actionCollection - ScriptId null. Default scripts not implemented.");
}
return new BadRequestException().asPromise();
} else {
scriptId = scriptIdValue.asString();
}
final JsonValue requests = actionRequest.getContent().get(REQUESTS);
final String realm = getRealm(serverContext);
final ScriptConfiguration scriptConfig = scriptingServiceFactory.create(SubjectUtils.createSuperAdminSubject(), realm).get(scriptId);
final ScriptObject script = new ScriptObject(scriptConfig.getName(), scriptConfig.getScript(), scriptConfig.getLanguage());
final ScriptResponse response = new ScriptResponse();
final Bindings bindings = new SimpleBindings();
bindings.put(PAYLOAD, requests);
bindings.put(CONTEXT, serverContext);
bindings.put(LOGGER, debug);
bindings.put(REQUESTER, requester);
bindings.put(RESPONSE, response);
return newResultPromise(newActionResponse((JsonValue) scriptEvaluator.evaluateScript(script, bindings)));
} catch (ScriptException e) {
debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
return exceptionMappingHandler.handleError(serverContext, actionRequest, e).asPromise();
} catch (javax.script.ScriptException e) {
debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
return new InternalServerErrorException().asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class ResourceSetService method isSharedWith.
/**
* Checks whether a ResourceSet is accessible by a user.
* @param resourceSet The resource set to check.
* @param resourceUserId The id of the user to check.
* @param realm The realm to check in.
* @return @code{true} if the user can access that ResourceSet.
*/
public boolean isSharedWith(ResourceSetDescription resourceSet, String resourceUserId, String realm) throws InternalServerErrorException {
Subject subject = createSubject(resourceUserId, realm);
try {
Evaluator evaluator = umaProviderSettingsFactory.get(realm).getPolicyEvaluator(subject, resourceSet.getClientId().toLowerCase());
String sharedResourceName = "uma://" + resourceSet.getId();
List<Entitlement> entitlements = evaluator.evaluate(realm, subject, sharedResourceName, null, false);
if (!entitlements.isEmpty() && !entitlements.iterator().next().getActionValues().isEmpty()) {
return true;
}
} catch (EntitlementException | NotFoundException e) {
throw new InternalServerErrorException(e);
}
return false;
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class PendingRequestsService method approvePendingRequest.
/**
* Approves the pending request with the specified {@literal id}.
*
* @param context The request context.
* @param id The pending request id.
* @param content The content of the approval request.
* @param realm The current realm. @return {@code Promise} which is completed successfully or
* failed with a {@code ResourceException}.
*/
public Promise<Void, ResourceException> approvePendingRequest(Context context, String id, JsonValue content, String realm) {
try {
final UmaPendingRequest request = store.read(id);
Collection<String> scopes = getScopes(request, content);
return createUmaPolicy(context, request, scopes).thenAsync(approvePendingRequest(request, scopes, id, realm));
} catch (NotFoundException e) {
return new org.forgerock.json.resource.NotFoundException("Pending request, " + id + ", not found", e).asPromise();
} catch (ServerException e) {
return new InternalServerErrorException("Failed to mark pending request, " + id + ", as approved", e).asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class PendingRequestsService method approvePendingRequest.
private AsyncFunction<UmaPolicy, Void, ResourceException> approvePendingRequest(final UmaPendingRequest request, final Collection<String> scopes, final String id, final String realm) {
return new AsyncFunction<UmaPolicy, Void, ResourceException>() {
@Override
public Promise<Void, ResourceException> apply(UmaPolicy value) {
try {
if (isEmailRequestingPartyOnPendingRequestApprovalEnabled(realm)) {
Pair<String, String> template = pendingRequestEmailTemplate.getApprovalTemplate(request.getRequestingPartyId(), realm);
try {
emailService.email(realm, request.getRequestingPartyId(), template.getFirst(), MessageFormat.format(template.getSecond(), request.getResourceOwnerId(), request.getResourceSetName(), pendingRequestEmailTemplate.buildScopeString(scopes, request.getRequestingPartyId(), realm)));
} catch (MessagingException e) {
debug.warning("Pending Request Approval email could not be sent", e);
}
}
store.delete(id);
AMIdentity resourceOwner = coreWrapper.getIdentity(request.getResourceOwnerId(), realm);
auditLogger.log(request.getResourceSetId(), request.getResourceSetName(), resourceOwner, UmaAuditType.REQUEST_APPROVED, request.getRequestingPartyId());
return newResultPromise(null);
} catch (NotFoundException e) {
return new org.forgerock.json.resource.NotFoundException("Pending request, " + id + ", not found", e).asPromise();
} catch (ServerException e) {
return new InternalServerErrorException("Failed to mark pending request, " + id + ", as approved", e).asPromise();
}
}
};
}
Aggregations