Search in sources :

Example 36 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project pulsar by apache.

the class BaseResource method getApiException.

public PulsarAdminException getApiException(Throwable e) {
    if (e instanceof PulsarAdminException) {
        return (PulsarAdminException) e;
    } else if (e instanceof ServiceUnavailableException) {
        if (e.getCause() instanceof java.net.ConnectException) {
            return new ConnectException(e.getCause());
        } else {
            ServerErrorException see = (ServerErrorException) e;
            int statusCode = see.getResponse().getStatus();
            String httpError = getReasonFromServer(see);
            return new PulsarAdminException(e, httpError, statusCode);
        }
    } else if (e instanceof WebApplicationException) {
        // Handle 5xx exceptions
        if (e instanceof ServerErrorException) {
            ServerErrorException see = (ServerErrorException) e;
            int statusCode = see.getResponse().getStatus();
            String httpError = getReasonFromServer(see);
            return new ServerSideErrorException(see, e.getMessage(), httpError, statusCode);
        } else if (e instanceof ClientErrorException) {
            // Handle 4xx exceptions
            ClientErrorException cee = (ClientErrorException) e;
            int statusCode = cee.getResponse().getStatus();
            String httpError = getReasonFromServer(cee);
            switch(statusCode) {
                case 401:
                case 403:
                    return new NotAuthorizedException(cee, httpError, statusCode);
                case 404:
                    return new NotFoundException(cee, httpError, statusCode);
                case 405:
                    return new NotAllowedException(cee, httpError, statusCode);
                case 409:
                    return new ConflictException(cee, httpError, statusCode);
                case 412:
                    return new PreconditionFailedException(cee, httpError, statusCode);
                default:
                    return new PulsarAdminException(httpError, cee, httpError, statusCode);
            }
        } else {
            WebApplicationException wae = (WebApplicationException) e;
            int statusCode = wae.getResponse().getStatus();
            String httpError = getReasonFromServer(wae);
            return new PulsarAdminException(httpError, wae, httpError, statusCode);
        }
    } else {
        return new PulsarAdminException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) NotAllowedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAllowedException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) ServerSideErrorException(org.apache.pulsar.client.admin.PulsarAdminException.ServerSideErrorException) ClientErrorException(javax.ws.rs.ClientErrorException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ServerErrorException(javax.ws.rs.ServerErrorException) ConnectException(org.apache.pulsar.client.admin.PulsarAdminException.ConnectException)

Example 37 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.

the class JaxRsCircuitBreakerImplTest method validateCircuitBreakerOpensTheCircuitWhenServiceUnavailableExceptionIsThrownAsync.

@Test
public void validateCircuitBreakerOpensTheCircuitWhenServiceUnavailableExceptionIsThrownAsync() {
    List<Future<Response>> futures = new ArrayList<>();
    for (int i = 0; i < MIN_NUM_CALLS + 2; i++) {
        Supplier<Future<Response>> clientCall = circuitBreaker.decorateFuture(() -> {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return responseFutureUnavailable;
        });
        Future<Response> responseFuture = clientCall.get();
        futures.add(responseFuture);
    }
    int callCount = 0;
    int unavailableCount = 0;
    for (Future<Response> responseFuture : futures) {
        try {
            callCount++;
            Response response = responseFuture.get();
            System.out.println(response.getStatus());
        } catch (CallNotAllowedException e) {
            break;
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } catch (ServiceUnavailableException e) {
            unavailableCount++;
        }
    }
    assertTrue(callCount >= MIN_NUM_CALLS);
    assertTrue(unavailableCount >= MIN_NUM_CALLS);
    assertEquals(CircuitBreaker.State.OPEN, circuitBreaker.getInternalCircuitBreaker().getState());
    assertEquals(CircuitBreakerState.OPEN, circuitBreaker.getState());
}
Also used : ArrayList(java.util.ArrayList) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CallNotAllowedException(com.oracle.bmc.circuitbreaker.CallNotAllowedException) Response(javax.ws.rs.core.Response) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 38 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.

the class JaxRsCircuitBreakerImplTest method setup.

@Before
public void setup() throws ExecutionException, InterruptedException {
    CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).permittedNumberOfCallsInHalfOpenState(4).slidingWindowSize(SLIDING_WINDOW_SIZE).minimumNumberOfCalls(MIN_NUM_CALLS).waitDurationInOpenState(Duration.ofSeconds(2)).build();
    circuitBreaker = new JaxRsCircuitBreakerImpl(config);
    Response response503 = mock(Response.class);
    Mockito.when(response503.getStatus()).thenReturn(503);
    Mockito.when(response503.getStatusInfo()).thenReturn(Response.Status.BAD_GATEWAY);
    invocation503 = mock(Invocation.class);
    Mockito.when(invocation503.invoke()).thenReturn(response503);
    Mockito.when(responseFuture503.get()).thenReturn(response503);
    Response response200 = mock(Response.class);
    Mockito.when(response200.getStatus()).thenReturn(200);
    Mockito.when(response200.getStatusInfo()).thenReturn(Response.Status.OK);
    invocation200 = mock(Invocation.class);
    Mockito.when(invocation200.invoke()).thenReturn(response200);
    Mockito.when(responseFuture200.get()).thenReturn(response200);
    invocationUnavailable = mock(Invocation.class);
    Mockito.when(invocationUnavailable.invoke()).thenThrow(new ServiceUnavailableException());
    invocationClientError = mock(Invocation.class);
    Mockito.when(invocationClientError.invoke()).thenThrow(new ClientErrorException(Response.Status.NOT_ACCEPTABLE));
    Mockito.when(responseFuture200.get()).thenReturn(response200);
    Mockito.when(responseFuture503.get()).thenReturn(response503);
    Mockito.when(responseFutureUnavailable.get()).thenThrow(new ServiceUnavailableException());
    Response response409 = mock(Response.class);
    Mockito.when(response409.getStatus()).thenReturn(409);
    Mockito.when(response409.getStatusInfo()).thenReturn(buildIncorrectStateResponse());
    invocation409 = mock(Invocation.class);
    Mockito.when(invocation409.invoke()).thenReturn(response409);
    Mockito.when(responseFuture409.get()).thenReturn(response409);
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ClientErrorException(javax.ws.rs.ClientErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) Before(org.junit.Before)

Example 39 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.

the class CircuitBreakerConfigurationTest method validateCircuitBreakerConfigurationWithCustomValues.

@Test
public void validateCircuitBreakerConfigurationWithCustomValues() {
    CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).slowCallRateThreshold(90).slowCallDurationThreshold(Duration.ofSeconds(6)).permittedNumberOfCallsInHalfOpenState(2).slidingWindowSize(10).minimumNumberOfCalls(4).waitDurationInOpenState(Duration.ofSeconds(2)).recordHttpStatuses(ImmutableSet.of(CircuitBreakerConfiguration.TOO_MANY_REQUESTS, CircuitBreakerConfiguration.SERVICE_UNAVAILABLE)).recordExceptions(ImmutableList.of(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE_EXCEPTION_CLASS)).build();
    JaxRsCircuitBreakerImpl circuitBreaker = new JaxRsCircuitBreakerImpl(config);
    CircuitBreakerConfig internalConfig = circuitBreaker.getInternalCircuitBreakerConfig();
    assertEquals(internalConfig.getFailureRateThreshold(), 50, 0.1);
    assertEquals(internalConfig.getSlowCallRateThreshold(), 90, 0.1);
    assertEquals(internalConfig.getSlowCallDurationThreshold(), Duration.ofSeconds(6));
    assertEquals(internalConfig.getPermittedNumberOfCallsInHalfOpenState(), 2);
    assertEquals(internalConfig.getSlidingWindowType(), CircuitBreakerConfig.SlidingWindowType.TIME_BASED);
    assertEquals(internalConfig.getSlidingWindowSize(), 10);
    assertEquals(internalConfig.getMinimumNumberOfCalls(), 4);
    assertEquals(internalConfig.getWaitDurationInOpenState(), Duration.ofSeconds(2));
    assertTrue(internalConfig.isAutomaticTransitionFromOpenToHalfOpenEnabled());
    Response response503 = mock(Response.class);
    Mockito.when(response503.getStatus()).thenReturn(503);
    HttpStatusErrorException ex = new HttpStatusErrorException(response503);
    Predicate<Throwable> recordExceptionPredicate = internalConfig.getRecordExceptionPredicate();
    assertTrue(recordExceptionPredicate.test(ex));
    assertTrue(recordExceptionPredicate.test(new ServiceUnavailableException()));
    assertFalse(recordExceptionPredicate.test(new InternalServerErrorException()));
    assertFalse(recordExceptionPredicate.test(new ProcessingException("test")));
    Set<Integer> recordHttpStatuses = circuitBreaker.getRecordHttpStatuses();
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE));
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.TOO_MANY_REQUESTS));
    assertEquals(recordHttpStatuses.size(), 2);
}
Also used : Response(javax.ws.rs.core.Response) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 40 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project Europeana-Cloud by europeana.

the class DpsClient method manageResponse.

private <T> T manageResponse(ResponseParams<T> responseParameters, Supplier<Response> responseSupplier, String errorMessage) throws DpsException {
    Response response = responseSupplier.get();
    try {
        response.bufferEntity();
        if (responseParameters.isStatusCodeValid(response.getStatus())) {
            return readEntityByClass(responseParameters, response);
        } else if (response.getStatus() == HttpURLConnection.HTTP_UNAVAILABLE) {
            throw DPSExceptionProvider.createException(errorMessage, "Service unavailable", new ServiceUnavailableException());
        } else if (response.getStatus() == HttpURLConnection.HTTP_NOT_FOUND) {
            throw DPSExceptionProvider.createException(errorMessage, "Endpoint not found", new NotFoundException());
        }
        ErrorInfo errorInfo = response.readEntity(ErrorInfo.class);
        throw DPSExceptionProvider.createException(errorInfo);
    } catch (DpsException | DPSClientException dpsException) {
        // re-throw just created DpsException
        throw dpsException;
    } catch (ProcessingException processingException) {
        String message = String.format("Could not deserialize response with statusCode: %d; message: %s", response.getStatus(), response.readEntity(String.class));
        throw DPSExceptionProvider.createException(errorMessage, message, processingException);
    } catch (Exception otherExceptions) {
        throw DPSExceptionProvider.createException(errorMessage, "Other exception", otherExceptions);
    } finally {
        closeResponse(response);
    }
}
Also used : Response(javax.ws.rs.core.Response) DpsException(eu.europeana.cloud.service.dps.exception.DpsException) ErrorInfo(eu.europeana.cloud.common.response.ErrorInfo) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) DPSClientException(eu.europeana.cloud.service.dps.exception.DPSClientException) DpsException(eu.europeana.cloud.service.dps.exception.DpsException) DPSClientException(eu.europeana.cloud.service.dps.exception.DPSClientException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)40 NotFoundException (javax.ws.rs.NotFoundException)16 GET (javax.ws.rs.GET)13 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13 WebApplicationException (javax.ws.rs.WebApplicationException)13 BadRequestException (javax.ws.rs.BadRequestException)12 Response (javax.ws.rs.core.Response)12 ForbiddenException (javax.ws.rs.ForbiddenException)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 DataFile (edu.harvard.iq.dataverse.DataFile)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 ClientErrorException (javax.ws.rs.ClientErrorException)7 ServerErrorException (javax.ws.rs.ServerErrorException)7 Test (org.junit.Test)6 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)4 RedirectionException (javax.ws.rs.RedirectionException)4 CircuitBreakerConfiguration (com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration)3 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)3