Search in sources :

Example 1 with ResourceException

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));
    }
}
Also used : Status(org.restlet.data.Status) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XACMLExportImport(com.sun.identity.entitlement.xacml3.XACMLExportImport) IOException(java.io.IOException) ImportStep(com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep) EntitlementException(com.sun.identity.entitlement.EntitlementException) ResourceException(org.restlet.resource.ResourceException) ResourceException(org.forgerock.json.resource.ResourceException) Map(java.util.Map) HashMap(java.util.HashMap) ImportStep(com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep) Post(org.restlet.resource.Post)

Example 2 with ResourceException

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);
}
Also used : ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.restlet.resource.ResourceException) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Put(org.restlet.resource.Put)

Example 3 with ResourceException

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RealmInfo(org.forgerock.openam.core.RealmInfo) SSOToken(com.iplanet.sso.SSOToken) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) ResourceException(org.restlet.resource.ResourceException)

Example 4 with ResourceException

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.");
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) List(java.util.List) 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 5 with ResourceException

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);
    }
}
Also used : JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ResourceException (org.restlet.resource.ResourceException)70 Representation (org.restlet.representation.Representation)20 PermissionException (org.vcell.util.PermissionException)20 VCellApiApplication (org.vcell.rest.VCellApiApplication)18 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)15 IOException (java.io.IOException)13 StringRepresentation (org.restlet.representation.StringRepresentation)12 ArrayList (java.util.ArrayList)10 Reference (org.restlet.data.Reference)8 BioModel (cbit.vcell.biomodel.BioModel)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 JSONObject (org.json.JSONObject)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 XMLSource (cbit.vcell.xml.XMLSource)6 Response (org.restlet.Response)6 User (org.vcell.util.document.User)6 EntitlementException (com.sun.identity.entitlement.EntitlementException)4 EntityReference (org.qi4j.api.entity.EntityReference)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4