use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResource method createApplicationWrapper.
/**
* Creates an {@link ApplicationWrapper} to hold the {@link Application} object, after having deserialized it
* via Jackson.
*
* @param jsonValue The JSON to deserialize
* @param mySubject The subject authorizing the request
* @return An ApplicationWrapper containing an Application, null
* @throws EntitlementException If there were issues generating the application wrapper
*/
protected ApplicationWrapper createApplicationWrapper(JsonValue jsonValue, Subject mySubject) throws EntitlementException {
final ApplicationWrapper wrapp = createApplicationWrapper(jsonValue);
final JsonValue appTypeValue = jsonValue.get("applicationType");
if (appTypeValue.getObject() == null || appTypeValue.asString().isEmpty() || !wrapp.setApplicationType(mySubject, appTypeValue.asString())) {
if (debug.errorEnabled()) {
debug.error("ApplicationsResource.createApplicationWrapper() : " + "Specified Application Type was not available.");
}
throw new EntitlementException(EntitlementException.INVALID_APP_TYPE);
}
return wrapp;
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResource method readInstance.
/**
* Reads an instance of an application.
*
* @param context {@inheritDoc}
* @param resourceId {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
final Subject mySubject = getContextSubject(context);
if (mySubject == null) {
debug.error("ApplicationsResource :: READ : Unknown Subject");
return new BadRequestException().asPromise();
}
final String realm = getRealm(context);
final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
try {
final Application app = appManager.getApplication(mySubject, realm, resourceId);
if (app == null) {
throw new EntitlementException(EntitlementException.APP_RETRIEVAL_ERROR, new String[] { realm });
}
final ApplicationWrapper wrapp = createApplicationWrapper(app, appTypeManagerWrapper);
ResourceResponse resource = newResourceResponse(resourceId, Long.toString(app.getLastModifiedDate()), wrapp.toJsonValue());
return newResultPromise(resource);
} catch (EntitlementException e) {
if (debug.errorEnabled()) {
debug.error("ApplicationsResource :: READ by " + principalName + ": Application failed to retrieve the resource specified.", e);
}
return exceptionMappingHandler.handleError(context, request, e).asPromise();
}
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResource method createInstance.
/**
* Create an {@link Application}.
*
* @param context {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request) {
//auth
final Subject callingSubject = getContextSubject(context);
if (callingSubject == null) {
debug.error("ApplicationsResource :: CREATE : Unknown Subject");
return new BadRequestException().asPromise();
}
final String realm = getRealm(context);
//select
final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(callingSubject);
final JsonValue creationRequest = request.getContent();
final ApplicationWrapper wrapp;
final Application previousApp;
try {
wrapp = createApplicationWrapper(creationRequest, callingSubject);
String wrappName = wrapp.getName();
String newResourceId = request.getNewResourceId();
if (wrappName != null && newResourceId != null) {
if (!wrappName.equals(newResourceId)) {
debug.error("ApplicationsResource :: CREATE : Resource name and JSON body name do not match.");
throw new EntitlementException(EntitlementException.APPLICATION_NAME_MISMATCH);
}
}
// user.
if (wrappName == null) {
wrapp.setName(newResourceId);
}
String appName = wrapp.getApplication().getName();
if (!appName.equals(DN.escapeAttributeValue(appName))) {
throw new EntitlementException(EntitlementException.INVALID_VALUE, new Object[] { "policy name \"" + appName + "\"" });
}
previousApp = appManager.getApplication(callingSubject, realm, appName);
if (previousApp != null) {
//return conflict
throw new EntitlementException(EntitlementException.APPLICATION_ALREADY_EXISTS);
}
appManager.saveApplication(callingSubject, realm, wrapp.getApplication());
Application savedApp = appManager.getApplication(callingSubject, realm, appName);
ApplicationWrapper savedAppWrapper = createApplicationWrapper(savedApp, appTypeManagerWrapper);
ResourceResponse resource = newResourceResponse(savedAppWrapper.getName(), Long.toString(savedAppWrapper.getLastModifiedDate()), savedAppWrapper.toJsonValue());
if (debug.messageEnabled()) {
debug.message("ApplicationsResource :: CREATE by " + principalName + ": for Application: " + wrapp.getName());
}
return newResultPromise(resource);
} catch (EntitlementException e) {
if (debug.errorEnabled()) {
debug.error("ApplicationsResource :: CREATE by " + principalName + ": Application creation failed. ", e);
}
return exceptionMappingHandler.handleError(context, request, e).asPromise();
}
}
Aggregations