Search in sources :

Example 1 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project Hystrix by Netflix.

the class HystricsMetricsControllerTest method testConcurrency.

@Test
public void testConcurrency() throws Exception {
    // Execute a Hystrix command so that metrics are initialized.
    executeHystrixCommand();
    List<EventInput> streamList = new ArrayList<EventInput>();
    // Fire 5 requests, validate their responses and hold these connections.
    for (int i = 0; i < 5; i++) {
        EventInput stream = getStream();
        System.out.println("Received Response for Request#" + (i + 1));
        streamList.add(stream);
        validateStream(stream, 1000);
        System.out.println("Validated Response#" + (i + 1));
    }
    // Sixth request should fail since max configured connection is 5.
    try {
        streamList.add(getStreamFailFast());
        Assert.fail("Expected 'ServiceUnavailableException' but, request went through.");
    } catch (ServiceUnavailableException e) {
        System.out.println("Got ServiceUnavailableException as expected.");
    }
    // Close one of the connections
    streamList.get(0).close();
    // Try again after closing one of the connections. This request should go through.
    EventInput eventInput = getStream();
    streamList.add(eventInput);
    validateStream(eventInput, 1000);
}
Also used : EventInput(org.glassfish.jersey.media.sse.EventInput) ArrayList(java.util.ArrayList) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project jersey by jersey.

the class JerseyInvocation method convertToException.

private ProcessingException convertToException(final Response response) {
    try {
        // Buffer and close entity input stream (if any) to prevent
        // leaking connections (see JERSEY-2157).
        response.bufferEntity();
        final WebApplicationException webAppException;
        final int statusCode = response.getStatus();
        final Response.Status status = Response.Status.fromStatusCode(statusCode);
        if (status == null) {
            final Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
            webAppException = createExceptionForFamily(response, statusFamily);
        } else {
            switch(status) {
                case BAD_REQUEST:
                    webAppException = new BadRequestException(response);
                    break;
                case UNAUTHORIZED:
                    webAppException = new NotAuthorizedException(response);
                    break;
                case FORBIDDEN:
                    webAppException = new ForbiddenException(response);
                    break;
                case NOT_FOUND:
                    webAppException = new NotFoundException(response);
                    break;
                case METHOD_NOT_ALLOWED:
                    webAppException = new NotAllowedException(response);
                    break;
                case NOT_ACCEPTABLE:
                    webAppException = new NotAcceptableException(response);
                    break;
                case UNSUPPORTED_MEDIA_TYPE:
                    webAppException = new NotSupportedException(response);
                    break;
                case INTERNAL_SERVER_ERROR:
                    webAppException = new InternalServerErrorException(response);
                    break;
                case SERVICE_UNAVAILABLE:
                    webAppException = new ServiceUnavailableException(response);
                    break;
                default:
                    final Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
                    webAppException = createExceptionForFamily(response, statusFamily);
            }
        }
        return new ResponseProcessingException(response, webAppException);
    } catch (final Throwable t) {
        return new ResponseProcessingException(response, LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
    }
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) WebApplicationException(javax.ws.rs.WebApplicationException) NotAllowedException(javax.ws.rs.NotAllowedException) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Response(javax.ws.rs.core.Response) NotAcceptableException(javax.ws.rs.NotAcceptableException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) NotSupportedException(javax.ws.rs.NotSupportedException)

Example 3 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project atlasdb by palantir.

the class RsErrorDecoder method decode.

@Override
public RuntimeException decode(String methodKey, feign.Response feignResponse) {
    try {
        Response response = convertResponseToRs(feignResponse);
        int statusCode = response.getStatus();
        Response.Status status = Response.Status.fromStatusCode(statusCode);
        if (status == null) {
            Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
            return createExceptionForFamily(response, statusFamily);
        } else {
            switch(status) {
                case BAD_REQUEST:
                    return new BadRequestException(response);
                case UNAUTHORIZED:
                    return new NotAuthorizedException(response);
                case FORBIDDEN:
                    return new ForbiddenException(response);
                case NOT_FOUND:
                    return new NotFoundException(response);
                case METHOD_NOT_ALLOWED:
                    return new NotAllowedException(response);
                case NOT_ACCEPTABLE:
                    return new NotAcceptableException(response);
                case UNSUPPORTED_MEDIA_TYPE:
                    return new NotSupportedException(response);
                case INTERNAL_SERVER_ERROR:
                    return new InternalServerErrorException(response);
                case SERVICE_UNAVAILABLE:
                    return new ServiceUnavailableException(response);
                default:
                    Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
                    return createExceptionForFamily(response, statusFamily);
            }
        }
    } catch (Throwable t) {
        return new RuntimeException("Failed to convert response to exception", t);
    }
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) NotAllowedException(javax.ws.rs.NotAllowedException) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Response(javax.ws.rs.core.Response) NotAcceptableException(javax.ws.rs.NotAcceptableException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotSupportedException(javax.ws.rs.NotSupportedException)

Example 4 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project dataverse by IQSS.

the class Meta method datafile.

@Path("datafile/{fileId}")
@GET
@Produces({ "text/xml" })
public String datafile(@PathParam("fileId") Long fileId, @QueryParam("exclude") String exclude, @QueryParam("include") String include, @Context HttpHeaders header, @Context HttpServletResponse response) throws NotFoundException, ServiceUnavailableException /*, PermissionDeniedException, AuthorizationRequiredException*/
{
    String retValue = "";
    DataFile dataFile = null;
    // httpHeaders.add("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
    // httpHeaders.add("Content-Type", "application/zip; name=\"dataverse_files.zip\"");
    response.setHeader("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
    dataFile = datafileService.find(fileId);
    if (dataFile == null) {
        throw new NotFoundException();
    }
    String fileName = dataFile.getFileMetadata().getLabel().replaceAll("\\.tab$", "-ddi.xml");
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
    response.setHeader("Content-Type", "application/xml; name=\"" + fileName + "\"");
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataFile(fileId, outStream, exclude, include);
        retValue = outStream.toString();
    } catch (Exception e) {
        // We return Service Unavailable.
        throw new ServiceUnavailableException();
    }
    response.setHeader("Access-Control-Allow-Origin", "*");
    return retValue;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) NotFoundException(javax.ws.rs.NotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project dataverse by IQSS.

the class Access method dvCardImage.

@Path("dvCardImage/{dataverseId}")
@GET
@Produces({ "image/png" })
public InputStream dvCardImage(@PathParam("dataverseId") Long dataverseId, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    logger.fine("entering dvCardImage");
    Dataverse dataverse = dataverseService.find(dataverseId);
    if (dataverse == null) {
        logger.warning("Preview: Version service could not locate a DatasetVersion object for id " + dataverseId + "!");
        return null;
    }
    String imageThumbFileName = null;
    if (dataverse.getDataverseTheme() != null && dataverse.getDataverseTheme().getLogo() != null && !dataverse.getDataverseTheme().getLogo().equals("")) {
        File dataverseLogoFile = getLogo(dataverse);
        if (dataverseLogoFile != null) {
            logger.fine("dvCardImage: logo file found");
            String logoThumbNailPath = null;
            InputStream in = null;
            try {
                if (dataverseLogoFile.exists()) {
                    logoThumbNailPath = ImageThumbConverter.generateImageThumbnailFromFile(dataverseLogoFile.getAbsolutePath(), 48);
                    if (logoThumbNailPath != null) {
                        in = new FileInputStream(logoThumbNailPath);
                    }
                }
            } catch (Exception ex) {
                in = null;
            }
            if (in != null) {
                logger.fine("dvCardImage: successfully obtained thumbnail for dataverse logo.");
                return in;
            }
        }
    }
    /*
        StorageIO thumbnailDataAccess = null; 
        
        if (!dataverse.isHarvested()) {
            for (Dataset dataset : datasetService.findPublishedByOwnerId(dataverseId)) {
                logger.info("dvCardImage: checking dataset "+dataset.getGlobalId());
                if (dataset != null) {
                    DatasetVersion releasedVersion = dataset.getReleasedVersion();
                    logger.info("dvCardImage: obtained released version "+releasedVersion.getTitle());
                    thumbnailDataAccess = getThumbnailForDatasetVersion(releasedVersion); 
                    if (thumbnailDataAccess != null) {
                        logger.info("dvCardImage: obtained thumbnail for the version.");
                        break;
                    }
                }
            }
        }
        
        if (thumbnailDataAccess != null && thumbnailDataAccess.getInputStream() != null) {
            return thumbnailDataAccess.getInputStream();
        }
        */
    return null;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Dataverse(edu.harvard.iq.dataverse.Dataverse) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) FileInputStream(java.io.FileInputStream) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)18 GET (javax.ws.rs.GET)10 NotFoundException (javax.ws.rs.NotFoundException)10 Path (javax.ws.rs.Path)10 Produces (javax.ws.rs.Produces)10 BadRequestException (javax.ws.rs.BadRequestException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ForbiddenException (javax.ws.rs.ForbiddenException)6 DataFile (edu.harvard.iq.dataverse.DataFile)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 IOException (java.io.IOException)4 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)3 HttpResponse (co.cask.common.http.HttpResponse)2 NotAcceptableException (javax.ws.rs.NotAcceptableException)2 NotAllowedException (javax.ws.rs.NotAllowedException)2 NotSupportedException (javax.ws.rs.NotSupportedException)2 Response (javax.ws.rs.core.Response)2 EventOutput (org.glassfish.jersey.media.sse.EventOutput)2 Test (org.junit.Test)2