Search in sources :

Example 1 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class ResponseReadAndBufferEntityTest method testCloseUnreadResponseWithEntityStreamThatFailsToClose.

/**
     * This method tests behavior of input stream operations in case the underlying input stream throws an exception when closed.
     * Reproducer for JRFCAF-1344.
     * <p>
     * UC-3 : Try to close the response - underlying exception should be reported.
     */
@Test
public void testCloseUnreadResponseWithEntityStreamThatFailsToClose() throws Exception {
    final CorruptableInputStream entityStream = new CorruptableInputStream();
    final WebTarget target = target("response/corrupted");
    target.register(new ClientResponseFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
            responseContext.setEntityStream(entityStream);
        }
    });
    final Response response = target.request().buildGet().invoke();
    entityStream.setCorruptClose(true);
    try {
        response.close();
        fail("ProcessingException expected when closing the context and underlying stream throws an IOException.");
    } catch (ProcessingException pe) {
        assertThat(pe.getCause(), instanceOf(IOException.class));
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class Jersey2878ITCase method configureClient.

@Override
protected void configureClient(ClientConfig config) {
    ClientResponseFilter trackInputStreams = new ClientResponseFilter() {

        @Override
        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
            responseInputStreams.add(responseContext.getEntityStream());
        }
    };
    config.register(trackInputStreams);
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) ClientResponseContext(javax.ws.rs.client.ClientResponseContext)

Example 3 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class OAuth2Test method testFlow.

private void testFlow(final boolean isArray) {
    ClientIdentifier clientId = new ClientIdentifier(CLIENT_PUBLIC, CLIENT_SECRET);
    final String authUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("authorization").build().toString();
    final String accessTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("access-token").build().toString();
    final String refreshTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("refresh-token").build().toString();
    final String state = STATE;
    final Client client = ClientBuilder.newClient();
    if (isArray) {
        client.register(new ClientRequestFilter() {

            @Override
            public void filter(final ClientRequestContext requestContext) throws IOException {
                requestContext.getHeaders().putSingle("isArray", true);
            }
        });
    }
    final OAuth2CodeGrantFlow.Builder builder = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientId, authUri, accessTokenUri);
    final OAuth2CodeGrantFlow flow = builder.client(client).refreshTokenUri(refreshTokenUri).property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, "readOnly", "true").property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, OAuth2Parameters.STATE, state).scope("contact").build();
    final String finalAuthorizationUri = flow.start();
    final Response response = ClientBuilder.newClient().target(finalAuthorizationUri).request().get();
    assertEquals(200, response.getStatus());
    final String code = response.readEntity(String.class);
    assertEquals(CODE, code);
    final TokenResult result = flow.finish(code, state);
    assertEquals("access-token-aab999f", result.getAccessToken());
    assertEquals(new Long(3600), result.getExpiresIn());
    assertEquals("access-token", result.getTokenType());
    final TokenResult refreshResult = flow.refreshAccessToken(result.getRefreshToken());
    assertEquals("access-token-new", refreshResult.getAccessToken());
    assertEquals(new Long(3600), refreshResult.getExpiresIn());
    assertEquals("access-token", refreshResult.getTokenType());
    if (isArray) {
        final Collection<String> array = (Collection<String>) refreshResult.getAllProperties().get("access_token");
        assertThat(array.size(), is(1));
        assertThat(array, hasItem("access-token-new"));
    }
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientIdentifier(org.glassfish.jersey.client.oauth2.ClientIdentifier) TokenResult(org.glassfish.jersey.client.oauth2.TokenResult) OAuth2CodeGrantFlow(org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) Collection(java.util.Collection) Client(javax.ws.rs.client.Client)

Example 4 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project robozonky by RoboZonky.

the class AuthenticatedFilterTest method hasToken.

@Test
void hasToken() throws URISyntaxException {
    final ClientRequestContext crc = mock(ClientRequestContext.class);
    when(crc.getUri()).thenReturn(new URI("http://somewhere"));
    when(crc.getHeaders()).thenReturn(new MultivaluedHashMap<>());
    getTestedFilter().filter(crc);
    assertThat(crc.getHeaders().getFirst("Authorization")).isEqualTo("Bearer " + String.valueOf(AuthenticatedFilterTest.TOKEN.getAccessToken()));
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project robozonky by RoboZonky.

the class AuthenticatedFilterTest method changes400to401.

@Test
void changes400to401() throws IOException {
    final int expectedCode = 400;
    final ClientRequestContext ctx = mock(ClientRequestContext.class);
    final ClientResponseContext ctx2 = mock(ClientResponseContext.class);
    final ZonkyApiToken token = new ZonkyApiToken("", "", 299);
    when(ctx2.hasEntity()).thenReturn(true);
    when(ctx2.getHeaders()).thenReturn(new MultivaluedMapImpl<>());
    when(ctx2.getEntityStream()).thenReturn(c(token));
    when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode));
    when(ctx2.getStatus()).thenReturn(expectedCode);
    final RoboZonkyFilter filter = new AuthenticatedFilter(token);
    filter.filter(ctx, ctx2);
    verify(ctx2, times(1)).setStatus(401);
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Test(org.junit.jupiter.api.Test)

Aggregations

ClientRequestContext (javax.ws.rs.client.ClientRequestContext)30 Test (org.junit.Test)15 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)11 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)9 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)8 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)7 WebTarget (javax.ws.rs.client.WebTarget)7 Test (org.junit.jupiter.api.Test)7 URI (java.net.URI)6 Client (javax.ws.rs.client.Client)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 Message (org.apache.cxf.message.Message)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ProcessingException (javax.ws.rs.ProcessingException)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)3 VerifySignatureFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter)3 MockAlgorithmProvider (org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider)3