Search in sources :

Example 51 with Representation

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;
}
Also used : HashMap(java.util.HashMap) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation)

Example 52 with 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;
}
Also used : Status(org.restlet.data.Status) EntitlementException(com.sun.identity.entitlement.EntitlementException) OutputRepresentation(org.restlet.representation.OutputRepresentation) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Disposition(org.restlet.data.Disposition) ResourceException(org.restlet.resource.ResourceException) ResourceException(org.forgerock.json.resource.ResourceException) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) OutputRepresentation(org.restlet.representation.OutputRepresentation) Representation(org.restlet.representation.Representation) IOException(java.io.IOException) PolicySet(com.sun.identity.entitlement.xacml3.core.PolicySet) VisibleForTesting(org.forgerock.util.annotations.VisibleForTesting)

Example 53 with Representation

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);
}
Also used : Disposition(org.restlet.data.Disposition) List(java.util.List) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) PolicySet(com.sun.identity.entitlement.xacml3.core.PolicySet) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 54 with Representation

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.");
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 55 with Representation

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StubPrivilege(org.forgerock.openam.entitlement.rest.StubPrivilege) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) XACMLExportImport(com.sun.identity.entitlement.xacml3.XACMLExportImport) ImportStep(com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep) Subject(javax.security.auth.Subject) ImportStep(com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10