use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.
the class XacmlService method importXACML.
/**
* Expects to receive XACML formatted XML which will be read and imported.
*/
@Post
public Representation importXACML(Representation entity) {
boolean dryRun = "true".equalsIgnoreCase(getQuery().getFirstValue("dryrun"));
List<ImportStep> steps;
try {
if (!checkPermission("MODIFY")) {
// not allowed
throw new ResourceException(new Status(FORBIDDEN));
}
String realm = RestletRealmRouter.getRealmFromRequest(getRequest());
steps = importExport.importXacml(realm, entity.getStream(), getAdminToken(), dryRun);
if (steps.isEmpty()) {
throw new ResourceException(new Status(BAD_REQUEST, "No policies found in XACML document", null, null));
}
List<Map<String, String>> result = new ArrayList<Map<String, String>>();
for (XACMLExportImport.ImportStep step : steps) {
Map<String, String> stepResult = new HashMap<String, String>();
stepResult.put("status", String.valueOf(step.getDiffStatus().getCode()));
stepResult.put("name", step.getPrivilege().getName());
result.add(stepResult);
}
getResponse().setStatus(Status.SUCCESS_OK);
return jacksonRepresentationFactory.create(result);
} catch (EntitlementException e) {
debug.warning("Importing XACML to policies failed", e);
throw new ResourceException(new Status(BAD_REQUEST, e, e.getLocalizedMessage(getRequestLocale()), null, null));
} catch (IOException e) {
debug.warning("Reading XACML import failed", e);
throw new ResourceException(new Status(BAD_REQUEST, e, e.getLocalizedMessage(), null, null));
}
}
use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpoint method updateResourceSet.
@Put
public Representation updateResourceSet(JsonRepresentation entity) throws NotFoundException, ServerException, BadRequestException {
if (!isConditionalRequest()) {
throw new ResourceException(512, "precondition_failed", "Require If-Match header to update Resource Set", null);
}
final Map<String, Object> resourceSetDescriptionAttributes = validator.validate(toMap(entity));
final String resourceSetId = getResourceSetId();
ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId()).update(resourceSetDescriptionAttributes);
JsonValue labels = resourceSetDescription.getDescription().get(OAuth2Constants.ResourceSets.LABELS);
resourceSetDescription.getDescription().remove(OAuth2Constants.ResourceSets.LABELS);
store.update(resourceSetDescription);
if (labels.isNotNull()) {
resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, labels.asSet());
} else {
resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, new HashSet<String>());
}
labelRegistration.updateLabelsForExistingResourceSet(resourceSetDescription);
return createJsonResponse(resourceSetDescription, false, true);
}
use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.
the class RestletRealmRouter method doHandle.
/**
* <p>Takes the last realm URI parameter from the request and appends to the growing full realm value.</p>
*
* <p>i.e. last realm URI parameter: realm2, current full realm value: /realm1, after appending: /realm1/realm2.</p>
*
* @param next {@inheritDoc}
* @param request {@inheritDoc}
* @param response {@inheritDoc}
*/
@Override
protected void doHandle(Restlet next, Request request, Response response) {
RealmInfo realmInfo = getRealmFromURI(request);
if (realmInfo == null) {
realmInfo = getRealmFromServerName(request);
}
if (next != delegateRoute) {
String overrideRealm = getRealmFromQueryString(request);
if (overrideRealm != null) {
realmInfo = realmInfo.withOverrideRealm(overrideRealm);
}
request.getAttributes().put(REALM_URL, request.getResourceRef().getBaseRef().toString());
}
// Check that the path references an existing realm
if (!realmValidator.isRealm(realmInfo.getAbsoluteRealm())) {
String realm = realmInfo.getAbsoluteRealm();
try {
SSOToken adminToken = coreWrapper.getAdminToken();
//Need to strip off leading '/' from realm otherwise just generates a DN based of the realm value, which is wrong
if (realmInfo.getAbsoluteRealm().startsWith("/")) {
realm = realm.substring(1);
}
String orgDN = coreWrapper.getOrganization(adminToken, realm);
realmInfo = realmInfo.withAbsoluteRealm(coreWrapper.convertOrgNameToRealmName(orgDN));
} catch (IdRepoException | SSOException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid realm, " + realm);
}
}
request.getAttributes().put(REALM, realmInfo.getAbsoluteRealm());
request.getAttributes().put(REALM_INFO, realmInfo);
HttpServletRequest httpRequest = ServletUtils.getRequest(request);
httpRequest.setAttribute(REALM, realmInfo.getAbsoluteRealm());
httpRequest.setAttribute(REALM_INFO, realmInfo);
request.getAttributes().remove("subrealm");
super.doHandle(next, request, response);
}
use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.
the class XacmlServiceTest method testExportXACMLEntitlementException.
@Test
public void testExportXACMLEntitlementException() throws Exception {
//given
EntitlementException ee = new EntitlementException(EntitlementException.JSON_PARSE_ERROR);
doThrow(ee).when(importExport).exportXACML(eq("/"), any(Subject.class), any(List.class));
try {
//when
Representation result = service.exportXACML();
//then
fail("Expect exception");
} catch (ResourceException e) {
assertThat(e.getStatus().getCode()).isEqualTo(INTERNAL_ERROR);
assertThat(e.getMessage()).isEqualTo("JSON Exception.");
}
}
use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.
the class XacmlServiceTest method testImportXACMLIOException.
@Test
public void testImportXACMLIOException() throws Exception {
//given
Representation representation = mock(Representation.class);
doThrow(new IOException()).when(representation).getStream();
try {
//when
service.importXACML(representation);
//then
fail("Expect exception");
} catch (ResourceException e) {
assertThat(e.getStatus().getCode()).isEqualTo(BAD_REQUEST);
}
}
Aggregations