Search in sources :

Example 66 with InternalServerErrorException

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;
}
Also used : SSOToken(com.iplanet.sso.SSOToken) UriRouterContext(org.forgerock.http.routing.UriRouterContext) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException)

Example 67 with InternalServerErrorException

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();
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) JsonValue(org.forgerock.json.JsonValue) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ScriptResponse(org.forgerock.openam.scripting.rest.batch.helpers.ScriptResponse)

Example 68 with InternalServerErrorException

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;
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) Evaluator(com.sun.identity.entitlement.Evaluator) Entitlement(com.sun.identity.entitlement.Entitlement) Subject(javax.security.auth.Subject)

Example 69 with InternalServerErrorException

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();
    }
}
Also used : ServerException(org.forgerock.openam.sm.datalayer.store.ServerException) UmaPendingRequest(org.forgerock.openam.sm.datalayer.impl.uma.UmaPendingRequest) NotFoundException(org.forgerock.openam.sm.datalayer.store.NotFoundException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException)

Example 70 with InternalServerErrorException

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();
            }
        }
    };
}
Also used : ServerException(org.forgerock.openam.sm.datalayer.store.ServerException) MessagingException(javax.mail.MessagingException) NotFoundException(org.forgerock.openam.sm.datalayer.store.NotFoundException) AsyncFunction(org.forgerock.util.AsyncFunction) AMIdentity(com.sun.identity.idm.AMIdentity) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaConstants.UmaPolicy)

Aggregations

InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)70 SSOException (com.iplanet.sso.SSOException)39 JsonValue (org.forgerock.json.JsonValue)33 SMSException (com.sun.identity.sm.SMSException)29 BadRequestException (org.forgerock.json.resource.BadRequestException)27 NotFoundException (org.forgerock.json.resource.NotFoundException)25 ResourceException (org.forgerock.json.resource.ResourceException)24 SSOToken (com.iplanet.sso.SSOToken)19 IdRepoException (com.sun.identity.idm.IdRepoException)18 Set (java.util.Set)15 ResourceResponse (org.forgerock.json.resource.ResourceResponse)15 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)14 AMIdentity (com.sun.identity.idm.AMIdentity)13 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11 ForbiddenException (org.forgerock.json.resource.ForbiddenException)11 ServiceConfig (com.sun.identity.sm.ServiceConfig)10 NotSupportedException (org.forgerock.json.resource.NotSupportedException)10 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)10 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)9