RedirectingExceptionMapper implements ExceptionMapper<RedirectingException> { public Response toResponse(final RedirectingException exception) { Throwable cause = exception.getCause(); ExceptionMappers mappers = injector.getInstance(ExceptionMappers.class); ExceptionMapper mapper = mappers.findMapping(cause); Response r = mapper.toResponse(cause); ErrorResponse responseEntity = (ErrorResponse) r.getEntity(); UriBuilder builder = UriBuilder.fromUri(exception.getRedirect()); builder.queryParam("error", responseEntity.getError()); builder.queryParam("error_description", responseEntity.getErrorDescription()); if (exception.getClientType().equals(ClientType.Implicit)) { builder.fragment(builder.build().getQuery()); builder.replaceQuery(""); } return Response.fromResponse(r) .status(Status.FOUND) .header(HttpHeaders.LOCATION, builder.build()) .build(); } @Inject  RedirectingExceptionMapper(final InjectionManager injector); Response toResponse(final RedirectingException exception);  }
@Test public void testRegularMapping() throws Exception { KangarooException ke = new InvalidClientException(); InjectionManager injector = Injections.createInjectionManager(); ExceptionMappers mappers = new ExceptionMapperFactory(injector); injector.register(new KangarooExceptionMapper.Binder()); injector.register(new RedirectingExceptionMapper.Binder()); injector.register(Bindings.service(mappers) .to(ExceptionMappers.class)); RedirectingExceptionMapper mapper = injector.getInstance(RedirectingExceptionMapper.class); URI redirect = UriBuilder.fromUri("http: .build(); RedirectingException re = new RedirectingException(ke, redirect, ClientType.AuthorizationGrant); Response r = mapper.toResponse(re); assertEquals(302, r.getStatus()); URI location = UriBuilder.fromUri(r.getHeaderString(HttpHeaders.LOCATION)) .build(); assertEquals(location.getScheme(), redirect.getScheme()); assertEquals(location.getHost(), redirect.getHost()); assertEquals(location.getPort(), redirect.getPort()); assertEquals(location.getPath(), redirect.getPath()); MultivaluedMap<String, String> params = HttpUtil.parseQueryParams(location); assertEquals(params.getFirst("error"), ke.getCode().getError()); assertEquals(params.getFirst("error_description"), ke.getCode().getErrorDescription()); }
@Test public void testImplicitMapping() throws Exception { KangarooException ke = new InvalidClientException(); InjectionManager injector = Injections.createInjectionManager(); ExceptionMappers mappers = new ExceptionMapperFactory(injector); injector.register(new KangarooExceptionMapper.Binder()); injector.register(new RedirectingExceptionMapper.Binder()); injector.register(Bindings.service(mappers) .to(ExceptionMappers.class)); RedirectingExceptionMapper mapper = injector.getInstance(RedirectingExceptionMapper.class); URI redirect = UriBuilder.fromUri("http: .build(); RedirectingException re = new RedirectingException(ke, redirect, ClientType.Implicit); Response r = mapper.toResponse(re); assertEquals(302, r.getStatus()); URI location = UriBuilder.fromUri(r.getHeaderString(HttpHeaders.LOCATION)) .build(); assertEquals(location.getScheme(), redirect.getScheme()); assertEquals(location.getHost(), redirect.getHost()); assertEquals(location.getPort(), redirect.getPort()); assertEquals(location.getPath(), redirect.getPath()); MultivaluedMap<String, String> params = HttpUtil.parseQueryParams(location.getFragment()); assertEquals(params.getFirst("error"), ke.getCode().getError()); assertEquals(params.getFirst("error_description"), ke.getCode().getErrorDescription()); injector.shutdown(); }