/** * @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(); }
/** * 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)); } }
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())); }
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()))); }
private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key, OrganizationDto organization) { RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key) .orElseThrow(() -> new NotFoundException(format("This rule does not exist: %s", key))); RuleUpdate ruleUpdate = ofNullable(rule.getTemplateId()) .map(x -> RuleUpdate.createForCustomRule(key)) .orElseGet(() -> RuleUpdate.createForPluginRule(key)); ruleUpdate.setOrganization(organization); return ruleUpdate; }
CommentData(DbSession dbSession, String commentKey) { this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, commentKey) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", commentKey))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments"); }
CommentData(DbSession dbSession, EditCommentRequest request) { this.request = request; this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, request.getComment()) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", request.getComment()))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments"); }
ComponentDto getComponent(DbSession dbSession, Request request) { try { String projectKey = request.mandatoryParam(PARAM_PROJECT); String branchName = request.param(PARAM_BRANCH); ComponentDto project = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, projectKey, branchName, null); checkComponentType(dbSession, project); userSession.checkComponentPermission(USER, project); return project; } catch (NotFoundException e) { throw new NotFoundException("Project has not been found"); } }
@Override public void handle(Request request, Response response) throws Exception { if (request.param(PARAM_DEVELOPER_ID) != null || request.param(PARAM_DEVELOPER_KEY) != null) { throw new NotFoundException("The Developer Cockpit feature has been dropped. The specified developer cannot be found."); } ComponentWsResponse componentWsResponse = doHandle(toComponentWsRequest(request)); writeProtobuf(componentWsResponse, request, response); }
private void writeQualityGate(JsonWriter json, DbSession session, OrganizationDto organization, ComponentDto component) { QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component) .orElseThrow(() -> new NotFoundException(format("Quality Gate not found for %s", component.getKey()))); QualityGateDto qualityGateDto = qualityGateData.getQualityGate(); json.name("qualityGate").beginObject() .prop("key", qualityGateDto.getId()) .prop("name", qualityGateDto.getName()) .prop("isDefault", qualityGateData.isDefault()) .endObject(); }