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();
}
}
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;
}
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());
}
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());
}
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();
}
}
Aggregations