/** * Build a new {@link InputSource} from a byte array. * * @param content the byte array * @return the input source */ public static InputSource newInputSource(final byte[] content) { try (InputStream is = new ByteArrayInputStream(content)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * Build a new {@link InputSource} from a {@link String}. * * @param content the content as {@link String} * @return the input source */ public static InputSource newInputSource(final String content) { try (InputStream is = IOUtils.toInputStream(content, StandardCharsets.UTF_8)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * Build a new {@link InputSource} from a collection of{@link String}. * * @param content the content as a collection {@link String} * @return the input source */ public static InputSource newInputSource(final Collection<String> content) { try (InputStream is = IOUtils.toInputStream(StringUtils.join(content, " "), StandardCharsets.UTF_8)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
public static byte[] readFiletoByteArray(final File file) { Validate.notNull(file); try { return FileUtils.readFileToByteArray(file); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, file); } } }
private void init() { this.prefixNamespaceMap = XmlNamespaceUtils.extractNamespaces(this.inputSource); this.prefixNamespaceMap.put("fn", "http://www.w3.org/2005/xpath-functions"); this.prefixNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); this.prefixNamespaceMap.put("op", "http://www.w3.org/2002/08/xquery-operators"); try { inputSource.getByteStream().reset(); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_RESETTING_INPUT_SOURCE, e); } }
/** * Provide {@code LocalDateTime.now()} as an {@link XMLGregorianCalendar}. * * @return the current moment as {@link XMLGregorianCalendar}. */ public static XMLGregorianCalendar nowAsGregorian() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(LocalDateTime.now().toString()); } catch (DatatypeConfigurationException e) { throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e); } }
/** * Build a new {@link InputSource} from a {@link File}. * * @param file the file * @return the input source */ public static InputSource newInputSource(final File file) { try (InputStream is = FileIOUtils.openInputStream(file)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * Check is a {@link Path file} is empty. * * @param filePath the path to a file. * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final Path filePath) { try { return Files.size(filePath) == 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .addContextValue(FileErrorContext.FILE, filePath); } }
/** * Open an {@link InputStream} on a file {@link File}. * * @param file the file. * @return the input stream. */ public static InputStream openInputStream(final File file) { try { return new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_OPENING_STREAM, e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Convert a {@link LocalDateTime} to {@link XMLGregorianCalendar}. * * @param localDateTime the date time to convert. * @return the date time converted as XML Gregorian date. */ public static XMLGregorianCalendar toXmlGregorian(final LocalDateTime localDateTime) { Validate.notNull(localDateTime); try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDateTime.toString()); } catch (DatatypeConfigurationException e) { throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e); } }
/** * Read the content of a {@link File} as a collection of {@link String} lines. * * @param file the file. * @return the collection if {@link String} lines. */ public static Collection<String> readLines(final File file) { Validate.notNull(file); try { return FileUtils.readLines(file, StandardCharsets.UTF_8); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Check if the content of a folder is empty (i.e. doesn't contain any folder nor file) * * @param folderPath the folder * @return true if the folder is empty, false otherwise */ public static boolean isFolderEmpty(final Path folderPath) { Validate.notNull(folderPath); try { return Files.list(folderPath).count() <= 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FOLDER, folderPath); } }
/** * Check if a file is empty. * * @param file the file * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final File file) { try { final List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8); return lines.isEmpty(); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .addContextValue(FileErrorContext.FILE, file); } }
@Override public void deleteObject(final String key) { Validate.notNull(key); try { amazonS3.deleteObject(awsS3Bucket.getName(), key); logger.info("Deleted object [{}] from AWS S3 bucket [{}]", key, awsS3Bucket.getName()); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.DELETE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@Override public PutObjectResult uploadObject(final String key, final String content) { Validate.notNull(key); Validate.notNull(content); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, content); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded content to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret); return ret; }
@Override public PutObjectResult uploadObject(final String key, final File file) { Validate.notNull(key); Validate.notNull(file); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, file); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded file [{}] to AWS S3 bucket [{}] : [{}]", file, awsS3Bucket.getName(), ret); return ret; }
@Override public PutObjectResult uploadObject(final String key, final InputStream inputStream, final ObjectMetadata objectMetadata) { Validate.notNull(key); Validate.notNull(inputStream); final PutObjectResult ret; try { ret = amazonS3.putObject(awsS3Bucket.getName(), key, inputStream, objectMetadata); } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } logger.info("Uploaded stream to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret); return ret; }
/** * 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; }
@Override public Collection<S3ObjectSummary> listObjects() { try { ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName()); final List<S3ObjectSummary> ret = result.getObjectSummaries(); logger.info("List [{}] objects from AWS S3 bucket [{}]", ret.size(), awsS3Bucket.getName()); return ret; } catch (AmazonServiceException e) { throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e) .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST); } catch (SdkClientException e) { throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e); } }
@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); } }