Search in sources :

Example 71 with BadRequestException

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

the class SmsServerPropertiesResource method actionInstance.

@Override
public Promise<ActionResponse, ResourceException> actionInstance(Context serverContext, ActionRequest actionRequest) {
    if (actionRequest.getAction().equals("schema")) {
        Map<String, String> uriVariables = getUriTemplateVariables(serverContext);
        final String serverName = uriVariables.get("serverName").toLowerCase();
        if (serverName == null) {
            return new BadRequestException("Server name not specified.").asPromise();
        }
        try {
            ServiceConfigManager scm = getServiceConfigManager(serverContext);
            ServiceConfig serverConfigs = getServerConfigs(scm);
            if (!serverConfigs.getSubConfigNames().contains(serverName)) {
                return new BadRequestException("Unknown server: " + serverName).asPromise();
            }
        } catch (SSOException | SMSException e) {
            logger.error("Error getting server config", e);
        }
        final String tabName = getTabName(uriVariables);
        if (tabName == null) {
            return new BadRequestException("Tab name not specified.").asPromise();
        }
        JsonValue schema;
        final JsonPointer tabPointer = new JsonPointer("_schema/properties/" + tabName);
        if (DIRECTORY_CONFIGURATION_TAB_NAME.equalsIgnoreCase(tabName)) {
            schema = directoryConfigSchema;
        } else if (serverName.equals(SERVER_DEFAULT_NAME)) {
            schema = defaultSchema.get(tabPointer);
        } else {
            schema = nonDefaultSchema.get(tabPointer);
        }
        if (schema == null) {
            return new BadRequestException("Unknown tab: " + tabName).asPromise();
        }
        return newResultPromise(newActionResponse(schema));
    } else {
        return new NotSupportedException("Action not supported: " + actionRequest.getAction()).asPromise();
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) SSOException(com.iplanet.sso.SSOException) JsonPointer(org.forgerock.json.JsonPointer) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 72 with BadRequestException

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

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

the class RealmContextFilterTest method filterShouldFailToConsumeRealmFromRequestOnExceptionWhenResolvingServerName.

@Test
public void filterShouldFailToConsumeRealmFromRequestOnExceptionWhenResolvingServerName() throws Exception {
    //Given
    Context context = mockContext(ENDPOINT_PATH_ELEMENT);
    Request request = createRequest(HOSTNAME, ENDPOINT_PATH_ELEMENT);
    IdRepoException exception = mock(IdRepoException.class);
    given(exception.getMessage()).willReturn("EXCEPTION_MESSAGE");
    doThrow(exception).when(coreWrapper).getOrganization(any(SSOToken.class), eq(HOSTNAME));
    //When
    Response response = filter.filter(context, request, handler).getOrThrowUninterruptibly();
    //Then
    assertThat(response.getStatus()).isSameAs(Status.BAD_REQUEST);
    assertThat(response.getEntity().getJson()).isEqualTo(new BadRequestException("FQDN \"HOSTNAME\" is not valid.").toJsonValue().getObject());
}
Also used : RootContext(org.forgerock.services.context.RootContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) AttributesContext(org.forgerock.services.context.AttributesContext) Response(org.forgerock.http.protocol.Response) ActionResponse(org.forgerock.json.resource.ActionResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SSOToken(com.iplanet.sso.SSOToken) CreateRequest(org.forgerock.json.resource.CreateRequest) ActionRequest(org.forgerock.json.resource.ActionRequest) ReadRequest(org.forgerock.json.resource.ReadRequest) DeleteRequest(org.forgerock.json.resource.DeleteRequest) UpdateRequest(org.forgerock.json.resource.UpdateRequest) PatchRequest(org.forgerock.json.resource.PatchRequest) Request(org.forgerock.http.protocol.Request) QueryRequest(org.forgerock.json.resource.QueryRequest) IdRepoException(com.sun.identity.idm.IdRepoException) BadRequestException(org.forgerock.json.resource.BadRequestException) Test(org.testng.annotations.Test)

Example 74 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 75 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)

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