use of org.haiku.haikudepotserver.dataobjects.RepositorySource in project haikudepotserver by haiku.
the class RepositoryApiImpl method updateRepository.
@Override
public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRepositoryRequest) {
Preconditions.checkNotNull(updateRepositoryRequest);
final ObjectContext context = serverRuntime.newContext();
Repository repository = getRepositoryOrThrow(context, updateRepositoryRequest.code);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
}
for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
switch(filter) {
case ACTIVE:
if (null == updateRepositoryRequest.active) {
throw new IllegalStateException("the active flag must be supplied");
}
if (repository.getActive() != updateRepositoryRequest.active) {
repository.setActive(updateRepositoryRequest.active);
LOGGER.info("did set the active flag on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.active);
}
if (!updateRepositoryRequest.active) {
for (RepositorySource repositorySource : repository.getRepositorySources()) {
if (repositorySource.getActive()) {
repositorySource.setActive(false);
LOGGER.info("did set the active flag on the repository source {} to false", repositorySource.getCode());
}
}
}
break;
case NAME:
if (null == updateRepositoryRequest.name) {
throw new IllegalStateException("the name must be supplied to update the repository");
}
String name = updateRepositoryRequest.name.trim();
if (0 == name.length()) {
throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
}
repository.setName(name);
break;
case INFORMATIONURL:
repository.setInformationUrl(updateRepositoryRequest.informationUrl);
LOGGER.info("did set the information url on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.informationUrl);
break;
case PASSWORD:
if (StringUtils.isBlank(updateRepositoryRequest.passwordClear)) {
repository.setPasswordSalt(null);
repository.setPasswordHash(null);
LOGGER.info("cleared the password for repository [{}]", repository);
} else {
repositoryService.setPassword(repository, updateRepositoryRequest.passwordClear);
LOGGER.info("did update the repository [{}] password", repository);
}
break;
default:
throw new IllegalStateException("unhandled filter for updating a repository");
}
}
if (context.hasChanges()) {
context.commitChanges();
} else {
LOGGER.info("update repository {} with no changes made", updateRepositoryRequest.code);
}
return new UpdateRepositoryResult();
}
use of org.haiku.haikudepotserver.dataobjects.RepositorySource in project haikudepotserver by haiku.
the class RepositoryApiImpl method createRepositorySourceMirror.
@Override
public CreateRepositorySourceMirrorResult createRepositorySourceMirror(CreateRepositorySourceMirrorRequest request) {
Preconditions.checkArgument(null != request, "the request must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositorySourceCode), "the code for the new repository source mirror");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.countryCode), "the country code should be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.baseUrl), "the base url should be supplied");
final ObjectContext context = serverRuntime.newContext();
Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.repositorySourceCode);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("the repository [" + repositorySource.getRepository() + "] is not able to be edited");
}
if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySource.getCode(), request.baseUrl).isPresent()) {
LOGGER.info("attempt to add a repository source mirror for a url [{}] that is " + " already in use", request.baseUrl);
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
}
// if there is no other mirror then this should be the primary.
RepositorySourceMirror mirror = context.newObject(RepositorySourceMirror.class);
mirror.setIsPrimary(repositorySource.tryGetPrimaryMirror().isEmpty());
mirror.setBaseUrl(request.baseUrl);
mirror.setRepositorySource(repositorySource);
mirror.setCountry(country);
mirror.setDescription(StringUtils.trimToNull(request.description));
mirror.setCode(UUID.randomUUID().toString());
repositorySource.getRepository().setModifyTimestamp();
context.commitChanges();
LOGGER.info("did add mirror [{}] to repository source [{}]", country.getCode(), repositorySource.getCode());
CreateRepositorySourceMirrorResult result = new CreateRepositorySourceMirrorResult();
result.code = mirror.getCode();
return result;
}
use of org.haiku.haikudepotserver.dataobjects.RepositorySource in project haikudepotserver by haiku.
the class RepositoryApiImpl method createRepositorySource.
@Override
public CreateRepositorySourceResult createRepositorySource(CreateRepositorySourceRequest request) {
Preconditions.checkArgument(null != request, "the request must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the new repository source must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository for the new repository source must be identified");
final ObjectContext context = serverRuntime.newContext();
Repository repository = getRepositoryOrThrow(context, request.repositoryCode);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
}
Optional<RepositorySource> existingRepositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
if (existingRepositorySourceOptional.isPresent()) {
throw new ValidationException(new ValidationFailure(RepositorySource.CODE.getName(), "unique"));
}
RepositorySource repositorySource = context.newObject(RepositorySource.class);
repositorySource.setRepository(repository);
repositorySource.setCode(request.code);
repository.setModifyTimestamp();
context.commitChanges();
LOGGER.info("did create a new repository source '{}' on the repository '{}'", repositorySource, repository);
return new CreateRepositorySourceResult();
}
use of org.haiku.haikudepotserver.dataobjects.RepositorySource in project haikudepotserver by haiku.
the class RepositoryController method importRepositorySource.
/**
* <p>Instructs HDS to import repository data for a repository source of
* a repository.</p>
*/
@RequestMapping(value = "{" + KEY_REPOSITORYCODE + "}/" + SEGMENT_SOURCE + "/{" + KEY_REPOSITORYSOURCECODE + "}/" + SEGMENT_IMPORT, method = RequestMethod.POST)
public ResponseEntity<String> importRepositorySource(@PathVariable(value = KEY_REPOSITORYCODE) String repositoryCode, @PathVariable(value = KEY_REPOSITORYSOURCECODE) String repositorySourceCode) {
ObjectContext context = serverRuntime.newContext();
Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, repositoryCode);
if (repositoryOptional.isEmpty()) {
return new ResponseEntity<>("repository not found", HttpStatus.NOT_FOUND);
}
Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, repositorySourceCode);
if (repositorySourceOptional.isEmpty() || !repositoryOptional.get().equals(repositorySourceOptional.get().getRepository())) {
return new ResponseEntity<>("repository source not found", HttpStatus.NOT_FOUND);
}
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositoryOptional.get(), Permission.REPOSITORY_IMPORT)) {
throw new AccessDeniedException("unable to import repository [" + repositoryOptional.get() + "]");
}
jobService.submit(new RepositoryHpkrIngressJobSpecification(repositoryCode, Collections.singleton(repositorySourceCode)), JobSnapshot.COALESCE_STATUSES_QUEUED);
return ResponseEntity.ok("repository source import submitted");
}
use of org.haiku.haikudepotserver.dataobjects.RepositorySource in project haikudepotserver by haiku.
the class PkgController method getAllAsJson.
/**
* <p>This streams back a redirect to report data that contains a JSON stream gzip-compressed
* that describes all of the packages for a repository source. This is used by clients to get
* deep(ish) data on all pkgs without having to query each one.</p>
*
* <p>The primary client for this is the Haiku desktop application "Haiku Depot".
* This API deprecates and older JSON-RPC API for obtaining bulk data.</p>
*/
// TODO; observe the natural language code
@RequestMapping(value = "/all-{repositorySourceCode}-{naturalLanguageCode}.json.gz", method = RequestMethod.GET)
public void getAllAsJson(HttpServletResponse response, @PathVariable(value = KEY_NATURALLANGUAGECODE) String naturalLanguageCode, @PathVariable(value = KEY_REPOSITORYSOURCECODE) String repositorySourceCode, @RequestHeader(value = HttpHeaders.IF_MODIFIED_SINCE, required = false) String ifModifiedSinceHeader) throws IOException {
ObjectContext objectContext = serverRuntime.newContext();
Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(objectContext, repositorySourceCode);
if (!repositorySourceOptional.isPresent()) {
LOGGER.info("repository source [" + repositorySourceCode + "] not found");
response.setStatus(HttpStatus.NOT_FOUND.value());
} else {
Date lastModifiedTimestamp = pkgService.getLastModifyTimestampSecondAccuracy(objectContext, repositorySourceOptional.get());
PkgDumpExportJobSpecification specification = new PkgDumpExportJobSpecification();
specification.setNaturalLanguageCode(naturalLanguageCode);
specification.setRepositorySourceCode(repositorySourceCode);
JobController.handleRedirectToJobData(response, jobService, ifModifiedSinceHeader, lastModifiedTimestamp, specification);
}
}
Aggregations