Search in sources :

Example 21 with InternalServerErrorException

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();
    }
}
Also used : Set(java.util.Set) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) SSOException(com.iplanet.sso.SSOException) ResourceException(org.forgerock.json.resource.ResourceException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 22 with InternalServerErrorException

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();
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 23 with InternalServerErrorException

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) AuthenticationFailedException(org.forgerock.caf.authentication.framework.AuthenticationFailedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Response(org.forgerock.http.protocol.Response) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) AuditTrail(org.forgerock.caf.authentication.framework.AuditTrail) Map(java.util.Map) TransformerException(javax.xml.transform.TransformerException)

Example 24 with InternalServerErrorException

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);
    }
}
Also used : HashMap(java.util.HashMap) HttpURLConnectionWrapper(org.forgerock.openam.sts.HttpURLConnectionWrapper) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) IOException(java.io.IOException) TokenValidationException(org.forgerock.openam.sts.TokenValidationException)

Example 25 with InternalServerErrorException

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) HttpURLConnectionWrapper(org.forgerock.openam.sts.HttpURLConnectionWrapper) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) IOException(java.io.IOException)

Aggregations

InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)70 SSOException (com.iplanet.sso.SSOException)39 JsonValue (org.forgerock.json.JsonValue)33 SMSException (com.sun.identity.sm.SMSException)29 BadRequestException (org.forgerock.json.resource.BadRequestException)27 NotFoundException (org.forgerock.json.resource.NotFoundException)25 ResourceException (org.forgerock.json.resource.ResourceException)24 SSOToken (com.iplanet.sso.SSOToken)19 IdRepoException (com.sun.identity.idm.IdRepoException)18 Set (java.util.Set)15 ResourceResponse (org.forgerock.json.resource.ResourceResponse)15 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)14 AMIdentity (com.sun.identity.idm.AMIdentity)13 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11 ForbiddenException (org.forgerock.json.resource.ForbiddenException)11 ServiceConfig (com.sun.identity.sm.ServiceConfig)10 NotSupportedException (org.forgerock.json.resource.NotSupportedException)10 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)10 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)9