private static NotFoundException analysisNotFoundException(String analysis) { return new NotFoundException(format("Analysis '%s' not found", analysis)); } }
/** * @throws NotFoundException if the value if null * @return the value */ public static <T> T checkFound(@Nullable T value, String message, Object... messageArguments) { if (value == null) { throw new NotFoundException(format(message, messageArguments)); } return value; }
/** * @throws NotFoundException if the value is not present * @return the value */ public static <T> T checkFoundWithOptional(Optional<T> value, String message, Object... messageArguments) { if (!value.isPresent()) { throw new NotFoundException(format(message, messageArguments)); } return value.get(); }
public static <T> T checkFoundWithOptional(java.util.Optional<T> value, String message, Object... messageArguments) { if (!value.isPresent()) { throw new NotFoundException(format(message, messageArguments)); } return value.get(); }
File getFile(String filename) { try { File input = new File(batchDir, filename); if (!input.exists() || !FileUtils.directoryContains(batchDir, input)) { throw new NotFoundException("Bad filename: " + filename); } return input; } catch (IOException e) { throw new IllegalStateException("Can get file " + filename, e); } } }
private ProfileExporter findExporter(String exporterKey) { for (ProfileExporter e : exporters) { if (exporterKey.equals(e.getKey())) { return e; } } throw new NotFoundException("Unknown quality profile exporter: " + exporterKey); }
private List<MetricDto> searchMetrics(DbSession dbSession, ComponentRequest request) { List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetricKeys()); if (metrics.size() < request.getMetricKeys().size()) { List<String> foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set<String> missingMetricKeys = Sets.difference( new LinkedHashSet<>(request.getMetricKeys()), new LinkedHashSet<>(foundMetricKeys)); throw new NotFoundException(format("The following metric keys are not found: %s", Joiner.on(", ").join(missingMetricKeys))); } return metrics; }
private static ComponentDto checkComponent(Optional<ComponentDto> componentDto, String message, Object... messageArguments) { if (componentDto.isPresent() && componentDto.get().isEnabled() && componentDto.get().getMainBranchProjectUuid() == null) { return componentDto.get(); } throw new NotFoundException(format(message, messageArguments)); }
private OrganizationDto getDto(DbSession dbSession, String key) { Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByKey(dbSession, key); if (!organizationDto.isPresent()) { throw new NotFoundException(format("Organization not found for key '%s'", (Object) key)); } return organizationDto.get(); }
void checkThatProjectBelongsToOrganization(ComponentDto componentDto, OrganizationDto organizationDto, String message, Object... messageArguments) { if (!organizationDto.getUuid().equals(componentDto.getOrganizationUuid())) { throw new NotFoundException(format(message, messageArguments)); } }
private OrganizationDto getOrganizationDtoOrFail(DbSession dbSession, String organizationKey) { return dbClient.organizationDao().selectByKey(dbSession, organizationKey) .orElseThrow(() -> new NotFoundException(format("Organization with key '%s' does not exist", organizationKey))); }
private EventDto getEvent(DbSession dbSession, String event) { return dbClient.eventDao().selectByUuid(dbSession, event) .orElseThrow(() -> new NotFoundException(format("Event '%s' not found", event))); }
/** * Components of the main branch won't be found */ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) { Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch)); }
public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) { Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' of pull request '%s' not found", key, pullRequest)); }
private MetricDto getNonNullMetric(DbSession dbSession, String metricKey) { MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey); if (metric == null) { throw new NotFoundException(format("There is no metric with key=%s", metricKey)); } return metric; }
private void checkProfileExists(String profileKey, DbSession session) { if (dbClient.qualityProfileDao().selectByUuid(session, profileKey) == null) { throw new NotFoundException(String.format("Could not find a quality profile with key '%s'", profileKey)); } }
private List<MetricDto> searchMetrics(DbSession dbSession, ComponentTreeRequest request) { List<String> metricKeys = requireNonNull(request.getMetricKeys()); List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys); if (metrics.size() < metricKeys.size()) { List<String> foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set<String> missingMetricKeys = Sets.difference( new LinkedHashSet<>(metricKeys), new LinkedHashSet<>(foundMetricKeys)); throw new NotFoundException(format("The following metric keys are not found: %s", COMMA_JOINER.join(missingMetricKeys))); } String forbiddenMetrics = metrics.stream() .filter(metric -> ComponentTreeAction.FORBIDDEN_METRIC_TYPES.contains(metric.getValueType())) .map(MetricDto::getKey) .sorted() .collect(MoreCollectors.join(COMMA_JOINER)); checkArgument(forbiddenMetrics.isEmpty(), "Metrics %s can't be requested in this web service. Please use api/measures/component", forbiddenMetrics); return metrics; }
void checkProjectBelongsToOrganization(OrganizationDto organization, ComponentDto project) { if (project.getOrganizationUuid().equals(organization.getUuid())) { return; } throw new NotFoundException(format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey())); }
public IssueDto getByKey(DbSession session, String issueKey) { IssueDto issue = dbClient.issueDao().selectByKey(session, issueKey).orElseThrow(() -> new NotFoundException(format("Issue with key '%s' does not exist", issueKey))); userSession.checkComponentUuidPermission(UserRole.USER, requireNonNull(issue.getProjectUuid())); return issue; }
private EventDto getDbEvent(DbSession dbSession, UpdateEventRequest request) { checkArgument(isNotBlank(request.getName()), "A non empty name is required"); return dbClient.eventDao().selectByUuid(dbSession, request.getEvent()) .orElseThrow(() -> new NotFoundException(format("Event '%s' not found", request.getEvent()))); }