use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SmsSingletonProvider method handleCreate.
/**
* Creates config for the singleton instance referenced, and returns the JsonValue representation.
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> handleCreate(Context serverContext, CreateRequest createRequest) {
final String realm = realmFor(serverContext);
try {
Map<String, Set<String>> attrs = convertFromJson(createRequest.getContent(), realm);
ServiceConfigManager scm = getServiceConfigManager(serverContext);
ServiceConfig config;
if (subSchemaPath.isEmpty()) {
if (type == SchemaType.GLOBAL) {
config = scm.createGlobalConfig(attrs);
} else {
config = scm.createOrganizationConfig(realm, attrs);
}
} else {
ServiceConfig parent = parentSubConfigFor(serverContext, scm);
parent.addSubConfig(resourceId(), lastSchemaNodeName(), -1, attrs);
config = parent.getSubConfig(lastSchemaNodeName());
}
JsonValue result = withExtraAttributes(realm, convertToJson(realm, config));
return newResultPromise(newResourceResponse(resourceId(), String.valueOf(result.hashCode()), result));
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SmsSingletonProvider method handleDelete.
/**
* Deletes config for the singleton instance referenced.
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> handleDelete(Context serverContext, DeleteRequest deleteRequest) {
try {
ServiceConfigManager scm = getServiceConfigManager(serverContext);
if (subSchemaPath.isEmpty()) {
if (type == SchemaType.GLOBAL) {
scm.removeGlobalConfiguration(null);
} else {
scm.deleteOrganizationConfig(realmFor(serverContext));
}
} else {
ServiceConfig parent = parentSubConfigFor(serverContext, scm);
parent.removeSubConfig(resourceId());
}
return newResultPromise(newResourceResponse(resourceId(), "0", json(object(field("success", true)))));
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class XMLResourceExceptionHandler method write.
@Override
public void write(MessageContext context, AuthenticationException exception) {
Reject.ifNull(exception);
try {
ResourceException jre;
if (exception instanceof AuthenticationFailedException) {
jre = new PermanentException(Status.UNAUTHORIZED.getCode(), exception.getMessage(), null);
} else if (exception.getCause() instanceof ResourceException) {
jre = (ResourceException) exception.getCause();
} else {
LOGGER.error(exception.getMessage(), exception);
jre = new InternalServerErrorException("Authentication Failed", exception);
}
AuditTrail auditTrail = context.getAuditTrail();
List<Map<String, Object>> failureReasonList = auditTrail.getFailureReasons();
if (failureReasonList != null && !failureReasonList.isEmpty()) {
jre.setDetail(json(object(field("failureReasons", failureReasonList))));
}
Response response = context.getResponse();
response.setStatus(Status.valueOf(jre.getCode()));
context.<Response>getResponse().getHeaders().put(ContentTypeHeader.valueOf(MediaType.XML_UTF_8.toString()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Transformer transformer = XMLUtils.getTransformerFactory().newTransformer();
transformer.transform(new DOMSource(asXMLDOM(jre.includeCauseInJsonValue().toJsonValue().asMap())), new StreamResult(outputStream));
response.getEntity().setBytes(outputStream.toByteArray());
} catch (TransformerException e1) {
throw new IllegalStateException("Could not write XML to response", e1);
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SoapSTSAccessTokenProviderImpl method getAccessToken.
@Override
public String getAccessToken() throws ResourceException {
Map<String, String> headerMap = new HashMap<>();
headerMap.put(AMSTSConstants.CONTENT_TYPE, AMSTSConstants.APPLICATION_JSON);
headerMap.put(AMSTSConstants.CREST_VERSION_HEADER_KEY, authNServiceVersion);
headerMap.put(AMSTSConstants.AM_REST_AUTHN_USERNAME_HEADER, credentialsAccess.getAgentUsername());
headerMap.put(AMSTSConstants.AM_REST_AUTHN_PASSWORD_HEADER, credentialsAccess.getAgentPassword());
try {
HttpURLConnectionWrapper.ConnectionResult connectionResult = httpURLConnectionWrapperFactory.httpURLConnectionWrapper(authenticateUrl).setRequestHeaders(headerMap).setRequestMethod(AMSTSConstants.POST).makeInvocation();
final int responseCode = connectionResult.getStatusCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw ResourceException.getException(responseCode, "Non-200 response authenticating against " + authenticateUrl + " : " + connectionResult.getResult());
} else {
try {
return amTokenParser.getSessionFromAuthNResponse(connectionResult.getResult());
} catch (TokenValidationException e) {
throw new InternalServerErrorException("Exception caught obtaining the soap-sts-agent token " + e, e);
}
}
} catch (IOException ioe) {
throw new InternalServerErrorException("IOException caught obtaining the soap-sts-agent token: " + ioe, ioe);
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SoapSTSAgentConfigAccessImpl method getConfigurationState.
@Override
public JsonValue getConfigurationState() throws ResourceException {
String sessionId = null;
try {
sessionId = accessTokenProvider.getAccessToken();
Map<String, String> headerMap = new HashMap<>();
headerMap.put(AMSTSConstants.CONTENT_TYPE, AMSTSConstants.APPLICATION_JSON);
headerMap.put(AMSTSConstants.CREST_VERSION_HEADER_KEY, agentsProfileServiceVersion);
headerMap.put(AMSTSConstants.COOKIE, createAMSessionCookie(sessionId));
HttpURLConnectionWrapper.ConnectionResult connectionResult = httpURLConnectionWrapperFactory.httpURLConnectionWrapper(agentProfileUrl).setRequestHeaders(headerMap).setRequestMethod(AMSTSConstants.GET).makeInvocation();
final int responseCode = connectionResult.getStatusCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return JsonValueBuilder.toJsonValue(connectionResult.getResult());
} else {
throw ResourceException.getException(responseCode, "non 200 response from agent config service at: " + agentProfileUrl + " : " + connectionResult.getResult());
}
} catch (IOException e) {
throw new InternalServerErrorException("Exception caught obtaining agent config state from: " + agentProfileUrl + "; Exception: " + e);
} finally {
if (sessionId != null) {
accessTokenProvider.invalidateAccessToken(sessionId);
}
}
}
Aggregations