Search in sources :

Example 26 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project cdap by caskdata.

the class RuntimeMonitor method run.

@Override
protected void run() {
    runThread = Thread.currentThread();
    try {
        while (!stopped) {
            try {
                if (topicsToRequest.isEmpty()) {
                    initializeTopics();
                }
                HttpResponse response = restClient.execute(HttpRequest.builder(HttpMethod.POST, clientConfig.resolveURL("runtime/metadata")).withBody(GSON.toJson(topicsToRequest)).build());
                if (response.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
                    throw new ServiceUnavailableException(response.getResponseBodyAsString());
                }
                Map<String, List<MonitorMessage>> monitorResponses = GSON.fromJson(response.getResponseBodyAsString(StandardCharsets.UTF_8), MAP_STRING_MESSAGE_TYPE);
                processResponse(monitorResponses);
            } catch (Exception e) {
                OUTAGE_LOG.warn("Failed to fetch monitoring data from program {}, run {}. Will be retried in next iteration.", programId.getProgram(), programId.getRun(), e);
            }
            Thread.sleep(pollTimeMillis);
        }
    } catch (InterruptedException e) {
    // Interruption means stopping the service.
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) List(java.util.List) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException)

Example 27 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project cdap by caskdata.

the class RuntimeMonitor method initializeTopics.

private void initializeTopics() throws Exception {
    Set<String> topicsToMonitor = new HashSet<>();
    topicsToMonitor.addAll(Arrays.asList(cConf.get(Constants.RuntimeMonitor.TOPICS).split(",")));
    HttpResponse response = restClient.execute(HttpMethod.POST, clientConfig.resolveURL("runtime/monitor/topics"), GSON.toJson(topicsToMonitor), Collections.emptyMap(), null);
    if (response.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
        throw new ServiceUnavailableException(response.getResponseBodyAsString());
    }
    Map<String, String> configToTopic = GSON.fromJson(response.getResponseBodyAsString(StandardCharsets.UTF_8), MAP_STRING_STRING_TYPE);
    // TODO initialize from offset table for a given programId
    for (Map.Entry<String, String> entry : configToTopic.entrySet()) {
        topicsToRequest.put(entry.getKey(), new MonitorConsumeRequest(entry.getValue(), entry.getKey(), null, limit));
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 28 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project incubator-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, httpError, 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 29 with ServiceUnavailableException

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

the class Access method tabularDatafileMetadataDDI.

/* 
     * This has been moved here, under /api/access, from the /api/meta hierarchy
     * which we are going to retire.
     */
@Path("datafile/{fileId}/metadata/ddi")
@GET
@Produces({ "text/xml" })
public String tabularDatafileMetadataDDI(@PathParam("fileId") String fileId, @QueryParam("fileMetadataId") Long fileMetadataId, @QueryParam("exclude") String exclude, @QueryParam("include") String include, @Context HttpHeaders header, @Context HttpServletResponse response) throws NotFoundException, ServiceUnavailableException /*, PermissionDeniedException, AuthorizationRequiredException*/
{
    String retValue = "";
    DataFile dataFile = null;
    dataFile = findDataFileOrDieWrapper(fileId);
    if (!dataFile.isTabularData()) {
        throw new BadRequestException("tabular data required");
    }
    if (dataFile.isRestricted() || FileUtil.isActivelyEmbargoed(dataFile)) {
        boolean hasPermissionToDownloadFile = false;
        DataverseRequest dataverseRequest;
        try {
            dataverseRequest = createDataverseRequest(findUserOrDie());
        } catch (WrappedResponse ex) {
            throw new BadRequestException("cannot find user");
        }
        if (dataverseRequest != null && dataverseRequest.getUser() instanceof GuestUser) {
            // We must be in the UI. Try to get a non-GuestUser from the session.
            dataverseRequest = dvRequestService.getDataverseRequest();
        }
        hasPermissionToDownloadFile = permissionService.requestOn(dataverseRequest, dataFile).has(Permission.DownloadFile);
        if (!hasPermissionToDownloadFile) {
            throw new BadRequestException("no permission to download file");
        }
    }
    response.setHeader("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
    FileMetadata fm = null;
    if (fileMetadataId == null) {
        fm = dataFile.getFileMetadata();
    } else {
        fm = dataFileService.findFileMetadata(fileMetadataId);
    }
    String fileName = fm.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();
    Long dataFileId = dataFile.getId();
    try {
        ddiExportService.exportDataFile(dataFileId, outStream, exclude, include, fileMetadataId);
        retValue = outStream.toString();
    } catch (Exception e) {
        // We return Service Unavailable.
        throw new ServiceUnavailableException();
    }
    return retValue;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) BadRequestException(javax.ws.rs.BadRequestException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) RedirectionException(javax.ws.rs.RedirectionException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) ServerErrorException(javax.ws.rs.ServerErrorException) URISyntaxException(java.net.URISyntaxException) BadRequestException(javax.ws.rs.BadRequestException) FileNotFoundException(java.io.FileNotFoundException) 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 30 with ServiceUnavailableException

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

the class Meta method dataset.

@Deprecated
@Path("dataset/{datasetId}")
@GET
@Produces({ "application/xml" })
public String dataset(@PathParam("datasetId") Long datasetId, @QueryParam("exclude") String exclude, @QueryParam("include") String include, @Context HttpHeaders header, @Context HttpServletResponse response) throws NotFoundException /*, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    Dataset dataset = datasetService.find(datasetId);
    if (dataset == null) {
        throw new NotFoundException();
    }
    String retValue = "";
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataset(datasetId, outStream, exclude, include);
        retValue = outStream.toString();
    } catch (Exception e) {
        // metadata record requested. We simply return an empty string.
        throw new ServiceUnavailableException();
    }
    return retValue;
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) 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)

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