Search in sources :

Example 1 with FrameworkClientException

use of org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException in project carbon-identity-framework by wso2.

the class SessionExtenderRequestFactory method handleException.

@Override
public HttpIdentityResponse.HttpIdentityResponseBuilder handleException(FrameworkClientException exception, HttpServletRequest request, HttpServletResponse response) {
    HttpIdentityResponse.HttpIdentityResponseBuilder errorResponseBuilder = new HttpIdentityResponse.HttpIdentityResponseBuilder();
    SessionExtenderErrorResponse.SessionExtenderErrorResponseBuilder sessionErrorResponseBuilder = new SessionExtenderErrorResponse.SessionExtenderErrorResponseBuilder();
    sessionErrorResponseBuilder.setErrorCode(exception.getErrorCode());
    String traceId = FrameworkUtils.getCorrelation();
    String errorLogDescription;
    if (exception instanceof SessionExtenderClientException) {
        sessionErrorResponseBuilder.setErrorMessage(((SessionExtenderClientException) exception).getErrorMessage());
        sessionErrorResponseBuilder.setErrorDescription(((SessionExtenderClientException) exception).getDescription());
        sessionErrorResponseBuilder.setTraceId(traceId);
        errorLogDescription = ((SessionExtenderClientException) exception).getDescription();
    } else {
        sessionErrorResponseBuilder.setErrorMessage(exception.getMessage());
        errorLogDescription = exception.getMessage();
    }
    if (log.isDebugEnabled()) {
        String clientErrorLog = String.format(ERROR_LOG_TEMPLATE, traceId, SessionExtenderRequestFactory.class.getName(), errorLogDescription);
        log.debug(clientErrorLog);
    }
    SessionExtenderErrorResponse sessionExtenderErrorResponse = sessionErrorResponseBuilder.build();
    errorResponseBuilder.setStatusCode(SC_BAD_REQUEST);
    errorResponseBuilder.setContentType(APPLICATION_JSON);
    errorResponseBuilder.setBody(sessionExtenderErrorResponse.getResponse());
    return errorResponseBuilder;
}
Also used : SessionExtenderErrorResponse(org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderErrorResponse) SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException) HttpIdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse)

Example 2 with FrameworkClientException

use of org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException in project carbon-identity-framework by wso2.

the class HttpIdentityRequestFactoryTest method testHandleFrameworkClientException.

@Test
public void testHandleFrameworkClientException() throws Exception {
    FrameworkClientException clientException = new FrameworkClientException(ERROR_MESSAGE);
    HttpIdentityResponseBuilder responseBuilder = httpIdentityRequestFactory.handleException(clientException, request, response);
    assertEquals(responseBuilder.body, ERROR_MESSAGE);
    assertEquals(responseBuilder.statusCode, 400);
}
Also used : HttpIdentityResponseBuilder(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse.HttpIdentityResponseBuilder) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with FrameworkClientException

use of org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException in project identity-inbound-auth-oauth by wso2-extensions.

the class HttpUnRegistrationResponseFactoryTest method testHandleException.

@Test
public void testHandleException() throws Exception {
    mockHttpIdentityResponseBuilder = mock(HttpIdentityResponse.HttpIdentityResponseBuilder.class);
    whenNew(HttpIdentityResponse.HttpIdentityResponseBuilder.class).withNoArguments().thenReturn(mockHttpIdentityResponseBuilder);
    final Integer[] statusCode = new Integer[1];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            statusCode[0] = (Integer) invocation.getArguments()[0];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).setStatusCode(anyInt());
    final String[] header = new String[3];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            header[0] = (String) invocation.getArguments()[1];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).addHeader(eq(OAuthConstants.HTTP_RESP_HEADER_CACHE_CONTROL), anyString());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            header[1] = (String) invocation.getArguments()[1];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).addHeader(eq(OAuthConstants.HTTP_RESP_HEADER_PRAGMA), anyString());
    FrameworkClientException exception = mock(FrameworkClientException.class);
    when(exception.getMessage()).thenReturn("dummyDescription");
    httpUnregistrationResponseFactory.handleException(exception);
    assertEquals(header[0], OAuthConstants.HTTP_RESP_HEADER_VAL_CACHE_CONTROL_NO_STORE, "Wrong header value " + "for " + OAuthConstants.HTTP_RESP_HEADER_CACHE_CONTROL);
    assertEquals(header[1], OAuthConstants.HTTP_RESP_HEADER_VAL_PRAGMA_NO_CACHE, "Wrong header value for " + OAuthConstants.HTTP_RESP_HEADER_PRAGMA);
    assertEquals((int) statusCode[0], HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Status code doesn't match with " + HttpServletResponse.SC_BAD_REQUEST);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpIdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse) Matchers.anyString(org.mockito.Matchers.anyString) FrameworkClientException(org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with FrameworkClientException

use of org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException in project identity-inbound-auth-oauth by wso2-extensions.

the class RegistrationRequestFactoryTest method testHandleException.

@Test
public void testHandleException() throws Exception {
    whenNew(HttpIdentityResponse.HttpIdentityResponseBuilder.class).withNoArguments().thenReturn(mockHttpIdentityResponseBuilder);
    final Integer[] statusCode = new Integer[1];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            statusCode[0] = (Integer) invocation.getArguments()[0];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).setStatusCode(anyInt());
    final String[] header = new String[3];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            header[0] = (String) invocation.getArguments()[1];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).addHeader(eq(OAuthConstants.HTTP_RESP_HEADER_CACHE_CONTROL), anyString());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            header[1] = (String) invocation.getArguments()[1];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).addHeader(eq(OAuthConstants.HTTP_RESP_HEADER_PRAGMA), anyString());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            header[2] = (String) invocation.getArguments()[1];
            return null;
        }
    }).when(mockHttpIdentityResponseBuilder).addHeader(eq(HttpHeaders.CONTENT_TYPE), anyString());
    FrameworkClientException exception = mock(FrameworkClientException.class);
    when(exception.getMessage()).thenReturn(dummyDescription);
    registrationRequestFactory.handleException(exception, mockHttpRequest, mockHttpResponse);
    assertEquals(header[0], OAuthConstants.HTTP_RESP_HEADER_VAL_CACHE_CONTROL_NO_STORE, "Wrong header value " + "for " + OAuthConstants.HTTP_RESP_HEADER_CACHE_CONTROL);
    assertEquals(header[1], OAuthConstants.HTTP_RESP_HEADER_VAL_PRAGMA_NO_CACHE, "Wrong header value for " + OAuthConstants.HTTP_RESP_HEADER_PRAGMA);
    assertEquals(header[2], MediaType.APPLICATION_JSON, "Wrong header value for " + HttpHeaders.CONTENT_TYPE);
    assertEquals((int) statusCode[0], HttpServletResponse.SC_BAD_REQUEST, "Status code doesn't match with " + HttpServletResponse.SC_BAD_REQUEST);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.simple.JSONObject) HttpIdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse) Matchers.anyString(org.mockito.Matchers.anyString) FrameworkClientException(org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with FrameworkClientException

use of org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessorTest method getInstanceType.

@DataProvider(name = "instanceTypeprovider")
public Object[][] getInstanceType() throws FrameworkClientException {
    RegistrationRequest registrationRequest = mock(RegistrationRequest.class);
    UnregistrationRequest unregistrationRequest = mock(UnregistrationRequest.class);
    return new Object[][] { { "RegistrationRequest", registrationRequest }, { "UnregistrationRequest", unregistrationRequest } };
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest) DataProvider(org.testng.annotations.DataProvider)

Aggregations

JSONObject (org.json.simple.JSONObject)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 FrameworkClientException (org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException)3 HttpIdentityResponse (org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse)3 RegistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)3 UnregistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest)3 JSONArray (org.json.simple.JSONArray)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DataProvider (org.testng.annotations.DataProvider)2 SessionExtenderClientException (org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException)2 RegistrationRequestProfile (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile)2 OIDCRegistrationRequestProfile (org.wso2.carbon.identity.oidc.dcr.model.OIDCRegistrationRequestProfile)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Cookie (javax.servlet.http.Cookie)1 JSONParser (org.json.simple.parser.JSONParser)1