Search in sources :

Example 26 with ResponseProcessingException

use of javax.ws.rs.client.ResponseProcessingException in project verify-hub by alphagov.

the class AttributeQueryRequestClient method sendSingleQuery.

private Optional<Element> sendSingleQuery(Element serialisedQuery, String messageId, SessionId sessionId, URI matchingServiceUri) {
    try {
        externalCommunicationEventLogger.logMatchingServiceRequest(messageId, sessionId, matchingServiceUri);
        // Use a custom timer so that we get separate metrics for each matching service
        final String scope = matchingServiceUri.toString().replace(':', '_').replace('/', '_');
        // TODO: remove this timer once we're happy the prometheus Histogram works properly
        final Timer timer = metricsRegistry.timer(MetricRegistry.name(AttributeQueryRequestClient.class, "sendSingleQuery", scope));
        final Timer.Context context = timer.time();
        Summary.Timer prometheusTimer = msaRequestDuration.labels(matchingServiceUri.toString()).startTimer();
        try {
            return Optional.ofNullable(soapRequestClient.makeSoapRequest(serialisedQuery, matchingServiceUri));
        } finally {
            context.stop();
            prometheusTimer.observeDuration();
        }
    } catch (SOAPRequestError e) {
        if (e.getEntity().isPresent()) {
            final String responseBody = e.getEntity().get();
            LOG.info(format("Error received from MSA (URI ''{0}'') following HTTP response {1}; response body:\n{2}", matchingServiceUri, e.getResponseStatus(), responseBody));
            // to split out these errors. See https://trello.com/c/N7edPMiO/
            if (responseBody.startsWith("uk.gov.ida.exceptions.ApplicationException")) {
                throw new MatchingServiceException(format("Unknown internal Matching Service error from {0} with status {1} ", matchingServiceUri, e.getResponseStatus()), e);
            }
        }
        throw new MatchingServiceException(format("Matching Service response from {0} was status {1}", matchingServiceUri, e.getResponseStatus()), e);
    } catch (ResponseProcessingException e) {
        LOG.error(format("Matching service attribute query to {0} failed during response processing ({1})", matchingServiceUri, e.getMessage()), e);
        throw new MatchingServiceException("Request to Matching Service Failed during response processing", e);
    } catch (ProcessingException e) {
        LOG.error(format("Matching service attribute query to {0} experienced a connection level failure ({1})", matchingServiceUri, e.getMessage()), e);
        throw new MatchingServiceException("Request to Matching Service Failed At Http Layer", e);
    } catch (Exception e) {
        throw new MatchingServiceException(format("The matching service attribute query to {0} failed for an unknown reason ({1}).", matchingServiceUri, e.getMessage()), e);
    }
}
Also used : Timer(com.codahale.metrics.Timer) Summary(io.prometheus.client.Summary) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

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