public static Job getJob(String jobId, ModelService model) throws NotFoundException, GenericException, RequestNotValidException, AuthorizationDeniedException { if (jobId != null) { return model.retrieveJob(jobId); } else { throw new NotFoundException("Job not found"); } }
@Override public void run(IndexedAIP item) throws GenericException, RequestNotValidException, AuthorizationDeniedException { PluginState state = PluginState.SUCCESS; try { model.deleteAIP(item.getId()); } catch (NotFoundException e) { state = PluginState.FAILURE; reportItem.addPluginDetails("Could not delete AIP: " + e.getMessage()); } String outcomeText; if (state.equals(PluginState.SUCCESS)) { outcomeText = PluginHelper.createOutcomeTextForAIP(item, "has been manually deleted"); } else { outcomeText = PluginHelper.createOutcomeTextForAIP(item, "has not been manually deleted"); } List<LinkingIdentifier> sources = new ArrayList<>(); sources .add(PluginHelper.getLinkingIdentifier(item.getId(), RodaConstants.PRESERVATION_LINKING_OBJECT_SOURCE)); model.createEvent(item.getId(), null, null, null, PreservationEventType.DELETION, EVENT_DESCRIPTION, sources, null, state, outcomeText, details, job.getUsername(), true); } }, e -> {
newGroup = getGroup(group.getName()); } catch (NotFoundException e) { throw new GenericException("The group was not created! " + e.getMessage());
@Override public byte[] load(String href) throws Exception { String filePath = RodaConstants.CROSSWALKS_DISSEMINATION_OTHER_PATH + href; try (InputStream in = RodaCoreFactory.getConfigurationFileAsStream(filePath); ByteArrayOutputStream out = new ByteArrayOutputStream()) { if (in == null) { throw new NotFoundException(filePath); } IOUtils.copy(in, out); return out.toByteArray(); } }
public static <T extends IsRODAObject> List<T> getObjectsFromIndex(IndexService index, Class<T> objectClass, List<String> uuids) throws NotFoundException, GenericException, RequestNotValidException { List<T> ret = (List<T>) index.retrieve((Class<IsIndexed>) objectClass, uuids, new ArrayList<>()); if (ret.isEmpty()) { throw new NotFoundException("Could not retrieve the " + objectClass.getSimpleName()); } return ret; }
public static Long recursivelyCountPath(Path directoryPath) throws NotFoundException, GenericException { // starting at -1 because the walk counts the directory itself Long count = -1L; try (Stream<Path> walk = Files.walk(directoryPath)) { final Iterator<Path> pathIterator = walk.iterator(); while (pathIterator.hasNext()) { count++; pathIterator.next(); } } catch (NoSuchFileException e) { throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + directoryPath); } catch (IOException e) { throw new GenericException("Could not list contents of entity at: " + directoryPath, e); } return count; }
public static List<AIP> getAIPs(ModelService model, List<String> uuids) throws NotFoundException { List<AIP> aipsToReturn = new ArrayList<>(); if (!uuids.isEmpty()) { for (String uuid : uuids) { try { aipsToReturn.add(model.retrieveAIP(uuid)); } catch (RODAException | RuntimeException e) { LOGGER.error("Error while retrieving AIP from model", e); } } } if (aipsToReturn.isEmpty()) { throw new NotFoundException("Could not retrieve the AIPs"); } return aipsToReturn; }
public static List<Representation> getRepresentations(ModelService model, IndexService index, List<String> uuids) throws NotFoundException { if (!uuids.isEmpty()) { try { List<IndexedRepresentation> retrieve = index.retrieve(IndexedRepresentation.class, uuids, new ArrayList<>()); List<Representation> representationsToReturn = getRepresentationFromList(model, retrieve); if (representationsToReturn.isEmpty()) { throw new NotFoundException("Could not retrieve the Representations"); } return representationsToReturn; } catch (RODAException | RuntimeException e) { LOGGER.error("Error while retrieving representations from index", e); } } throw new NotFoundException("Could not retrieve the Representations"); }
public static List<File> getFiles(ModelService model, IndexService index, List<String> uuids) throws NotFoundException { if (!uuids.isEmpty()) { try { List<IndexedFile> retrieve = index.retrieve(IndexedFile.class, uuids, new ArrayList<>()); List<File> filesToReturn = getFilesFromList(model, retrieve); if (filesToReturn.isEmpty()) { throw new NotFoundException("Could not retrieve the Files"); } return filesToReturn; } catch (RODAException | RuntimeException e) { LOGGER.error("Error while retrieving files from index", e); } } throw new NotFoundException("Could not retrieve the Files"); }
@Override public byte[] load(Pair<String, String> pair) throws Exception { String systemId = pair.getSecond(); String filename = Paths.get(URI.create(systemId)).getFileName().toString(); String configurationFile = RodaConstants.CORE_SCHEMAS_FOLDER + "/" + filename; try (InputStream in = RodaCoreFactory.getConfigurationFileAsStream(configurationFile); ByteArrayOutputStream out = new ByteArrayOutputStream()) { if (in != null) { IOUtils.copy(in, out); return out.toByteArray(); } else throw new NotFoundException(configurationFile); } }
public static String getJobUsername(String jobId, IndexService index) throws NotFoundException, GenericException, RequestNotValidException, AuthorizationDeniedException { if (jobId != null) { Job job = index.retrieve(Job.class, jobId, Arrays.asList(RodaConstants.JOB_USERNAME)); return job.getUsername(); } else { throw new NotFoundException("Job not found"); } }
public static Long countPath(Path directoryPath) throws NotFoundException, GenericException { Long count = 0L; try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) { final Iterator<Path> pathIterator = directoryStream.iterator(); while (pathIterator.hasNext()) { count++; pathIterator.next(); } } catch (NoSuchFileException e) { throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + directoryPath); } catch (IOException e) { throw new GenericException("Could not list contents of entity at: " + directoryPath, e); } return count; }
public static String getReindexPluginName(Class<?> reindexClass) throws NotFoundException { if (reindexClass.equals(AIP.class)) { return ReindexAIPPlugin.class.getName(); } else if (reindexClass.equals(RepresentationInformation.class)) { return ReindexRepresentationInformationPlugin.class.getName(); } else if (reindexClass.equals(Risk.class)) { return ReindexRiskPlugin.class.getName(); } else if (reindexClass.equals(RiskIncidence.class)) { return ReindexIncidencePlugin.class.getName(); } else if (reindexClass.equals(Job.class)) { return ReindexJobPlugin.class.getName(); } else if (reindexClass.equals(Notification.class)) { return ReindexNotificationPlugin.class.getName(); } else if (reindexClass.equals(TransferredResource.class)) { return ReindexTransferredResourcePlugin.class.getName(); } else if (reindexClass.equals(RODAMember.class)) { return ReindexRodaMemberPlugin.class.getName(); } else if (reindexClass.equals(LogEntry.class)) { return ReindexActionLogPlugin.class.getName(); } else if (reindexClass.equals(IndexedPreservationAgent.class)) { return ReindexPreservationAgentPlugin.class.getName(); } else if (reindexClass.equals(IndexedPreservationEvent.class)) { return ReindexPreservationRepositoryEventPlugin.class.getName(); } else if (reindexClass.equals(DIP.class)) { return ReindexDIPPlugin.class.getName(); } else { throw new NotFoundException("No reindex plugin available"); } }
/** * 20160329 hsilva: use this method only to get job information that most * certainly won't change in time (e.g. username, etc.) * * @throws RequestNotValidException */ public static <T extends IsRODAObject> Job getJob(Plugin<T> plugin, IndexService index) throws NotFoundException, GenericException, RequestNotValidException { String jobId = getJobId(plugin); if (jobId != null) { return index.retrieve(Job.class, jobId, new ArrayList<>()); } else { throw new NotFoundException("Job not found"); } }
@Override public byte[] load(String href) throws Exception { InputStream in = null; ByteArrayOutputStream out = null; try { String filename = href; try { filename = Paths.get(URI.create(href)).getFileName().toString(); } catch (IllegalArgumentException e) { try { filename = Paths.get(href).getFileName().toString(); } catch (InvalidPathException e2) { // nothing to do } } String filePath = RodaConstants.CORE_SCHEMAS_FOLDER + "/" + filename; in = RodaCoreFactory.getConfigurationFileAsStream(filePath); if (in == null) { throw new NotFoundException(filePath); } out = new ByteArrayOutputStream(); IOUtils.copy(in, out); return out.toByteArray(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + path, e); } catch (IOException e) { throw new GenericException("Could not list contents of entity at: " + path, e);
public Path retrieveFilePath(String path) throws NotFoundException, RequestNotValidException, GenericException { Path p = basePath.resolve(path); if (!FSUtils.exists(p)) { throw new NotFoundException("File not found: " + path); } else if (!FSUtils.isFile(p)) { throw new RequestNotValidException("Requested file is not a regular file: " + path); } return p; }
public InputStream retrieveFile(String path) throws NotFoundException, RequestNotValidException, GenericException { InputStream ret; Path p = basePath.resolve(path); if (!FSUtils.exists(p)) { throw new NotFoundException("File not found: " + path); } else if (!FSUtils.isFile(p)) { throw new RequestNotValidException("Requested file is not a regular file: " + path); } else { try { ret = Files.newInputStream(p); } catch (IOException e) { throw new GenericException("Could not create input stream: " + e.getMessage()); } } return ret; }
public static <T extends IsRODAObject> String getJobUsername(Plugin<T> plugin, ModelService model) throws NotFoundException, GenericException, RequestNotValidException, AuthorizationDeniedException { String jobId = getJobId(plugin); if (jobId != null) { Job job = model.retrieveJob(jobId); return job.getUsername(); } else { throw new NotFoundException("Job not found"); } }
public static Representation convertResourceToRepresentation(Resource resource) throws GenericException, NotFoundException, AuthorizationDeniedException, RequestNotValidException { if (resource == null) { throw new RequestNotValidException(RESOURCE_CANNOT_BE_NULL); } StoragePath resourcePath = resource.getStoragePath(); String id = resourcePath.getName(); String aipId = ModelUtils.extractAipId(resourcePath).orElse(null); AIP aip = RodaCoreFactory.getModelService().retrieveAIP(aipId); Optional<Representation> rep = aip.getRepresentations().stream().filter(r -> r.getId().equals(id)).findFirst(); if (rep.isPresent()) { return rep.get(); } else { throw new NotFoundException("Unable to find representation with storage path " + resourcePath); } }