Search in sources :

Example 11 with ResponseProcessingException

use of javax.ws.rs.client.ResponseProcessingException in project cxf by apache.

the class AuthorizationGrantNegativeTest method testRepeatAuthorizationCode.

// Send the authorization code twice to get an access token
@org.junit.Test
public void testRepeatAuthorizationCode() throws Exception {
    URL busFile = AuthorizationGrantTest.class.getResource("client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = OAuth2TestUtils.getAuthorizationCode(client);
    assertNotNull(code);
    // Now get the access token
    client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    client.type("application/x-www-form-urlencoded").accept("application/json");
    client.path("token");
    // First invocation
    Form form = new Form();
    form.param("grant_type", "authorization_code");
    form.param("code", code);
    form.param("client_id", "consumer-id");
    Response response = client.post(form);
    ClientAccessToken token = response.readEntity(ClientAccessToken.class);
    assertNotNull(token.getTokenKey());
    // Now try to get a second token
    response = client.post(form);
    try {
        response.readEntity(ClientAccessToken.class);
        fail("Failure expected on trying to get a second access token");
    } catch (ResponseProcessingException ex) {
    // expected
    }
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 12 with ResponseProcessingException

use of javax.ws.rs.client.ResponseProcessingException in project cxf by apache.

the class OIDCNegativeTest method testJWTRequestNonmatchingClientId.

@org.junit.Test
public void testJWTRequestNonmatchingClientId() throws Exception {
    URL busFile = OIDCNegativeTest.class.getResource("client.xml");
    String address = "https://localhost:" + PORT + "/unsignedjwtservices/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    JwtClaims claims = new JwtClaims();
    claims.setIssuer("consumer-id");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(Collections.singletonList("https://localhost:" + PORT + "/unsignedjwtservices/"));
    claims.setProperty("client_id", "consumer-id2");
    JwsHeaders headers = new JwsHeaders();
    headers.setAlgorithm("none");
    JwtToken token = new JwtToken(headers, claims);
    JwsJwtCompactProducer jws = new JwsJwtCompactProducer(token);
    String request = jws.getSignedEncodedJws();
    AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
    parameters.setConsumerId("consumer-id");
    parameters.setScope("openid");
    parameters.setResponseType("code");
    parameters.setPath("authorize/");
    parameters.setRequest(request);
    // Get Authorization Code
    try {
        OAuth2TestUtils.getLocation(client, parameters);
        fail("Failure expected on a non-matching client id");
    } catch (ResponseProcessingException ex) {
    // expected
    }
}
Also used : JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsJwtCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) AuthorizationCodeParameters(org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuth2TestUtils.AuthorizationCodeParameters) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 13 with ResponseProcessingException

use of javax.ws.rs.client.ResponseProcessingException in project cxf by apache.

the class OIDCNegativeTest method testJWTRequestNonmatchingResponseType.

@org.junit.Test
public void testJWTRequestNonmatchingResponseType() throws Exception {
    URL busFile = OIDCNegativeTest.class.getResource("client.xml");
    String address = "https://localhost:" + PORT + "/unsignedjwtservices/";
    WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    JwtClaims claims = new JwtClaims();
    claims.setIssuer("consumer-id");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(Collections.singletonList("https://localhost:" + PORT + "/unsignedjwtservices/"));
    claims.setProperty("response_type", "token");
    JwsHeaders headers = new JwsHeaders();
    headers.setAlgorithm("none");
    JwtToken token = new JwtToken(headers, claims);
    JwsJwtCompactProducer jws = new JwsJwtCompactProducer(token);
    String request = jws.getSignedEncodedJws();
    AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
    parameters.setConsumerId("consumer-id");
    parameters.setScope("openid");
    parameters.setResponseType("code");
    parameters.setPath("authorize/");
    parameters.setRequest(request);
    // Get Authorization Code
    try {
        OAuth2TestUtils.getLocation(client, parameters);
        fail("Failure expected on a non-matching response_type");
    } catch (ResponseProcessingException ex) {
    // expected
    }
}
Also used : JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsJwtCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) AuthorizationCodeParameters(org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuth2TestUtils.AuthorizationCodeParameters) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 14 with ResponseProcessingException

use of javax.ws.rs.client.ResponseProcessingException in project stdlib by petergeneric.

the class RemoteExceptionClientResponseFilter method filter.

@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) throws IOException {
    final int code = responseContext.getStatus();
    String operationId;
    if (Tracing.isVerbose()) {
        operationId = requestContext.getHeaderString(TracingConstants.HTTP_HEADER_CORRELATION_ID);
        if (operationId != null)
            Tracing.logOngoing(operationId, "HTTP:resp", () -> "" + code);
        else
            // can't find outgoing trace id
            operationId = Tracing.log("HTTP:resp:unexpected", () -> "" + code);
    } else {
        operationId = null;
    }
    if (code >= 200 && code <= 299)
        // Do not run if the return code is 2xx
        return;
    if (responseContext.getHeaders().containsKey(RestThrowableConstants.HEADER_RICH_EXCEPTION)) {
        try {
            final InputStream is = responseContext.getEntityStream();
            RestFailure failure;
            if (tryParseLegacyExceptionNamespace) {
                // If parsing the legacy namespace fails, throw the original parse error back to the client
                try {
                    // Mark the start of the stream so we can reset back to it if we need to process this as a legacy exception
                    is.mark(Integer.MAX_VALUE);
                    failure = parseResponse(is);
                } catch (JAXBUnmarshalException e) {
                    log.trace("Error parsing rich exception response, will fall back on parsing it as a legacy exception XML", e);
                    try {
                        failure = parseLegacyResponse(is);
                    } catch (Throwable legacyFailure) {
                        log.trace("Error parsing rich exception response as legacy rich exception XML!", legacyFailure);
                        // throw the original exception
                        throw e;
                    }
                }
            } else {
                failure = parseResponse(is);
            }
            if (Tracing.isVerbose() && failure != null && failure.exception != null) {
                final ExceptionInfo ei = failure.exception;
                Tracing.logOngoing(operationId, "HTTP:error", () -> ei.shortName + " " + ei.detail);
            }
            RestException exception = exceptionFactory.build(failure, responseContext);
            // Try to shorten the stack trace
            exception.fillInStackTrace();
            throw exception;
        } catch (ResponseProcessingException e) {
            throw e;
        } catch (Throwable e) {
            throw new ResponseProcessingException(null, "Error mapping exception from thrown from " + requestContext.getUri() + " to exception!", e);
        }
    }
}
Also used : JAXBUnmarshalException(org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException) RestFailure(com.peterphi.std.guice.restclient.jaxb.RestFailure) InputStream(java.io.InputStream) RestException(com.peterphi.std.guice.restclient.exception.RestException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ExceptionInfo(com.peterphi.std.guice.restclient.jaxb.ExceptionInfo)

Example 15 with ResponseProcessingException

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

the class EmptyEntityTest method testReceiveEmptyJAXBElement.

@Test
public void testReceiveEmptyJAXBElement() {
    WebTarget target = target("empty/getempty");
    final Response response = target.request("application/xml").get();
    assertEquals(200, response.getStatus());
    try {
        response.readEntity(new GenericType<JAXBElement<String>>() {
        });
        fail("ProcessingException expected.");
    } catch (ProcessingException ex) {
        assertSame(NoContentException.class, ex.getCause().getClass());
    }
    try {
        target.request("application/xml").get(new GenericType<JAXBElement<String>>() {
        });
        fail("ResponseProcessingException expected.");
    } catch (ResponseProcessingException ex) {
        assertSame(NoContentException.class, ex.getCause().getClass());
    }
}
Also used : Response(javax.ws.rs.core.Response) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) WebTarget(javax.ws.rs.client.WebTarget) JAXBElement(javax.xml.bind.JAXBElement) NoContentException(javax.ws.rs.core.NoContentException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) Test(org.junit.Test)

Aggregations

ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)26 Response (javax.ws.rs.core.Response)16 ProcessingException (javax.ws.rs.ProcessingException)10 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 URL (java.net.URL)8 WebTarget (javax.ws.rs.client.WebTarget)8 Test (org.junit.Test)8 Form (javax.ws.rs.core.Form)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 NoContentException (javax.ws.rs.core.NoContentException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ReaderInputStream (org.apache.cxf.io.ReaderInputStream)4 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)4 PushbackInputStream (java.io.PushbackInputStream)2 BadRequestException (javax.ws.rs.BadRequestException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 JwsHeaders (org.apache.cxf.rs.security.jose.jws.JwsHeaders)2 JwsJwtCompactProducer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer)2 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)2