List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
From source file:org.fao.geonet.api.records.MetadataInsertDeleteApi.java
@ApiOperation(value = "Add a record", notes = "Add one or more record from an XML fragment, " + "URL or file in a folder on the catalog server. When loading" + "from the catalog server folder, it might be faster to use a " + "local filesystem harvester.", nickname = "insert") @RequestMapping(method = { RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE }) @ApiResponses(value = { @ApiResponse(code = 201, message = API_PARAM_REPORT_ABOUT_IMPORTED_RECORDS), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_EDITOR) }) @PreAuthorize("hasRole('Editor')") @ResponseStatus(HttpStatus.CREATED)/*w ww . ja v a 2s. co m*/ public @ResponseBody SimpleMetadataProcessingReport insert( @ApiParam(value = API_PARAM_RECORD_TYPE, required = false, defaultValue = "METADATA") @RequestParam(required = false, defaultValue = "METADATA") final MetadataType metadataType, @ApiParam(value = "XML fragment.", required = false) @RequestBody(required = false) String xml, @ApiParam(value = "URL of a file to download and insert.", required = false) @RequestParam(required = false) String[] url, @ApiParam(value = "Server folder where to look for files.", required = false) @RequestParam(required = false) String serverFolder, @ApiParam(value = "(Server folder import only) Recursive search in folder.", required = false) @RequestParam(required = false, defaultValue = "false") final boolean recursiveSearch, @ApiParam(value = "(MEF file only) Assign to current catalog.", required = false) @RequestParam(required = false, defaultValue = "false") final boolean assignToCatalog, @ApiParam(value = API_PARAM_RECORD_UUID_PROCESSING, required = false, defaultValue = "NOTHING") @RequestParam(required = false, defaultValue = "NOTHING") final MEFLib.UuidAction uuidProcessing, @ApiParam(value = API_PARAP_RECORD_GROUP, required = false) @RequestParam(required = false) final String group, @ApiParam(value = API_PARAM_RECORD_TAGS, required = false) @RequestParam(required = false) final String[] category, @ApiParam(value = API_PARAM_RECORD_VALIDATE, required = false) @RequestParam(required = false, defaultValue = "false") final boolean rejectIfInvalid, @ApiParam(value = API_PARAM_RECORD_XSL, required = false, defaultValue = "_none_") @RequestParam(required = false, defaultValue = "_none_") final String transformWith, @ApiParam(value = API_PARAM_FORCE_SCHEMA, required = false) @RequestParam(required = false) String schema, @ApiParam(value = "(experimental) Add extra information to the record.", required = false) @RequestParam(required = false) final String extra, HttpServletRequest request) throws Exception { if (url == null && xml == null && serverFolder == null) { throw new IllegalArgumentException( String.format("XML fragment or a URL or a server folder MUST be provided.")); } SimpleMetadataProcessingReport report = new SimpleMetadataProcessingReport(); ApplicationContext applicationContext = ApplicationContextHolder.get(); if (xml != null) { Element element = null; try { element = Xml.loadString(xml, false); } catch (JDOMParseException ex) { throw new IllegalArgumentException( String.format("XML fragment is invalid. Error is %s", ex.getMessage())); } Pair<Integer, String> pair = loadRecord(metadataType, Xml.loadString(xml, false), uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from XML with UUID '%s'", pair.two())); triggerImportEvent(request, pair.two()); report.incrementProcessedRecords(); } if (url != null) { for (String u : url) { Element xmlContent = null; try { xmlContent = Xml.loadFile(ApiUtils.downloadUrlInTemp(u)); } catch (Exception e) { report.addError(e); } if (xmlContent != null) { Pair<Integer, String> pair = loadRecord(metadataType, xmlContent, uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from URL with UUID '%s'", pair.two())); triggerImportEvent(request, pair.two()); } report.incrementProcessedRecords(); } } if (serverFolder != null) { Path serverFolderPath = IO.toPath(serverFolder); final List<Path> files = Lists.newArrayList(); final MEFLib.MefOrXmlFileFilter predicate = new MEFLib.MefOrXmlFileFilter(); if (recursiveSearch) { Files.walkFileTree(serverFolderPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (predicate.accept(file)) { files.add(file); } return FileVisitResult.CONTINUE; } }); } else { try (DirectoryStream<Path> paths = Files.newDirectoryStream(serverFolderPath, predicate)) { for (Path file : paths) { files.add(file); } } } if (files.size() == 0) { throw new Exception( String.format("No XML or MEF or ZIP file found in server folder '%s'.", serverFolder)); } SettingManager settingManager = ApplicationContextHolder.get().getBean(SettingManager.class); ServiceContext context = ApiUtils.createServiceContext(request); for (Path f : files) { if (MEFLib.isValidArchiveExtensionForMEF(f.getFileName().toString())) { try { MEFLib.Version version = MEFLib.getMEFVersion(f); List<String> ids = MEFLib.doImport(version == MEFLib.Version.V1 ? "mef" : "mef2", uuidProcessing, transformWith, settingManager.getSiteId(), metadataType, category, group, rejectIfInvalid, assignToCatalog, context, f); for (String id : ids) { report.addMetadataInfos(Integer.parseInt(id), String.format("Metadata imported from MEF with id '%s'", id)); triggerCreationEvent(request, id); report.incrementProcessedRecords(); } } catch (Exception e) { report.addError(e); report.addInfos(String.format("Failed to import MEF file '%s'. Check error for details.", f.getFileName().toString())); } } else { try { Pair<Integer, String> pair = loadRecord(metadataType, Xml.loadFile(f), uuidProcessing, group, category, rejectIfInvalid, false, transformWith, schema, extra, request); report.addMetadataInfos(pair.one(), String.format("Metadata imported from server folder with UUID '%s'", pair.two())); triggerCreationEvent(request, pair.two()); } catch (Exception e) { report.addError(e); } report.incrementProcessedRecords(); } } } report.close(); return report; }
From source file:org.apache.nifi.controller.repository.FileSystemRepository.java
private synchronized void initializeRepository() throws IOException { final Map<String, Path> realPathMap = new HashMap<>(); final ExecutorService executor = Executors.newFixedThreadPool(containers.size()); final List<Future<Long>> futures = new ArrayList<>(); // Run through each of the containers. For each container, create the sections if necessary. // Then, we need to scan through the archived data so that we can determine what the oldest // archived data is, so that we know when we have to start aging data off. for (final Map.Entry<String, Path> container : containers.entrySet()) { final String containerName = container.getKey(); final ContainerState containerState = containerStateMap.get(containerName); final Path containerPath = container.getValue(); final boolean pathExists = Files.exists(containerPath); final Path realPath; if (pathExists) { realPath = containerPath.toRealPath(); } else {/*from w w w.j a va2 s. c o m*/ realPath = Files.createDirectories(containerPath).toRealPath(); } for (int i = 0; i < SECTIONS_PER_CONTAINER; i++) { Files.createDirectories(realPath.resolve(String.valueOf(i))); } realPathMap.put(containerName, realPath); // We need to scan the archive directories to find out the oldest timestamp so that know whether or not we // will have to delete archived data based on time threshold. Scanning all of the directories can be very // expensive because of all of the disk accesses. So we do this in multiple threads. Since containers are // often unique to a disk, we just map 1 thread to each container. final Callable<Long> scanContainer = new Callable<Long>() { @Override public Long call() throws IOException { final AtomicLong oldestDateHolder = new AtomicLong(0L); // the path already exists, so scan the path to find any files and update maxIndex to the max of // all filenames seen. Files.walkFileTree(realPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { LOG.warn("Content repository contains un-readable file or directory '" + file.getFileName() + "'. Skipping. ", exc); return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory()) { return FileVisitResult.CONTINUE; } // Check if this is an 'archive' directory final Path relativePath = realPath.relativize(file); if (relativePath.getNameCount() > 3 && ARCHIVE_DIR_NAME.equals(relativePath.subpath(1, 2).toString())) { final long lastModifiedTime = getLastModTime(file); if (lastModifiedTime < oldestDateHolder.get()) { oldestDateHolder.set(lastModifiedTime); } containerState.incrementArchiveCount(); } return FileVisitResult.CONTINUE; } }); return oldestDateHolder.get(); } }; // If the path didn't exist to begin with, there's no archive directory, so don't bother scanning. if (pathExists) { futures.add(executor.submit(scanContainer)); } } executor.shutdown(); for (final Future<Long> future : futures) { try { final Long oldestDate = future.get(); if (oldestDate < oldestArchiveDate.get()) { oldestArchiveDate.set(oldestDate); } } catch (final ExecutionException | InterruptedException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new RuntimeException(e); } } } containers.clear(); containers.putAll(realPathMap); }
From source file:org.matonto.etl.rest.impl.DelimitedRestImpl.java
private void deleteDirectory(Path dir) throws IOException { if (Files.exists(dir)) { Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override/*from w w w . j ava 2 s . com*/ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc == null) { Files.delete(dir); return CONTINUE; } else { throw exc; } } }); } }
From source file:se.skl.skltpservices.npoadapter.test.stub.EhrExtractWS.java
protected void resetCache() throws Exception { log.info("reset testdata cache"); responseCache.clear();/* w ww. j a v a2s.co m*/ final String path = SpringPropertiesUtil.getProperty("EHR_TESTDATA_PATH"); if (StringUtils.isBlank(path)) { log.warn("No value for property EHR_TESTDATA_PATH"); } else { Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { responseCache.put(file.getFileName().toString(), Util.loadDynamicTestData(file)); log.info("Cached file: " + file.getFileName().toString()); } catch (JAXBException err) { log.error("File: " + file.toString(), err); } return super.visitFile(file, attrs); } }); log.info("Finished loading testdata cache from: " + path); } }
From source file:org.deventropy.shared.utils.DirectoryArchiverUtilTest.java
private ArchiveEntries createArchiveEntries(final File sourceDirectory, final String pathPrefix) throws IOException { final ArchiveEntries archiveEntries = new ArchiveEntries(); final Path sourcePath = Paths.get(sourceDirectory.toURI()); final StringBuilder normalizedPathPrefix = new StringBuilder(); if (null != pathPrefix && !pathPrefix.isEmpty()) { normalizedPathPrefix.append(pathPrefix.replace("\\", "/")); if (normalizedPathPrefix.charAt(normalizedPathPrefix.length() - 1) != '/') { normalizedPathPrefix.append('/'); }//from w w w. j a v a 2s . c o m } Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { final Path relativeSourcePath = sourcePath.relativize(dir); String normalizedPath = normalizedPathPrefix.toString() + relativeSourcePath; if (!normalizedPath.isEmpty()) { if (!normalizedPath.endsWith("/")) { normalizedPath += "/"; } archiveEntries.dirs.add(normalizedPath.replace("\\", "/")); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { final Path relativeSourcePath = sourcePath.relativize(file); final String normalizedPath = normalizedPathPrefix.toString() + relativeSourcePath; final byte[] md5Digest = getMd5Digest(Files.newInputStream(file), true); archiveEntries.files.put(normalizedPath.replace("\\", "/"), md5Digest); return FileVisitResult.CONTINUE; } }); return archiveEntries; }
From source file:org.eclipse.winery.repository.importing.CSARImporter.java
/** * Imports a self-service meta data description (if available) * /* w ww. j a v a 2 s. c o m*/ * The first service template in the provided entry definitions is taken * * @param tmf * * @param errors */ private void importSelfServiceMetaData(final TOSCAMetaFile tmf, final Path rootPath, Path entryDefinitions, final List<String> errors) { final Path selfServiceDir = rootPath.resolve(Constants.DIRNAME_SELF_SERVICE_METADATA); if (!Files.exists(selfServiceDir)) { CSARImporter.logger.debug("Self-service Portal directory does not exist in CSAR"); return; } if (!Files.exists(entryDefinitions)) { CSARImporter.logger.debug("Entry definitions does not exist."); return; } Unmarshaller um = JAXBSupport.createUnmarshaller(); TDefinitions defs; try { defs = (TDefinitions) um.unmarshal(entryDefinitions.toFile()); } catch (JAXBException e) { errors.add("Could not unmarshal definitions " + entryDefinitions.getFileName() + " " + e.getMessage()); return; } catch (ClassCastException e) { errors.add( "Definitions " + entryDefinitions.getFileName() + " is not a TDefinitions " + e.getMessage()); return; } final int cutLength = selfServiceDir.toString().length() + 1; Iterator<TExtensibleElements> iterator = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation() .iterator(); boolean found = false; TExtensibleElements next = null; while (iterator.hasNext() && !found) { next = iterator.next(); if (next instanceof TServiceTemplate) { found = true; } } if (found) { TServiceTemplate serviceTemplate = (TServiceTemplate) next; String namespace = serviceTemplate.getTargetNamespace(); if (namespace == null) { namespace = defs.getTargetNamespace(); } ServiceTemplateId stId = new ServiceTemplateId(namespace, serviceTemplate.getId(), false); final SelfServiceMetaDataId id = new SelfServiceMetaDataId(stId); // QUICK HACK: We just import all data without any validation // Reason: the metadata resource can deal with nearly arbitrary formats of the data, therefore // we do not do any checking here try { Files.walkFileTree(selfServiceDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { String name = file.toString().substring(cutLength); // check: if name contains "/", this could lead to exceptions RepositoryFileReference ref = new RepositoryFileReference(id, name); if (name.equals("data.xml")) { // we have to check whether the data.xml contains // (uri:"http://opentosca.org/self-service", local:"application") // instead of // (uri:"http://www.eclipse.org/winery/model/selfservice", local:"Application" // We quickly replace it via String replacement instead of XSLT try { String oldContent = org.apache.commons.io.FileUtils.readFileToString(file.toFile(), "UTF-8"); String newContent = oldContent.replace("http://opentosca.org/self-service", "http://www.eclipse.org/winery/model/selfservice"); newContent = newContent.replace(":application", ":Application"); if (!oldContent.equals(newContent)) { // we replaced something -> write new content to old file org.apache.commons.io.FileUtils.writeStringToFile(file.toFile(), newContent, "UTF-8"); } } catch (IOException e) { CSARImporter.logger.debug("Could not replace content in data.xml", e); } } CSARImporter.this.importFile(file, ref, tmf, rootPath, errors); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { CSARImporter.logger.debug(e.getMessage(), e); errors.add("Self-service Meta Data: " + e.getMessage()); } } }
From source file:org.bonitasoft.platform.setup.PlatformSetupIT.java
@Test public void push_method_should_clean_previous_config() throws Exception { //given//from w ww . ja v a 2 s . c o m List<FullBonitaConfiguration> configurations = new ArrayList<>(); final Path initPath = temporaryFolder.newFolder("init").toPath(); final Path pushPath = temporaryFolder.newFolder("push").toPath(); final Path checkPath = temporaryFolder.newFolder("check").toPath(); final Path licensesPath = temporaryFolder.newFolder("lic").toPath(); FileUtils.writeByteArrayToFile( initPath.resolve(PLATFORM_CONF_FOLDER_NAME).resolve("initial") .resolve(PLATFORM_ENGINE.name().toLowerCase()).resolve("initial.properties").toFile(), "key1=value1".getBytes()); FileUtils.writeByteArrayToFile(pushPath.resolve(PLATFORM_CONF_FOLDER_NAME).resolve("current") .resolve(TENANT_TEMPLATE_PORTAL.name().toLowerCase()).resolve("current.properties").toFile(), "key2=value2".getBytes()); System.setProperty(BONITA_SETUP_FOLDER, initPath.toString()); configurationFolderUtil.buildSqlFolder(initPath.toFile().toPath(), dbVendor); platformSetup.init(); //when System.setProperty(BONITA_SETUP_FOLDER, pushPath.toString()); platformSetup.forcePush(); //then platformSetup.pull(checkPath, licensesPath); Files.walkFileTree(checkPath, new AllConfigurationResourceVisitor(configurations)); assertThat(configurations).as("should remove all files").hasSize(1).extracting("resourceName") .containsOnly("current.properties"); }
From source file:com.hybridbpm.core.util.HybridbpmCoreUtil.java
public static void deleteDirectory(String path) { try {//from ww w .ja v a2s. co m Path directory = Paths.get(path); Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException ex) { logger.severe(ex.getMessage()); } }
From source file:org.apache.openaz.xacml.pdp.test.policy.TestPolicy.java
protected void removeRequests() { ////from ww w .j a v a 2 s . com // Delete any existing request files that we generate. i.e. Have the Unknown in the file name. // try { Files.walkFileTree(Paths.get(this.directory.toString(), "requests"), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { // // Sanity check the file name // Matcher matcher = pattern.matcher(file.getFileName().toString()); if (matcher.matches()) { try { // // Pull what this request is supposed to be // String group = null; int count = matcher.groupCount(); if (count >= 1) { group = matcher.group(count - 1); } // // Send it // if (group.equals("Unknown")) { // // Remove the file // Files.delete(file); } } catch (Exception e) { logger.error(e); e.printStackTrace(); } } return super.visitFile(file, attrs); } }); } catch (IOException e) { logger.error("Failed to removeRequests from " + this.directory + " " + e); } }
From source file:org.bonitasoft.platform.setup.PlatformSetupIT.java
@Test public void push_method_should_throw_exception_if_no_current_folder() throws Exception { //given/*from www .j a va 2 s . c om*/ List<FullBonitaConfiguration> configurations = new ArrayList<>(); final Path confFolder = temporaryFolder.newFolder().toPath(); configurationFolderUtil.buildInitialFolder(confFolder); configurationFolderUtil.buildSqlFolder(confFolder, dbVendor); System.setProperty(BONITA_SETUP_FOLDER, confFolder.toString()); platformSetup.init(); Path current = confFolder.resolve("platform_conf").resolve("current"); //when try { platformSetup.push(); fail(); } catch (PlatformException ignored) { assertThat(ignored.getMessage()).isEqualTo("Unable to push configuration from " + current + ", as directory does not exists. To modify your configuration, run 'setup pull', update your configuration files from " + current + " folder, and then push your new configuration."); //ok } //then platformSetup.pull(); Files.walkFileTree(current, new AllConfigurationResourceVisitor(configurations)); assertThat(configurations).as("should have kept old files").hasSize(1).extracting("resourceName") .containsOnly("initialConfig.properties"); }