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 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; }
/** * 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"); } }
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); } }
/**********************************************************/ public Representation retrieveRepresentation(String aipId, String representationId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); Representation ret = null; for (Representation representation : aip.getRepresentations()) { if (representation.getId().equals(representationId)) { ret = representation; break; } } if (ret == null) { throw new NotFoundException("Could not find representation: " + representationId); } return ret; }
public DescriptiveMetadata retrieveDescriptiveMetadata(String aipId, String representationId, String descriptiveMetadataId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); DescriptiveMetadata ret = null; for (DescriptiveMetadata descriptiveMetadata : getDescriptiveMetadata(aip, representationId)) { if (descriptiveMetadata.getId().equals(descriptiveMetadataId)) { ret = descriptiveMetadata; break; } } if (ret == null) { throw new NotFoundException("Could not find descriptive metadata: " + descriptiveMetadataId); } return ret; }
@Override public Class<? extends Entity> getEntity(StoragePath storagePath) throws NotFoundException { Path entity = FSUtils.getEntityPath(basePath, storagePath); if (FSUtils.exists(entity)) { if (FSUtils.isDirectory(entity)) { if (storagePath.isFromAContainer()) { return DefaultContainer.class; } else { return DefaultDirectory.class; } } else { return DefaultBinary.class; } } else { throw new NotFoundException("Entity was not found: " + storagePath); } }
@Override public Container getContainer(StoragePath storagePath) throws RequestNotValidException, NotFoundException { if (!storagePath.isFromAContainer()) { throw new RequestNotValidException("Storage path is not from a container"); } Path containerPath = FSUtils.getEntityPath(basePath, storagePath); Container container; if (FSUtils.exists(containerPath)) { container = new DefaultContainer(storagePath); } else { throw new NotFoundException("Container not found: " + storagePath); } return container; }
public boolean isInternal(String username) throws GenericException, NotFoundException { final CoreSession session = this.service.getAdminSession(); Entry entry; try { entry = session.lookup(new Dn(getUserDN(username))); return entry.containsAttribute(USER_PASSWORD); } catch (LdapNoSuchObjectException e) { throw new NotFoundException(username); }catch (LdapException e) { throw new GenericException(e); } }