Example usage for org.springframework.integration.file FileHeaders ORIGINAL_FILE

List of usage examples for org.springframework.integration.file FileHeaders ORIGINAL_FILE

Introduction

In this page you can find the example usage for org.springframework.integration.file FileHeaders ORIGINAL_FILE.

Prototype

String ORIGINAL_FILE

To view the source code for org.springframework.integration.file FileHeaders ORIGINAL_FILE.

Click Source Link

Usage

From source file:com.japp.FileCompressor.java

/**
 * Actual Spring Integration transformation handler.
 *
 * @param inputMessage Spring Integration input message
 * @return New Spring Integration message with updated headers
 *///from w  w  w . jav a2  s . c  om
@Transformer
public Message<String> handleFile(final Message<File> inputMessage) {

    final File inputFile = inputMessage.getPayload();
    final String filename = inputFile.getName();
    final String fileExtension = FilenameUtils.getExtension(filename);

    final String inputAsString;

    try {
        inputAsString = FileUtils.readFileToString(inputFile);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    final Message<String> message = MessageBuilder.withPayload(inputAsString.toUpperCase(Locale.ENGLISH))
            .setHeader(FileHeaders.FILENAME, filename).setHeader(FileHeaders.ORIGINAL_FILE, inputFile)
            .setHeader("file_size", inputFile.length()).setHeader("file_extension", fileExtension).build();

    return message;
}

From source file:com.excelsiorsoft.transformer.TransformationHandler.java

/**
 * Actual Spring Integration transformation handler.
 *
 * @param inputMessage Spring Integration input message
 * @return New Spring Integration message with updated headers
 *///from   ww w. j av  a  2 s. c o m
@Transformer
public Message<byte[]> handleFile(final Message<File> inputMessage) {

    final File inputFile = inputMessage.getPayload();
    final String filename = inputFile.getName();

    final String inputAsString;

    try {
        inputAsString = FileUtils.readFileToString(inputFile);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("Sample Sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 0);
    // Create a cell and put a value in it.
    Cell cell = row.createCell(0);

    cell.setCellValue(inputAsString);

    try {
        wb.write(bout);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    final Message<byte[]> message = MessageBuilder.withPayload(bout.toByteArray())
            .setHeader(FileHeaders.FILENAME, filename + ".xls").setHeader(FileHeaders.ORIGINAL_FILE, inputFile)
            .setHeader("file_size", inputFile.length()).build();

    return message;
}

From source file:au.edu.uws.eresearch.cr8it.TransformationHandler.java

/**
 * Actual Spring Integration transformation handler.
 *
 * @param inputMessage Spring Integration input message
 * @return New Spring Integration message with updated headers
 *///from   w  w  w  .j av  a2s. c om
@Transformer
public Message<String> handleFile(final Message<File> inputMessage) {

    final File inputFile = inputMessage.getPayload();
    final String filename = inputFile.getName();
    final String fileExtension = FilenameUtils.getExtension(filename);

    //populate this with json-ld data
    final String inputAsString;
    String finalString = "";

    if ("zip".equals(fileExtension)) {
        inputAsString = getJsonData(inputFile, FilenameUtils.getName(filename));

        try {
            finalString = getJsonMapping(inputAsString);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        if (finalString.length() > 0) {
            final Message<String> message = MessageBuilder.withPayload(finalString)
                    .setHeader(FileHeaders.FILENAME, FilenameUtils.getBaseName(filename) + ".json")
                    .setHeader(FileHeaders.ORIGINAL_FILE, inputFile)
                    .setHeader("file_size", finalString.length()).setHeader("file_extension", "json").build();

            return message;
        } else {
            System.out.println("Empty json string.");
            return null;
        }
    } else {
        System.out.println("Invalid file format");
        return null;
    }
}

From source file:org.springframework.integration.file.FileWritingMessageHandler.java

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    Assert.notNull(requestMessage, "message must not be null");
    Object payload = requestMessage.getPayload();
    Assert.notNull(payload, "message payload must not be null");
    String generatedFileName = this.fileNameGenerator.generateFileName(requestMessage);
    File originalFileFromHeader = this.retrieveOriginalFileFromHeader(requestMessage);

    final File destinationDirectoryToUse = evaluateDestinationDirectoryExpression(requestMessage);

    File tempFile = new File(destinationDirectoryToUse, generatedFileName + temporaryFileSuffix);
    File resultFile = new File(destinationDirectoryToUse, generatedFileName);

    if (FileExistsMode.FAIL.equals(this.fileExistsMode) && resultFile.exists()) {
        throw new MessageHandlingException(requestMessage,
                "The destination file already exists at '" + resultFile.getAbsolutePath() + "'.");
    }//ww  w.  j  a  va 2  s.  com

    final boolean ignore = FileExistsMode.IGNORE.equals(this.fileExistsMode)
            && (resultFile.exists() || (StringUtils.hasText(this.temporaryFileSuffix) && tempFile.exists()));

    if (!ignore) {

        try {
            if (payload instanceof File) {
                resultFile = this.handleFileMessage((File) payload, tempFile, resultFile);
            } else if (payload instanceof byte[]) {
                resultFile = this.handleByteArrayMessage((byte[]) payload, originalFileFromHeader, tempFile,
                        resultFile);
            } else if (payload instanceof String) {
                resultFile = this.handleStringMessage((String) payload, originalFileFromHeader, tempFile,
                        resultFile);
            } else {
                throw new IllegalArgumentException(
                        "unsupported Message payload type [" + payload.getClass().getName() + "]");
            }
        } catch (Exception e) {
            throw new MessageHandlingException(requestMessage, "failed to write Message payload to file", e);
        }

    }

    if (!this.expectReply) {
        return null;
    }

    if (resultFile != null) {
        if (originalFileFromHeader == null && payload instanceof File) {
            return MessageBuilder.withPayload(resultFile).setHeader(FileHeaders.ORIGINAL_FILE, payload);
        }
    }
    return resultFile;
}

From source file:org.springframework.integration.file.FileWritingMessageHandler.java

/**
 * Retrieves the File instance from the {@link FileHeaders#ORIGINAL_FILE}
 * header if available. If the value is not a File instance or a String
 * representation of a file path, this will return <code>null</code>.
 *//*  w ww.ja v a2  s .  c o  m*/
private File retrieveOriginalFileFromHeader(Message<?> message) {
    Object value = message.getHeaders().get(FileHeaders.ORIGINAL_FILE);
    if (value instanceof File) {
        return (File) value;
    }
    if (value instanceof String) {
        return new File((String) value);
    }
    return null;
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithStringPayloadAndFileInstanceHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);/*from  w  w w.  jav  a 2  s  .c o  m*/
    handler.setOutputChannel(output);
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT)
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithStringPayloadAndFilePathHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);//from   www  . j ava  2 s.c om
    handler.setOutputChannel(output);
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT)
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile.getAbsolutePath()).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithByteArrayPayloadAndFileInstanceHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);//www. j  ava  2 s .  c  om
    handler.setOutputChannel(output);
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT.getBytes(DEFAULT_ENCODING))
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithByteArrayPayloadAndFilePathHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);/* w w w.j a  v  a  2  s  .  c o  m*/
    handler.setOutputChannel(output);
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT.getBytes(DEFAULT_ENCODING))
            .setHeader(FileHeaders.ORIGINAL_FILE, sourceFile.getAbsolutePath()).build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void deleteSourceFileWithInputstreamPayloadAndFileInstanceHeader() throws Exception {
    QueueChannel output = new QueueChannel();
    handler.setCharset(DEFAULT_ENCODING);
    handler.setDeleteSourceFiles(true);//from   w  ww .  j a  va  2s .c o m
    handler.setOutputChannel(output);

    InputStream is = new FileInputStream(sourceFile);

    Message<?> message = MessageBuilder.withPayload(is).setHeader(FileHeaders.ORIGINAL_FILE, sourceFile)
            .build();
    assertTrue(sourceFile.exists());
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIsMatching(result);
    assertFalse(sourceFile.exists());
}