use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpoint method createJsonResponse.
private Representation createJsonResponse(ResourceSetDescription resourceSetDescription, boolean includeResourceSet, boolean withPolicyUri) {
Map<String, Object> response = new HashMap<String, Object>();
if (includeResourceSet) {
response = new HashMap<String, Object>(resourceSetDescription.asMap());
}
response.put(ID_FIELD, resourceSetDescription.getId());
if (withPolicyUri && resourceSetDescription.getPolicyUri() != null) {
response.put(POLICY_URI_FIELD, resourceSetDescription.getPolicyUri());
}
Representation representation = jacksonRepresentationFactory.create(response);
representation.setTag(generateETag(resourceSetDescription));
return representation;
}
use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class XacmlService method exportXACML.
/**
* This version of exportXACML here for testing - it saves trying to mock the static getRealmFromRequest
* @param realm The realm
* @return Representation object wrapping the converted XACML
*/
@VisibleForTesting
Representation exportXACML(String realm) {
List<String> filters = new ArrayList<String>(Arrays.asList(getQuery().getValuesArray(QUERY_PARAM_STRING)));
PolicySet policySet;
try {
if (!checkPermission("READ")) {
throw new ResourceException(new Status(FORBIDDEN));
}
policySet = importExport.exportXACML(realm, getAdminToken(), filters);
getResponse().setStatus(Status.SUCCESS_OK);
} catch (EntitlementException e) {
debug.warning("Reading Policies failed", e);
throw new ResourceException(new Status(INTERNAL_ERROR, e.getLocalizedMessage(getRequestLocale()), null, null));
}
final PolicySet finalPolicySet = policySet;
Representation result = new OutputRepresentation(XACMLServiceEndpointApplication.APPLICATION_XML_XACML3) {
@Override
public void write(OutputStream outputStream) throws IOException {
try {
XACMLPrivilegeUtils.writeXMLToStream(finalPolicySet, outputStream);
} catch (EntitlementException e) {
throw new IOException(e);
}
}
};
// OPENAM-4974
Disposition disposition = new Disposition();
disposition.setType(Disposition.TYPE_ATTACHMENT);
disposition.setFilename(getPolicyAttachmentFileName(realm));
result.setDisposition(disposition);
return result;
}
use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class XacmlServiceTest method testDispositionOfRootRealmExport.
@Test
public void testDispositionOfRootRealmExport() throws Exception {
//given
query.add(XacmlService.QUERY_PARAM_STRING, "test1");
query.add(XacmlService.QUERY_PARAM_STRING, "test2");
PolicySet policySet = new PolicySet();
doReturn(policySet).when(importExport).exportXACML(eq("/"), any(Subject.class), any(List.class));
//when
Representation result = service.exportXACML("/");
Disposition disposition = result.getDisposition();
assertThat(disposition.getFilename()).isEqualTo("realm-policies.xml");
assertThat(disposition.getType()).isEqualTo(disposition.TYPE_ATTACHMENT);
}
use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class XacmlServiceTest method testImportXACMLImportFailure.
@Test
public void testImportXACMLImportFailure() throws Exception {
//given
Representation representation = mock(Representation.class);
InputStream is = new ByteArrayInputStream("Hello World".getBytes());
doReturn(is).when(representation).getStream();
EntitlementException failure = new EntitlementException(EntitlementException.JSON_PARSE_ERROR);
doThrow(failure).when(importExport).importXacml(eq("/"), eq(is), any(Subject.class), eq(false));
try {
//when
service.importXACML(representation);
//then
fail("Expect exception");
} catch (ResourceException e) {
assertThat(e.getStatus().getCode()).isEqualTo(BAD_REQUEST);
assertThat(e.getMessage()).isEqualTo("JSON Exception.");
}
}
use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class XacmlServiceTest method testImportXACML.
@Test
public void testImportXACML() throws Exception {
//given
Representation representation = mock(Representation.class);
InputStream is = new ByteArrayInputStream("Hello World".getBytes());
doReturn(is).when(representation).getStream();
StubPrivilege privilege = new StubPrivilege();
privilege.setName("fred");
XACMLExportImport.ImportStep importStep = mock(XACMLExportImport.ImportStep.class);
doReturn(XACMLExportImport.DiffStatus.ADD).when(importStep).getDiffStatus();
doReturn(privilege).when(importStep).getPrivilege();
List<ImportStep> steps = Arrays.asList(importStep);
doReturn(steps).when(importExport).importXacml(eq("/"), eq(is), any(Subject.class), eq(false));
//when
Representation result = service.importXACML(representation);
//then
assertThat(result).isInstanceOf(JacksonRepresentation.class);
Map<String, Object> resultMap = JsonValueBuilder.toJsonArray(result.getText()).get(0).asMap();
assertThat(resultMap).contains(entry("status", "A"), entry("name", "fred"));
verify(response).setStatus(Status.SUCCESS_OK);
}
Aggregations