/** * @see Option#fold */ private static <A, B> Function<Option<A>, B> fold(final Supplier<B> none, final Function<A, B> some) { return new Function<Option<A>, B>() { @Override public B apply(final Option<A> op) { return op.fold(none, some); } }; }
private ProjectPermissionKey getPermission() { return actual.fold(new Function<PermissionGrant, ProjectPermissionKey>() { @Override public ProjectPermissionKey apply(final PermissionGrant input) { return input.getPermission(); } }, new Function<PermissionGrantInput, ProjectPermissionKey>() { @Override public ProjectPermissionKey apply(final PermissionGrantInput input) { return input.getPermission(); } }); }
private PermissionHolder getHolder() { return actual.fold(new Function<PermissionGrant, PermissionHolder>() { @Override public PermissionHolder apply(final PermissionGrant input) { return input.getHolder(); } }, new Function<PermissionGrantInput, PermissionHolder>() { @Override public PermissionHolder apply(final PermissionGrantInput input) { return input.getHolder(); } }); }
@Override public boolean canCreateTemporaryAttachments(@Nullable final ApplicationUser user, final Either<Issue, Project> issueOrProject, final ErrorCollection errorCollection) { return validateAttachmentsEnabled(user, errorCollection) && issueOrProject.fold( new Function<Issue, Boolean>() { @Override public Boolean apply(@SuppressWarnings ("NullableProblems") final Issue issue) { return validateCreateAttachmentsWithoutWorkflow(user, issue, errorCollection); } }, new Function<Project, Boolean>() { @Override public Boolean apply(@SuppressWarnings ("NullableProblems") final Project project) { return validateCreateTemporaryAttachmentsInProject(user, project, errorCollection); } }); }
@Override public Promise<Attachment> putAttachment(final Attachment metadata, final File source) { //noinspection NullableProblems return storeAttachmentBeanMapper.mapToBean(metadata, source).fold( new com.google.common.base.Function<FileNotFoundException, Promise<Attachment>>() { @Override public Promise<Attachment> apply(final FileNotFoundException fileNotFoundException) { return Promises.rejected(new AttachmentWriteException("Failed to open file: " + source.getAbsolutePath(), fileNotFoundException)); } }, new com.google.common.base.Function<StoreAttachmentBean, Promise<Attachment>>() { @Override public Promise<Attachment> apply(final StoreAttachmentBean storeAttachmentBean) { return putAttachment(storeAttachmentBean).map(Functions.constant(metadata)); } }); }
@GET @Path ("{issueKey}") public Response issueMentions(@PathParam ("issueKey") final String issueKey) { Either<Throwable, Issue> issue = getIssueByKey(issueKey); return issue.fold( errorResponse(issueKey), new Function<Issue, Response>() { @Override public Response apply(@Nullable Issue issue) { return doGetIssueMentionsInfo(issue, new IssueMentionsToViewItemsFunction(issue.getId(), responseFactory) ); } } ); }
@GET @Path ("{issueKey}/rooms") public Response issueMentionRooms(@PathParam ("issueKey") final String issueKey) { Either<Throwable, Issue> issue = getIssueByKey(issueKey); return issue.fold( errorResponse(issueKey), new Function<Issue, Response>() { @Override public Response apply(@Nullable Issue issue) { return doGetIssueMentionsInfo(issue, new IssueMentionsToRoomsFunction( new IssueMentionsToViewItemsFunction(issue.getId(), responseFactory)) ); } } ); }
@Override public String apply(Iterable<IssueMention> issueMentions) { final Either<Throwable, List<IssueMentionViewResponseFactory.IssueMentionViewItem>> mentionsViewResponse = responseFactory.createResponse(issue.getId(), issueMentions, true); return mentionsViewResponse.fold(handleError, mentionsListToActionViewResult); } });
private Properties loadRequiredProperties(final Function<String, Option<InputStream>> resourceLoader, final String path) { final Function<? super IOException, Properties> rethrowException = exception -> { throw new RuntimeIOException("Unable to load properties from " + path, exception); }; final Supplier<IllegalStateException> missingFileExceptionSupplier = () -> { throw new IllegalStateException(String.format("Resource %s not found", path)); }; final Function<Option<Properties>, Properties> unFoldProperties = (Option<Properties> propertiesOption) -> propertiesOption.getOrThrow(missingFileExceptionSupplier); return tryLoadProperties(() -> resourceLoader.apply(path)).fold( rethrowException, unFoldProperties); }
@GET @Path ("{ruleId}") public Response getRule(@PathParam ("ruleId") final int ruleId) { Either<ErrorCollection, Rule> result = ruleService.getRule(userManager.getRemoteUsername(), ruleId); return result.fold(new HandleErrorFunction(), new Function<Rule, Response>() { @Override public Response apply(@Nullable final Rule input) { if (input != null) { return Response.ok(convertRuleToView(input)).build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } }); }
@PUT @Path ("{ruleId}/disable") public Response disableRule(@PathParam ("ruleId") final int ruleId) { Either<ErrorCollection, RuleService.UpdateRuleStatusValidationResult> result = ruleService.validateUpdateRuleStatus(userManager.getRemoteUsername(), ruleId); return result.fold(new HandleErrorFunction(), new Function<RuleService.UpdateRuleStatusValidationResult, Response>() { @Override public Response apply(@Nullable RuleService.UpdateRuleStatusValidationResult input) { if (input != null) { final Rule rule = ruleService.updateRuleStatus(input, false); return Response.ok(convertRuleToView(rule)).build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } } ); }
@DELETE @Path ("{ruleId}") public Response deleteRule(@PathParam ("ruleId") final int ruleId) { Either<ErrorCollection, RuleService.DeleteRuleValidationResult> result = ruleService.validateDeleteRule(userManager.getRemoteUsername(), ruleId); return result.fold(new HandleErrorFunction(), new Function<RuleService.DeleteRuleValidationResult, Response>() { @Override public Response apply(@Nullable RuleService.DeleteRuleValidationResult input) { if (input != null) { ruleService.deleteRule(input); return Response.ok().build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } } ); }
@PUT @Path ("{ruleId}") public Response updateRule(final DefaultRule rule) { final Either<RuleErrors, RuleService.RuleValidationResult> validationResult = ruleService.validateUpdateRule(userManager.getRemoteUsername(), rule); return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>() { @Override public Response apply(@Nullable final RuleService.RuleValidationResult input) { if (input != null) { return Response.ok(convertRuleToView(ruleService.updateRule(input))).build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } }); }
private Map<Object, Object> getSalDarkFeatures() { final String salDarkFeaturesProperty = jiraSystemProperties.getProperty( DARKFEATURES_PROPERTIES_FILE_PROPERTY, DARKFEATURES_PROPERTIES_FILE_PROPERTY_DEFAULT); final Supplier<Properties> emptyProperties = Properties::new; return tryLoadProperties(() -> resourceLoader.getResourceAsStream(salDarkFeaturesProperty)) .fold(ex -> emptyMap(), (Option<Properties> propertiesOption) -> propertiesOption.getOrElse(emptyProperties)); }
@POST public Response createRule(final DefaultRule rule) { final Either<RuleErrors, RuleService.RuleValidationResult> validationResult = ruleService.validateAddRule(userManager.getRemoteUsername(), rule); return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>() { @Override public Response apply(@Nullable final RuleService.RuleValidationResult input) { if (input != null) { return Response.ok(convertRuleToView(ruleService.addRule(input))).build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } }); }
@GET public Response getAllRules() { Either<ErrorCollection, ? extends Iterable<Rule>> result = ruleService.getRules(userManager.getRemoteUsername()); return result.fold(new HandleErrorFunction(), new Function<Iterable<Rule>, Response>() { @Override public Response apply(final Iterable<Rule> input) { final Iterable<RuleView> ruleViews = Iterables.transform(input, new Function<Rule, RuleView>() { @Override public RuleView apply(Rule input) { return convertRuleToView(input); } }); return Response.ok(Iterables.toArray(ruleViews, RuleView.class)).build(); } }); }
/** * Installs add-ons publicly available on Marketplace. * * @param addonKey the add-on to be installed * @param dependenciesAllowed if installed addon can have dependencies * @return the installation result */ private MarketplaceInstallationResult installFromMarketplace(String addonKey, boolean dependenciesAllowed) { return addonResolver.getDescriptorFromMarketplace(addonKey) .fold( error -> MarketplaceInstallationResult.error(addonKey, error), json -> installAddon(addonKey, json, dependenciesAllowed)); }
@POST @Path("import") public Response importRule(final DefaultRule rule) { rule.unsetAllIds(); final Either<RuleErrors, RuleService.RuleValidationResult> validationResult = ruleService.validateAddRule(userManager.getRemoteUsername(), rule); return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>() { @Override public Response apply(@Nullable final RuleService.RuleValidationResult input) { if (input != null) { return Response.ok(convertRuleToView(ruleService.addRule(input))).build(); } return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build(); } }); }
private <T> ServiceOutcome<T> asAdminOrProjectAdminWithAdminAccessToProject(final ApplicationUser user, final Either<Long, String> projectIdOrKey, Function<Project, ServiceOutcome<T>> action) { final Project project = projectIdOrKey.fold(projectManager::getProjectObj, projectManager::getProjectObjByKey); if (project == null) { return ServiceOutcomeImpl.error(i18n.getText("admin.errors.portal.project.nonexist"), ErrorCollection.Reason.NOT_FOUND); } else { return performActionWithUserPermissionCondition(user, () -> isAdmin(user) || canViewProjectAsAdmin(user, project), () -> action.apply(project)); } }
private ServiceOutcome<IssueSecurityLevelScheme> getIssueSecurityLevelSchemeForProject(final ApplicationUser user, final Either<Long, String> projectIdOrKey) { Supplier<ServiceOutcome<IssueSecurityLevelScheme>> notFoundSupplier = notFoundSupplier("rest.error.issuesecurityscheme.securitylevel.for.project.not.found", projectIdOrKey.fold(l -> l.toString(), String::toString)); return asAdminOrProjectAdminWithAdminAccessToProject(user, projectIdOrKey, project -> { final Long schemeIdForProject = issueSecuritySchemeManager.getSchemeIdFor(project); final IssueSecurityLevelScheme issueSecurityLevelScheme = issueSecuritySchemeManager.getIssueSecurityLevelScheme(schemeIdForProject); return Option.option(issueSecurityLevelScheme) .fold(notFoundSupplier, IssueSecuritySchemeServiceImpl.okOutcome()); }); }