@Override public Object resolveVariable(final QName qName) { final String variableName = qName.getLocalPart(); if (!variables.containsKey(variableName)) { throw new ApplicationException(XPathErrorType.ERROR_XPATH_EVALUATION, "Cannot resolve variable for XPath evaluation") .addContextValue(XPathErrorContext.VARIABLE_NAME, variableName) .addContextValue(XPathErrorContext.LIST, ExceptionMessageUtils.printMap(variables, String::toString, String::toString)); } return variables.get(qName.getLocalPart()); } });
/** * Retrieve a file by name in a folder. * * @param name the name of the file * @param folder the folder * @return the requested file */ public static File retrieveFileInFolder(final String name, final File folder) { Validate.notNull(folder); Validate.notNull(name); File ret = new File(folder, name); if (!ret.exists() || !ret.isFile()) { throw new ApplicationException(FileErrorType.NO_FILE_FOUND, "No file found in the folder with given name") .addContextValue(FileErrorContext.FOLDER, folder) .addContextValue(FileErrorContext.FILENAME, name); } logger.info("File [{}] found in folder [{}]", ret, folder); return ret; }
/** * Retrieve a file by name in a folder. * * @param filename the name of the file * @param folderPath the folder * @return the requested file */ public static Path retrieveFileInFolder(final String filename, final Path folderPath) { Validate.notNull(folderPath); Validate.notNull(filename); Path ret = folderPath.resolve(filename); if (Files.notExists(ret) || !Files.isRegularFile(ret)) { throw new ApplicationException(FileErrorType.NO_FILE_FOUND, "No file found in the folder with given name") .addContextValue(FileErrorContext.FOLDER, folderPath) .addContextValue(FileErrorContext.FILENAME, filename); } logger.info("File [{}] found in folder [{}]", ret, folderPath); return ret; }
/** * Copy a folder to a target folder. * * @param sourceFolder the source folder * @param targetFolder the target folder * @return the copied folder */ public static File copyFolder(final File sourceFolder, final File targetFolder) { Validate.notNull(targetFolder); Validate.notNull(sourceFolder); if (!sourceFolder.isDirectory()) { throw new IllegalArgumentException("at least one of the parameters is not a directory"); } try { FileUtils.copyDirectory(sourceFolder, targetFolder); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, "Cannot copy folder", e) .addContextValue(FileErrorContext.FOLDER, sourceFolder) .addContextValue(FileErrorContext.FOLDER, targetFolder); } logger.info("Copied folder [{}] to [{}}", sourceFolder, targetFolder); return targetFolder; }
/** * Copy a file to a target folder. * * @param filePath the file * @param targetFolderPath the target folder * @return the copied file */ public static Path copyFileToFolder(final Path filePath, final Path targetFolderPath) { Validate.notNull(targetFolderPath); Validate.notNull(filePath); if (!Files.isDirectory(targetFolderPath)) { throw new IllegalArgumentException("target is not a directory " + targetFolderPath); } if (!Files.isRegularFile(filePath)) { throw new IllegalArgumentException("Given file is not a file " + filePath); } final Path targetPath = targetFolderPath.resolve(filePath.getFileName()); final Path ret; try { ret = Files.copy(filePath, targetPath); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, "Cannot copy file", e) .addContextValue(FileErrorContext.FILE, filePath) .addContextValue(FileErrorContext.FILE, targetPath); } logger.info("Copied file [{}] to [{}}", filePath, ret); return ret; }
/** * Parse an XML file using a SAX parser, with a given SAX handler. * * @param xmlFile the XML file. * @param saxHandler the SAX handler. */ public static void parseXmlFile(final File xmlFile, final DefaultHandler saxHandler) { try { SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); saxParser.parse(xmlFile, saxHandler); } catch (ApplicationException e) { throw e; } catch (Exception e) { throw new SystemException(XmlErrorType.ERROR_PARSING_XML, "Cannot parse xml file with sax parser", e) .addContextValue(XmlErrorContext.XML_FILE, xmlFile) .addContextValue("saxHandler", saxHandler); } } }
/** * Copy a source file to a target file. * * @param file the source file. * @param outputFile the target file. * @return the target file. */ public static File copyFile(final File file, final File outputFile) { try { FileUtils.copyFile(file, outputFile); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, file) .addContextValue(FileErrorContext.FILE, outputFile); } return outputFile; }
/** * Copy a source file to a target file. * * @param filePath the path to the source file. * @param outputFilePath the path to the target file. * @return the path to the target file. */ public static Path copyFile(final Path filePath, final Path outputFilePath) { final Path ret; try { ret = Files.copy(filePath, outputFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, filePath) .addContextValue(FileErrorContext.FILE, outputFilePath); } return ret; }
/** * Copy a file to a target folder. * * @param file the file * @param targetFolder the target folder * @return the copied file */ public static File copyFileToFolder(final File file, final File targetFolder) { Validate.notNull(targetFolder); Validate.notNull(file); if (!targetFolder.isDirectory()) { throw new IllegalArgumentException("target is not a directory " + targetFolder); } if (!file.isFile()) { throw new IllegalArgumentException("Given file is not a file " + file); } File target = new File(targetFolder, file.getName()); try { FileUtils.copyFile(file, target); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, "Cannot copy file", e) .addContextValue(FileErrorContext.FILE, file) .addContextValue(FileErrorContext.FILE, target); } logger.info("Copied file [{}] to [{}}", file, target); return target; }
/** * Copy a folder to a target folder. * * @param sourceFolderPath the source folder * @param targetFolderPath the target folder * @return the copied folder */ public static Path copyFolder(final Path sourceFolderPath, final Path targetFolderPath) { Validate.notNull(sourceFolderPath); Validate.notNull(targetFolderPath); if (!Files.isDirectory(sourceFolderPath)) { throw new IllegalArgumentException("at least one of the parameters is not a directory"); } try { Files.walkFileTree(sourceFolderPath, new DuplicateFileVisitor(sourceFolderPath, targetFolderPath)); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, "Cannot copy folder", e) .addContextValue(FileErrorContext.FOLDER, sourceFolderPath) .addContextValue(FileErrorContext.FOLDER, targetFolderPath); } logger.info("Copied folder [{}] to [{}}", sourceFolderPath, targetFolderPath); return targetFolderPath; }
@Override public Collection<S3ObjectSummary> listObjects(final String prefix) { Validate.notNull(prefix); try { ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName(), prefix); final List<S3ObjectSummary> ret = result.getObjectSummaries(); logger.info("List [{}] objects with prefix [{}] from AWS S3 bucket [{}]", ret.size(), prefix, awsS3Bucket.getName()); return ret; } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST) .addContextValue(AwsS3ErrorContext.PREFIX, prefix); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@Override public File downloadObject(final String key, final File outputFile) { Validate.notNull(key); try { final S3Object o = amazonS3.getObject(awsS3Bucket.getName(), key); try (S3ObjectInputStream s3is = o.getObjectContent()) { FileIOUtils.writeFile(s3is, outputFile); } } catch (AmazonServiceException | IOException e) { throw new SystemException(AwsS3ErrorType.CANNOT_READ_OBJECT, e) .addContextValue(AwsS3ErrorContext.KEY, key) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.READ); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Downloaded object [{}] from AWS S3 bucket [{}} to [{}]", key, awsS3Bucket.getName(), outputFile); return outputFile; }
.addContextValue(CommonErrorContext.CAUSE, e.getMessage()); } catch (Exception e) { throw new SystemException(XPathErrorType.ERROR_EXTRACTION_XPATH_VARIABLES, e)