Search in sources :

Example 76 with BadRequestException

use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.

the class RealmContextFilterTest method verifyInvalidRealmResponse.

private void verifyInvalidRealmResponse(Response response, String expectedInvalidRealm) throws IOException {
    assertThat(response.getStatus()).isSameAs(Status.BAD_REQUEST);
    assertThat(response.getEntity().getJson()).isEqualTo(new BadRequestException("Invalid realm, " + expectedInvalidRealm).toJsonValue().getObject());
}
Also used : BadRequestException(org.forgerock.json.resource.BadRequestException)

Example 77 with BadRequestException

use of org.forgerock.json.resource.BadRequestException 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 78 with BadRequestException

use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.

the class SoapSTSPublishServiceRequestHandler method handleQuery.

public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) {
    QueryFilter<JsonPointer> queryFilter = request.getQueryFilter();
    if (queryFilter == null) {
        return new BadRequestException(getQueryUsageString()).asPromise();
    }
    String realm;
    try {
        realm = getRealmFromQueryFilter(queryFilter);
    } catch (STSPublishException e) {
        return e.asPromise();
    }
    try {
        if (!realmValidator.isRealm(realm)) {
            return new BadRequestException("The specified realm does not exist.").asPromise();
        }
        final List<SoapSTSInstanceConfig> publishedInstances = publisher.getPublishedInstances(realm);
        for (SoapSTSInstanceConfig instanceConfig : publishedInstances) {
            /*
                Although instanceConfig.toJson() will yield the JsonValue which the handleResource invocation requires,
                the SoapSTSInstanceConfig is a complicated nesting of JsonValue objects, which should be 'homogenized'
                into a json format prior to inclusion in the response.
                 */
            handler.handleResource(newResourceResponse(instanceConfig.getDeploymentSubPath(), getInstanceConfigEtag(instanceConfig), new JsonValue(mapStringToJson(instanceConfig.toJson().toString()))));
        }
        return newResultPromise(newQueryResponse());
    } catch (STSPublishException e) {
        logger.error("Exception caught obtaining soap sts instances for realm " + (realm != null ? realm : "null realm") + "; Exception: " + e);
        return e.asPromise();
    }
}
Also used : SoapSTSInstanceConfig(org.forgerock.openam.sts.soap.config.user.SoapSTSInstanceConfig) STSPublishException(org.forgerock.openam.sts.STSPublishException) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) JsonPointer(org.forgerock.json.JsonPointer)

Example 79 with BadRequestException

use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.

the class SoapSTSPublishServiceRequestHandler method handleUpdate.

/*
     * A PUT to the url composed of the publish endpont + the sts instance id with a payload corresponding to a
     * SoapSTSInstanceId (wrapped in invocation context information) will result in republishing the existing instance
     * (which is a delete followed by a create).
     */
public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest request) {
    String stsId = request.getResourcePath();
    String realm = getRealmFromResourceName(request.getResourcePath());
    if (!realmValidator.isRealm(realm)) {
        logger.warn("Update of soap STS instance state for instance " + stsId + " in realm " + realm + " rejected because realm does not exist");
        return new NotFoundException("The specified realm does not exist.").asPromise();
    }
    /*
        Insure that the instance is published before performing an update.
         */
    final boolean publishedToSMS;
    try {
        publishedToSMS = (publisher.getPublishedInstance(stsId, realm) != null);
    } catch (STSPublishException e) {
        logger.error("In SoapSTSPublishServiceRequestHandler#handleUpdate, exception caught determining whether " + "instance persisted in SMS. Instance not updated. Exception: " + e, e);
        return e.asPromise();
    }
    if (publishedToSMS) {
        SoapSTSInstanceConfig instanceConfig;
        try {
            instanceConfig = marshalInstanceConfigFromInvocation(request.getContent());
        } catch (BadRequestException e) {
            logger.error("In SoapSTSPublishServiceRequestHandler#handleUpdate, exception caught marshalling " + "invocation state to SoapSTSInstanceConfig. Instance not updated. The state: " + request.getContent() + "Exception: " + e, e);
            return e.asPromise();
        }
        try {
            publisher.removeInstance(stsId, realm);
        } catch (STSPublishException e) {
            logger.error("In SoapSTSPublishServiceRequestHandler#handleUpdate, exception caught removing " + "soap sts instance " + instanceConfig.getDeploymentSubPath() + ". This means instance is" + "in indeterminate state, and has not been updated. The instance config: " + instanceConfig + "; Exception: " + e, e);
            return e.asPromise();
        }
        try {
            ResourceResponse response = publishInstance(instanceConfig);
            logger.info("Soap STS instance " + instanceConfig.getDeploymentSubPath() + " updated to state " + instanceConfig.toJson());
            return newResultPromise(response);
        } catch (ResourceException e) {
            logger.error("In SoapSTSPublishServiceRequestHandler#handleUpdate, exception caught publishing " + "soap sts instance " + instanceConfig.getDeploymentSubPath() + ". This means instance is" + "in indeterminate state, having been removed, but not successfully published with updated " + "state. The instance config: " + instanceConfig + "; Exception: " + e, e);
            return e.asPromise();
        }
    } else {
        //404 - realm and id not found in SMS
        return new NotFoundException("No soap sts instance with id " + stsId + " in realm " + realm).asPromise();
    }
}
Also used : SoapSTSInstanceConfig(org.forgerock.openam.sts.soap.config.user.SoapSTSInstanceConfig) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) STSPublishException(org.forgerock.openam.sts.STSPublishException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException)

Example 80 with BadRequestException

use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.

the class TokenGenerationService method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(final Context serverContext, final QueryRequest queryRequest, final QueryResourceHandler queryResultHandler) {
    QueryFilter<JsonPointer> queryFilter = queryRequest.getQueryFilter();
    if (queryFilter == null) {
        return new BadRequestException(getUsageString()).asPromise();
    }
    try {
        final QueryFilter<CoreTokenField> coreTokenFieldQueryFilter = convertToCoreTokenFieldQueryFilter(queryFilter);
        final List<STSIssuedTokenState> issuedTokens = ctsTokenPersistence.listTokens(coreTokenFieldQueryFilter);
        for (STSIssuedTokenState tokenState : issuedTokens) {
            queryResultHandler.handleResource(newResourceResponse(tokenState.getTokenId(), EMPTY_STRING, tokenState.toJson()));
        }
        return newResultPromise(newQueryResponse());
    } catch (CTSTokenPersistenceException e) {
        logger.error("Exception caught obtaining list of sts-issued tokens: " + e, e);
        return e.asPromise();
    }
}
Also used : BadRequestException(org.forgerock.json.resource.BadRequestException) STSIssuedTokenState(org.forgerock.openam.sts.user.invocation.STSIssuedTokenState) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) JsonPointer(org.forgerock.json.JsonPointer) CTSTokenPersistenceException(org.forgerock.openam.sts.CTSTokenPersistenceException)

Aggregations

BadRequestException (org.forgerock.json.resource.BadRequestException)82 JsonValue (org.forgerock.json.JsonValue)44 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)40 ResourceException (org.forgerock.json.resource.ResourceException)39 SSOException (com.iplanet.sso.SSOException)37 NotFoundException (org.forgerock.json.resource.NotFoundException)37 SMSException (com.sun.identity.sm.SMSException)31 ForbiddenException (org.forgerock.json.resource.ForbiddenException)26 ResourceResponse (org.forgerock.json.resource.ResourceResponse)25 IdRepoException (com.sun.identity.idm.IdRepoException)23 PermanentException (org.forgerock.json.resource.PermanentException)22 ConflictException (org.forgerock.json.resource.ConflictException)21 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)20 SSOToken (com.iplanet.sso.SSOToken)19 NotSupportedException (org.forgerock.json.resource.NotSupportedException)17 RealmContext (org.forgerock.openam.rest.RealmContext)17 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)16 DeleteFailedException (org.forgerock.openam.cts.exceptions.DeleteFailedException)16 IdentityDetails (com.sun.identity.idsvcs.IdentityDetails)14 MessagingException (javax.mail.MessagingException)13